Reputation: 5658
I've run into a problem where my local config overrides global but I need local to remove not just override.
E.g.
// global.php
'mail_transport' => [
'type' => 'Zend\Mail\Transport\Smtp',
'options' => [
'host' => 'smtp.gmail.com',
'port' => 587,
'connectionClass' => 'login',
'connectionConfig' => [
// ...
],
],
], // ...
// local.php
'mail_transport' => [
'type' => 'Zend\Mail\Transport\File',
'options' => [
'path' => 'data/mail/',
]
],
// ...
So, mail_transport
is being overridden, yet its options host
, port
, connectionClass
remain and muck up the mail transport factory. Is there any way to override as I'd like? Or is the only way to edit global.php directly?
Upvotes: 1
Views: 255
Reputation: 9857
You can add a listener on the event Zend\ModuleManager\ModuleEvent::EVENT_MERGE_CONFIG
to remove the required options.
Zend\ModuleManager\Listener\ConfigListener
triggers a special event,Zend\ModuleManager\ModuleEvent::EVENT_MERGE_CONFIG
, after merging all configuration, but prior to it being passed to the ServiceManager. By listening to this event, you can inspect the merged configuration and manipulate it.
Such a listener could look like this.
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;
use Zend\ModuleManager\Feature\InitProviderInterface;
class Module implements InitProviderInterface
{
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, [$this, 'removeMailOptions']);
}
public function removeMailOptions(ModuleEvent $event)
{
$listener = $event->getConfigListener();
$config = $listener->getMergedConfig(false);
if (isset($config['mail_transport']['type'])) {
switch($config['mail_transport']['type']) {
case \Zend\Mail\Transport\File::class :
$config['mail_transport']['options'] = [
'path' => $config['mail_transport']['options']['path']
];
break;
}
}
$listener->setMergedConfig($config);
}
}
Upvotes: 1