|
9 | 9 |
|
10 | 10 | namespace Zend\Mail\Protocol; |
11 | 11 |
|
12 | | -use Interop\Container\ContainerInterface; |
| 12 | +use Zend\ServiceManager\AbstractPluginManager; |
| 13 | +use Zend\ServiceManager\Factory\InvokableFactory; |
| 14 | +use Zend\ServiceManager\Exception\InvalidServiceException; |
13 | 15 |
|
14 | 16 | /** |
15 | 17 | * Plugin manager implementation for SMTP extensions. |
16 | 18 | * |
17 | 19 | * Enforces that SMTP extensions retrieved are instances of Smtp. Additionally, |
18 | 20 | * it registers a number of default extensions available. |
19 | 21 | */ |
20 | | -class SmtpPluginManager implements ContainerInterface |
| 22 | +class SmtpPluginManager extends AbstractPluginManager |
21 | 23 | { |
22 | 24 | /** |
23 | | - * Default set of plugins |
| 25 | + * Service aliases |
| 26 | + */ |
| 27 | + protected $aliases = [ |
| 28 | + 'crammd5' => Smtp\Auth\Crammd5::class, |
| 29 | + 'cramMd5' => Smtp\Auth\Crammd5::class, |
| 30 | + 'CramMd5' => Smtp\Auth\Crammd5::class, |
| 31 | + 'cramMD5' => Smtp\Auth\Crammd5::class, |
| 32 | + 'CramMD5' => Smtp\Auth\Crammd5::class, |
| 33 | + 'login' => Smtp\Auth\Login::class, |
| 34 | + 'Login' => Smtp\Auth\Login::class, |
| 35 | + 'plain' => Smtp\Auth\Plain::class, |
| 36 | + 'Plain' => Smtp\Auth\Plain::class, |
| 37 | + 'smtp' => Smtp::class, |
| 38 | + 'Smtp' => Smtp::class, |
| 39 | + 'SMTP' => Smtp::class, |
| 40 | + ]; |
| 41 | + |
| 42 | + /** |
| 43 | + * Service factories |
24 | 44 | * |
25 | 45 | * @var array |
26 | 46 | */ |
27 | | - protected $plugins = [ |
28 | | - 'crammd5' => 'Zend\Mail\Protocol\Smtp\Auth\Crammd5', |
29 | | - 'login' => 'Zend\Mail\Protocol\Smtp\Auth\Login', |
30 | | - 'plain' => 'Zend\Mail\Protocol\Smtp\Auth\Plain', |
31 | | - 'smtp' => 'Zend\Mail\Protocol\Smtp', |
| 47 | + protected $factories = [ |
| 48 | + Smtp\Auth\Crammd5::class => InvokableFactory::class, |
| 49 | + Smtp\Auth\Login::class => InvokableFactory::class, |
| 50 | + Smtp\Auth\Plain::class => InvokableFactory::class, |
| 51 | + Smtp::class => InvokableFactory::class, |
| 52 | + |
| 53 | + // v2 normalized service names |
| 54 | + |
| 55 | + 'zendmailprotocolsmtpauthcrammd5' => InvokableFactory::class, |
| 56 | + 'zendmailprotocolsmtpauthlogin' => InvokableFactory::class, |
| 57 | + 'zendmailprotocolsmtpauthplain' => InvokableFactory::class, |
| 58 | + 'zendmailprotocolsmtp' => InvokableFactory::class, |
32 | 59 | ]; |
33 | 60 |
|
34 | 61 | /** |
35 | | - * Do we have the plugin? |
| 62 | + * Plugins must be an instance of the Smtp class |
36 | 63 | * |
37 | | - * @param string $id |
38 | | - * @return bool |
| 64 | + * @var string |
39 | 65 | */ |
40 | | - public function has($id) |
| 66 | + protected $instanceOf = Smtp::class; |
| 67 | + |
| 68 | + /** |
| 69 | + * Validate a retrieved plugin instance (v3). |
| 70 | + * |
| 71 | + * @param object $plugin |
| 72 | + * @throws InvalidServiceException |
| 73 | + */ |
| 74 | + public function validate($plugin) |
41 | 75 | { |
42 | | - return array_key_exists($id, $this->plugins); |
| 76 | + if (! $plugin instanceof $this->instanceOf) { |
| 77 | + throw new InvalidServiceException(sprintf( |
| 78 | + 'Plugin of type %s is invalid; must extend %s', |
| 79 | + (is_object($plugin) ? get_class($plugin) : gettype($plugin)), |
| 80 | + Smtp::class |
| 81 | + )); |
| 82 | + } |
43 | 83 | } |
| 84 | + |
44 | 85 | /** |
45 | | - * Retrieve the smtp plugin |
| 86 | + * Validate a retrieved plugin instance (v2). |
46 | 87 | * |
47 | | - * @param string $id |
48 | | - * @param array $options |
49 | | - * @return AbstractProtocol |
| 88 | + * @param object $plugin |
| 89 | + * @throws Exception\InvalidArgumentException |
50 | 90 | */ |
51 | | - public function get($id, array $options = null) |
| 91 | + public function validatePlugin($plugin) |
52 | 92 | { |
53 | | - $class = $this->plugins[$id]; |
54 | | - return new $class($options); |
| 93 | + try { |
| 94 | + $this->validate($plugin); |
| 95 | + } catch (InvalidServiceException $e) { |
| 96 | + throw new Exception\InvalidArgumentException( |
| 97 | + $e->getMessage(), |
| 98 | + $e->getCode(), |
| 99 | + $e |
| 100 | + ); |
| 101 | + } |
55 | 102 | } |
56 | 103 | } |
0 commit comments