I have a problem to get list of all transports defined for mailer component.
In my config/packages/mailer.yaml
I have defined three transports:
framework:
mailer:
transports:
default: '%env(MAILER_DEFAULT_DSN)%'
first: '%env(MAILER_FIRST_DSN)%'
second: '%env(MAILER_SECOND_DSN)%'
Now, in my code I want to get list of defined transport in associative array manner:
[
'default' => 'default_mailer_dsn',
'first' => 'first_mailer_dsn',
'second' => 'second_mailer_dsn',
]
Any idea how to do it?
So this does not exactly answer the question but will yield a result that looks like:
You get the desired array indexes along with the actual transport objects.
Using bin/console debug:container mailer.transports reveals the existence of a Transports object:
$transports is what we are after but it is not accessible without reflection and the class is final so we can't really extend it. A brute force solution is to clone(i.e copy) the complete class into the App namespace and convince the mailer to use it:
Convincing the mailer system to use our Transports is the hard part. Normally you would just add a compiler pass and change the class for the mailer.transports service to the App transport. Sadly, this does not work since the actual service definition uses a factory to create transports and there does not seem to be a specific point for changing the Transports class. Specifically:
As you can see the Transports class name is hardcoded. I fooled around a bit trying to extend the class and convince it to use a different Transports class but the return type of Transports makes it difficult. As a proof of concept I decided to just copy the whole class to the App namespace and see what happens. You would not normally do this because there is quite a bit of code in the class. Code which might get updated as time goes by. I also renamed the class to TransportFactory because the name Transport seems to be a but overused. And then change the service definition to use the new class.
At this point you should have a working example yielding the array I gave at the beginning of the answer. It's quite a bit of work for what should have been a simple request. Maybe somebody else will now come along and say, hey, just do this.
Obviously you should carefully consider if you really do need this capability. Might be better to just read config/packages/mailer.yaml directly from your app's extension.