Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Add a mail plugin manager
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Apr 8, 2014
1 parent 369e49b commit aa4548d
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 8 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
);
```

Expand Down Expand Up @@ -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);
```

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
6 changes: 6 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
],
],

'mail_manager' => [
'invokables' => [
'DefaultMail' => 'Phpro\MailManager\Mail\DefaultMail',
],
],

'view_manager' => [
'template_map' => [
'mails/layout' => __DIR__ . '/../view/mails/layout.phtml',
Expand Down
21 changes: 21 additions & 0 deletions spec/Phpro/MailManager/ModuleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

}
4 changes: 3 additions & 1 deletion spec/Phpro/MailManager/Service/Factory/MailManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
21 changes: 21 additions & 0 deletions spec/Phpro/MailManager/Service/Factory/MailPluginManagerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace spec\Phpro\MailManager\Service\Factory;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class MailPluginManagerSpec extends ObjectBehavior
{

public function it_is_initializable()
{
$this->shouldHaveType('Phpro\MailManager\Service\Factory\MailPluginManager');
}

public function it_is_a_abstract_plugin_manager_factory()
{
$this->shouldHaveType('Zend\Mvc\Service\AbstractPluginManagerFactory');
}

}
5 changes: 3 additions & 2 deletions spec/Phpro/MailManager/Service/MailManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
37 changes: 37 additions & 0 deletions spec/Phpro/MailManager/Service/MailPluginManagerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace spec\Phpro\MailManager\Service;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class MailPluginManagerSpec extends ObjectBehavior
{
/**
* @param \Zend\ServiceManager\ConfigInterface $configuration
*/
public function let($configuration)
{
$this->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);
}
}
26 changes: 25 additions & 1 deletion src/Phpro/MailManager/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,7 +13,8 @@
*/
class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface
ConfigProviderInterface,
InitProviderInterface
{
/**
* @return mixed
Expand All @@ -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'
);
}

}
4 changes: 3 additions & 1 deletion src/Phpro/MailManager/Service/Factory/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class MailManager
implements FactoryInterface
{

/**
* Create service
*
Expand All @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Phpro/MailManager/Service/Factory/MailPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Phpro\MailManager\Service\Factory;

use Zend\Mvc\Service\AbstractPluginManagerFactory;

/**
* Class TaskPluginManager
*
* @package TaskManager\Service\Factory
*/
class MailPluginManager extends AbstractPluginManagerFactory
{
const PLUGIN_MANAGER_CLASS = 'Phpro\MailManager\Service\MailPluginManager';
}
38 changes: 37 additions & 1 deletion src/Phpro/MailManager/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,64 @@
namespace Phpro\MailManager\Service;
use Phpro\MailManager\Adapter\AdapterInterface;
use Phpro\MailManager\Mail\MailInterface;
use Zend\ServiceManager\Exception;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Class MailManager
*
* @package Phpro\MailManager\Service
*/
class MailManager
implements ServiceLocatorInterface
{

/**
* @var MailPluginManager
*/
protected $pluginManager;

/**
* @var AdapterInterface
*/
protected $adapter;

/**
* @param $pluginmanager
* @param $adapter
*/
public function __construct($adapter)
public function __construct($pluginmanager, $adapter)
{
$this->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
*/
Expand Down
40 changes: 40 additions & 0 deletions src/Phpro/MailManager/Service/MailPluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Phpro\MailManager\Service;

use Phpro\MailManager\Mail\MailInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\RuntimeException;

/**
* Class TaskPluginManager
*
* @package TaskManager\Service
*/
class MailPluginManager extends AbstractPluginManager
{

/**
* Whether or not to share by default
*
* @var bool
*/
protected $shareByDefault = false;

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof MailInterface) {
// we're okay
return;
}

throw new RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement MailInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}

}

0 comments on commit aa4548d

Please sign in to comment.