From aa4548d1b5ad3023f36f21699d85f9bfb1727bec Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Tue, 8 Apr 2014 16:04:26 +0200 Subject: [PATCH] Add a mail plugin manager --- README.md | 26 +++++++++++- composer.json | 1 + config/module.config.php | 6 +++ spec/Phpro/MailManager/ModuleSpec.php | 21 ++++++++++ .../Service/Factory/MailManagerSpec.php | 4 +- .../Service/Factory/MailPluginManagerSpec.php | 21 ++++++++++ .../MailManager/Service/MailManagerSpec.php | 5 ++- .../Service/MailPluginManagerSpec.php | 37 +++++++++++++++++ src/Phpro/MailManager/Module.php | 26 +++++++++++- .../Service/Factory/MailManager.php | 4 +- .../Service/Factory/MailPluginManager.php | 15 +++++++ src/Phpro/MailManager/Service/MailManager.php | 38 +++++++++++++++++- .../MailManager/Service/MailPluginManager.php | 40 +++++++++++++++++++ 13 files changed, 236 insertions(+), 8 deletions(-) create mode 100644 spec/Phpro/MailManager/Service/Factory/MailPluginManagerSpec.php create mode 100644 spec/Phpro/MailManager/Service/MailPluginManagerSpec.php create mode 100644 src/Phpro/MailManager/Service/Factory/MailPluginManager.php create mode 100644 src/Phpro/MailManager/Service/MailPluginManager.php diff --git a/README.md b/README.md index 71b0c7a..c20a1b0 100644 --- a/README.md +++ b/README.md @@ -30,17 +30,33 @@ return array( ### Add your custom mail settings ```php return array( + // + // Default Mailmanager settings: + // 'service_manager' => array( 'aliases' => array( 'Phpro\MailManager\DefaultAdapter' => 'Phpro\MailManager\Adapter\ZendMailAdapter', ) ), + + // + // Paths to e-mail templates + // 'view_manager' => [ 'template_map' => [ 'mails/layout' => __DIR__ . '/../view/mails/layout.phtml', 'mails/customer/registered' => __DIR__ . '/../view/mails/customer/registered.phtml', ], - ] + ], + + // + // Custom e-mail plugin manager + // + 'mail_manager' => [ + 'invokables' => [ + 'CustomerRegisteredMail' => 'CustomerRegisteredMail', + ], + ], ); ``` @@ -72,8 +88,14 @@ class CustomerRegisteredMail extends DefaultMail ### Sending your e-mail: ```php -$mail = new CustomerRegisteredMail(); +// Through the mail plugin manager: $mailManager = $serviceManager->get('Phpro\MailManager'); +$mail = $mailManager->get('CustomerRegisteredMail'); +$mailManager->send($mail); + +// Without the mail plugin manager: +$mailManager = $serviceManager->get('Phpro\MailManager'); +$mail = new CustomerRegisteredMail(); $mailManager->send($mail); ``` diff --git a/composer.json b/composer.json index d5874d9..7ab75f4 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "slm/mail": "~1.4.0", "zendframework/zend-mail": "2.*", "zendframework/zend-mime": "2.*", + "zendframework/zend-mvc": "2.*", "zendframework/zend-modulemanager": "2.*", "zendframework/zend-servicemanager": "2.*", "zendframework/zend-view": "2.*" diff --git a/config/module.config.php b/config/module.config.php index 429d630..814cec8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -13,6 +13,12 @@ ], ], + 'mail_manager' => [ + 'invokables' => [ + 'DefaultMail' => 'Phpro\MailManager\Mail\DefaultMail', + ], + ], + 'view_manager' => [ 'template_map' => [ 'mails/layout' => __DIR__ . '/../view/mails/layout.phtml', diff --git a/spec/Phpro/MailManager/ModuleSpec.php b/spec/Phpro/MailManager/ModuleSpec.php index 787d789..2c36666 100644 --- a/spec/Phpro/MailManager/ModuleSpec.php +++ b/spec/Phpro/MailManager/ModuleSpec.php @@ -22,6 +22,11 @@ public function it_should_implement_config_provider_interface() $this->shouldImplement('Zend\ModuleManager\Feature\ConfigProviderInterface'); } + public function it_should_implement_module_provider_interface() + { + $this->shouldImplement('Zend\ModuleManager\Feature\InitProviderInterface'); + } + public function it_should_load_config() { $this->getConfig()->shouldBeArray(); @@ -32,4 +37,20 @@ public function it_should_load_autoloader_config() $this->getAutoloaderConfig()->shouldBeArray(); } + /** + * @param \Zend\ModuleManager\ModuleManager $moduleManager + * @param \Zend\ModuleManager\ModuleEvent $event + * @param \Zend\ServiceManager\ServiceManager $serviceManager + * @param \Zend\ModuleManager\Listener\ServiceListenerInterface $serviceListener + */ + public function it_should_add_the_mailmanager_as_service_plugin_manager($moduleManager, $event, $serviceManager, $serviceListener) + { + $moduleManager->getEvent()->willReturn($event); + $event->getParam('ServiceManager')->willReturn($serviceManager); + $serviceManager->get('ServiceListener')->willReturn($serviceListener); + $serviceListener->addServiceManager('Phpro\MailManager\PluginManager', Argument::cetera())->shouldBeCalled(); + + $this->init($moduleManager); + } + } diff --git a/spec/Phpro/MailManager/Service/Factory/MailManagerSpec.php b/spec/Phpro/MailManager/Service/Factory/MailManagerSpec.php index 0f8093a..61bc4b1 100644 --- a/spec/Phpro/MailManager/Service/Factory/MailManagerSpec.php +++ b/spec/Phpro/MailManager/Service/Factory/MailManagerSpec.php @@ -19,10 +19,12 @@ public function it_is_a_factory() /** * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceManager + * @param \Phpro\MailManager\Service\MailPluginManager $pluginManager * @param \Phpro\MailManager\Adapter\AdapterInterface $adapter */ - public function it_should_create_an_instance($serviceManager, $adapter) + public function it_should_create_an_instance($serviceManager, $pluginManager, $adapter) { + $serviceManager->get('Phpro\MailManager\PluginManager')->willReturn($pluginManager); $serviceManager->get('Phpro\MailManager\DefaultAdapter')->willReturn($adapter); $this->createService($serviceManager)->shouldBeAnInstanceOf('Phpro\MailManager\Service\MailManager'); diff --git a/spec/Phpro/MailManager/Service/Factory/MailPluginManagerSpec.php b/spec/Phpro/MailManager/Service/Factory/MailPluginManagerSpec.php new file mode 100644 index 0000000..1b54ea6 --- /dev/null +++ b/spec/Phpro/MailManager/Service/Factory/MailPluginManagerSpec.php @@ -0,0 +1,21 @@ +shouldHaveType('Phpro\MailManager\Service\Factory\MailPluginManager'); + } + + public function it_is_a_abstract_plugin_manager_factory() + { + $this->shouldHaveType('Zend\Mvc\Service\AbstractPluginManagerFactory'); + } + +} diff --git a/spec/Phpro/MailManager/Service/MailManagerSpec.php b/spec/Phpro/MailManager/Service/MailManagerSpec.php index 03d36f8..8a4646f 100644 --- a/spec/Phpro/MailManager/Service/MailManagerSpec.php +++ b/spec/Phpro/MailManager/Service/MailManagerSpec.php @@ -9,11 +9,12 @@ class MailManagerSpec extends ObjectBehavior { /** + * @param \Phpro\MailManager\Service\MailPluginManager $pluginManager * @param \Phpro\MailManager\Adapter\AdapterInterface $adapter */ - public function let($adapter) + public function let($pluginManager, $adapter) { - $this->beConstructedWith($adapter); + $this->beConstructedWith($pluginManager, $adapter); } public function it_is_initializable() diff --git a/spec/Phpro/MailManager/Service/MailPluginManagerSpec.php b/spec/Phpro/MailManager/Service/MailPluginManagerSpec.php new file mode 100644 index 0000000..3ac4b1e --- /dev/null +++ b/spec/Phpro/MailManager/Service/MailPluginManagerSpec.php @@ -0,0 +1,37 @@ +beConstructedWith($configuration); + } + + public function it_is_initializable() + { + $this->shouldHaveType('Phpro\MailManager\Service\MailPluginManager'); + } + + public function it_is_an_abstract_plugin_manager() + { + $this->shouldHaveType('Zend\ServiceManager\AbstractPluginManager'); + } + + /** + * @param \Phpro\MailManager\Mail\MailInterface $plugin + * @param \stdClass $invalid + */ + public function it_should_validate_loaded_instace($plugin, $invalid) + { + $this->shouldNotThrow()->duringValidatePlugin($plugin); + $this->shouldThrow('\Zend\ServiceManager\Exception\RuntimeException')->duringValidatePlugin($invalid); + } +} diff --git a/src/Phpro/MailManager/Module.php b/src/Phpro/MailManager/Module.php index 56cb4ca..01627ac 100644 --- a/src/Phpro/MailManager/Module.php +++ b/src/Phpro/MailManager/Module.php @@ -3,6 +3,8 @@ use Zend\ModuleManager\Feature\AutoloaderProviderInterface; use Zend\ModuleManager\Feature\ConfigProviderInterface; +use Zend\ModuleManager\Feature\InitProviderInterface; +use Zend\ModuleManager\ModuleManagerInterface; /** * Class Module @@ -11,7 +13,8 @@ */ class Module implements AutoloaderProviderInterface, - ConfigProviderInterface + ConfigProviderInterface, + InitProviderInterface { /** * @return mixed @@ -34,4 +37,25 @@ public function getAutoloaderConfig() ), ); } + + /** + * Initialize workflow + * + * @param ModuleManagerInterface $moduleManager + * + * @return void + */ + public function init(ModuleManagerInterface $moduleManager) + { + $serviceManager = $moduleManager->getEvent()->getParam('ServiceManager'); + $serviceListener = $serviceManager->get('ServiceListener'); + + $serviceListener->addServiceManager( + 'Phpro\MailManager\PluginManager', + 'mail_manager', + 'Phpro\MailManager\Mail\MailInterface', + 'getMailManagerConfig' + ); + } + } diff --git a/src/Phpro/MailManager/Service/Factory/MailManager.php b/src/Phpro/MailManager/Service/Factory/MailManager.php index 21744c8..856e06c 100644 --- a/src/Phpro/MailManager/Service/Factory/MailManager.php +++ b/src/Phpro/MailManager/Service/Factory/MailManager.php @@ -14,6 +14,7 @@ class MailManager implements FactoryInterface { + /** * Create service * @@ -23,9 +24,10 @@ class MailManager */ public function createService(ServiceLocatorInterface $serviceLocator) { + $pluginManager = $serviceLocator->get('Phpro\MailManager\PluginManager'); $adapter = $serviceLocator->get('Phpro\MailManager\DefaultAdapter'); - $instance = new Instance($adapter); + $instance = new Instance($pluginManager, $adapter); return $instance; } diff --git a/src/Phpro/MailManager/Service/Factory/MailPluginManager.php b/src/Phpro/MailManager/Service/Factory/MailPluginManager.php new file mode 100644 index 0000000..c8ea2d2 --- /dev/null +++ b/src/Phpro/MailManager/Service/Factory/MailPluginManager.php @@ -0,0 +1,15 @@ +pluginManager = $pluginmanager; $this->adapter = $adapter; } + /** + * Retrieve a registered instance + * + * @param string $name + * + * @throws Exception\ServiceNotFoundException + * @return object|array + */ + public function get($name) + { + return $this->pluginManager->get($name); + } + + /** + * Check for a registered instance + * + * @param string|array $name + * + * @return bool + */ + public function has($name) + { + return $this->pluginManager->has($name); + } + + /** * @param MailInterface $mail */ diff --git a/src/Phpro/MailManager/Service/MailPluginManager.php b/src/Phpro/MailManager/Service/MailPluginManager.php new file mode 100644 index 0000000..0b05ff7 --- /dev/null +++ b/src/Phpro/MailManager/Service/MailPluginManager.php @@ -0,0 +1,40 @@ +