This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathControllerManager.php
113 lines (103 loc) · 3.52 KB
/
ControllerManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Mvc\Controller;
use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\Stdlib\DispatchableInterface;
/**
* Manager for loading controllers
*
* Does not define any controllers by default, but does add a validator.
*/
class ControllerManager extends AbstractPluginManager
{
/**
* We do not want arbitrary classes instantiated as controllers.
*
* @var bool
*/
protected $autoAddInvokableClass = false;
/**
* Controllers must be of this type.
*
* @var string
*/
protected $instanceOf = DispatchableInterface::class;
/**
* Constructor
*
* Injects an initializer for injecting controllers with an
* event manager and plugin manager.
*
* @param ConfigInterface|ContainerInterface $container
* @param array $config
*/
public function __construct($configOrContainerInstance, array $config = [])
{
$this->addInitializer([$this, 'injectEventManager']);
$this->addInitializer([$this, 'injectPluginManager']);
parent::__construct($configOrContainerInstance, $config);
}
/**
* Validate a plugin
*
* {@inheritDoc}
*/
public function validate($plugin)
{
if (! $plugin instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'Plugin of type "%s" is invalid; must implement %s',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
$this->instanceOf
));
}
}
/**
* Initializer: inject EventManager instance
*
* Set a new event manager injected with the shared event manager.
*
* The AbstractController lazy-instantiates an EventManager instance,
* which is why the SharedEventManager injection needs to happen; the
* conditional will always pass.
*
* This works because we fetch the EventManager via the container
* (ServiceManager). So it gets built by the EventManagerFactory,
* which injects the SharedEventManager via EventManager's constructor.
*
* @param ContainerInterface $container
* @param DispatchableInterface $controller
*/
public function injectEventManager(ContainerInterface $container, $controller)
{
if (! $controller instanceof EventManagerAwareInterface) {
return;
}
$events = $controller->getEventManager();
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
$controller->setEventManager($container->get('EventManager'));
}
}
/**
* Initializer: inject plugin manager
*
* @param ContainerInterface $container
* @param DispatchableInterface $controller
*/
public function injectPluginManager(ContainerInterface $container, $controller)
{
if (! method_exists($controller, 'setPluginManager')) {
return;
}
$controller->setPluginManager($container->get('ControllerPluginManager'));
}
}