This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
ControllerManager.php
139 lines (125 loc) · 4.54 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\Controller;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Exception;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
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;
/**
* Constructor
*
* After invoking parent constructor, add an initializer to inject the
* service manager, event manager, and plugin manager
*
* @param null|ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
// Pushing to bottom of stack to ensure this is done last
$this->addInitializer(array($this, 'injectControllerDependencies'), false);
}
/**
* Inject required dependencies into the controller.
*
* @param DispatchableInterface $controller
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function injectControllerDependencies($controller, ServiceLocatorInterface $serviceLocator)
{
if (!$controller instanceof DispatchableInterface) {
return;
}
$parentLocator = $serviceLocator->getServiceLocator();
if ($controller instanceof ServiceLocatorAwareInterface) {
$controller->setServiceLocator($parentLocator->get('Zend\ServiceManager\ServiceLocatorInterface'));
}
if ($controller instanceof EventManagerAwareInterface) {
// If we have an event manager composed already, make sure it gets
// injected with the shared event manager.
// The AbstractController lazy-instantiates an EM instance, which
// is why the shared EM injection needs to happen; the conditional
// will always pass.
$events = $controller->getEventManager();
if (!$events instanceof EventManagerInterface) {
$controller->setEventManager($parentLocator->get('EventManager'));
} else {
$events->setSharedManager($parentLocator->get('SharedEventManager'));
}
}
if ($controller instanceof AbstractConsoleController) {
$controller->setConsole($parentLocator->get('Console'));
}
if (method_exists($controller, 'setPluginManager')) {
$controller->setPluginManager($parentLocator->get('ControllerPluginManager'));
}
}
/**
* Validate the plugin
*
* Ensure we have a dispatchable.
*
* @param mixed $plugin
* @return true
* @throws Exception\InvalidControllerException
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof DispatchableInterface) {
// we're okay
return;
}
throw new Exception\InvalidControllerException(sprintf(
'Controller of type %s is invalid; must implement Zend\Stdlib\DispatchableInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
/**
* Override: do not use peering service managers
*
* @param string|array $name
* @param bool $checkAbstractFactories
* @param bool $usePeeringServiceManagers
* @return bool
*/
public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = false)
{
return parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers);
}
/**
* Override: do not use peering service managers
*
* @param string $name
* @param array $options
* @param bool $usePeeringServiceManagers
* @return mixed
*/
public function get($name, $options = array(), $usePeeringServiceManagers = false)
{
return parent::get($name, $options, $usePeeringServiceManagers);
}
}