use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Controller / PluginManager.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Mvc\Controller;
11
12 use Zend\Mvc\Exception;
13 use Zend\ServiceManager\AbstractPluginManager;
14 use Zend\ServiceManager\Exception\InvalidServiceException;
15 use Zend\ServiceManager\Factory\InvokableFactory;
16 use Zend\Stdlib\DispatchableInterface;
17
18 /**
19 * Plugin manager implementation for controllers
20 *
21 * Registers a number of default plugins, and contains an initializer for
22 * injecting plugins with the current controller.
23 */
24 class PluginManager extends AbstractPluginManager
25 {
26 /**
27 * Plugins must be of this type.
28 *
29 * @var string
30 */
31 protected $instanceOf = Plugin\PluginInterface::class;
32
33 /**
34 * @var string[] Default aliases
35 */
36 protected $aliases = [
37 'AcceptableViewModelSelector' => Plugin\AcceptableViewModelSelector::class,
38 'acceptableViewModelSelector' => Plugin\AcceptableViewModelSelector::class,
39 'acceptableviewmodelselector' => Plugin\AcceptableViewModelSelector::class,
40 'FilePostRedirectGet' => Plugin\FilePostRedirectGet::class,
41 'filePostRedirectGet' => Plugin\FilePostRedirectGet::class,
42 'filepostredirectget' => Plugin\FilePostRedirectGet::class,
43 'fileprg' => Plugin\FilePostRedirectGet::class,
44 'FlashMessenger' => Plugin\FlashMessenger::class,
45 'flashMessenger' => Plugin\FlashMessenger::class,
46 'flashmessenger' => Plugin\FlashMessenger::class,
47 'Forward' => Plugin\Forward::class,
48 'forward' => Plugin\Forward::class,
49 'Identity' => Plugin\Identity::class,
50 'identity' => Plugin\Identity::class,
51 'Layout' => Plugin\Layout::class,
52 'layout' => Plugin\Layout::class,
53 'Params' => Plugin\Params::class,
54 'params' => Plugin\Params::class,
55 'PostRedirectGet' => Plugin\PostRedirectGet::class,
56 'postRedirectGet' => Plugin\PostRedirectGet::class,
57 'postredirectget' => Plugin\PostRedirectGet::class,
58 'prg' => Plugin\PostRedirectGet::class,
59 'Redirect' => Plugin\Redirect::class,
60 'redirect' => Plugin\Redirect::class,
61 'Url' => Plugin\Url::class,
62 'url' => Plugin\Url::class,
63 'CreateHttpNotFoundModel' => Plugin\CreateHttpNotFoundModel::class,
64 'createHttpNotFoundModel' => Plugin\CreateHttpNotFoundModel::class,
65 'createhttpnotfoundmodel' => Plugin\CreateHttpNotFoundModel::class,
66 'CreateConsoleNotFoundModel' => Plugin\CreateConsoleNotFoundModel::class,
67 'createConsoleNotFoundModel' => Plugin\CreateConsoleNotFoundModel::class,
68 'createconsolenotfoundmodel' => Plugin\CreateConsoleNotFoundModel::class,
69 ];
70
71 /**
72 * @var string[]|callable[] Default factories
73 */
74 protected $factories = [
75 Plugin\Forward::class => Plugin\Service\ForwardFactory::class,
76 Plugin\Identity::class => Plugin\Service\IdentityFactory::class,
77 Plugin\AcceptableViewModelSelector::class => InvokableFactory::class,
78 Plugin\FilePostRedirectGet::class => InvokableFactory::class,
79 Plugin\FlashMessenger::class => InvokableFactory::class,
80 Plugin\Layout::class => InvokableFactory::class,
81 Plugin\Params::class => InvokableFactory::class,
82 Plugin\PostRedirectGet::class => InvokableFactory::class,
83 Plugin\Redirect::class => InvokableFactory::class,
84 Plugin\Url::class => InvokableFactory::class,
85 Plugin\CreateHttpNotFoundModel::class => InvokableFactory::class,
86 Plugin\CreateConsoleNotFoundModel::class => InvokableFactory::class,
87
88 // v2 normalized names
89
90 'zendmvccontrollerpluginforward' => Plugin\Service\ForwardFactory::class,
91 'zendmvccontrollerpluginidentity' => Plugin\Service\IdentityFactory::class,
92 'zendmvccontrollerpluginacceptableviewmodelselector' => InvokableFactory::class,
93 'zendmvccontrollerpluginfilepostredirectget' => InvokableFactory::class,
94 'zendmvccontrollerpluginflashmessenger' => InvokableFactory::class,
95 'zendmvccontrollerpluginlayout' => InvokableFactory::class,
96 'zendmvccontrollerpluginparams' => InvokableFactory::class,
97 'zendmvccontrollerpluginpostredirectget' => InvokableFactory::class,
98 'zendmvccontrollerpluginredirect' => InvokableFactory::class,
99 'zendmvccontrollerpluginurl' => InvokableFactory::class,
100 'zendmvccontrollerplugincreatehttpnotfoundmodel' => InvokableFactory::class,
101 'zendmvccontrollerplugincreateconsolenotfoundmodel' => InvokableFactory::class,
102 ];
103
104 /**
105 * @var DispatchableInterface
106 */
107 protected $controller;
108
109 /**
110 * Retrieve a registered instance
111 *
112 * After the plugin is retrieved from the service locator, inject the
113 * controller in the plugin every time it is requested. This is required
114 * because a controller can use a plugin and another controller can be
115 * dispatched afterwards. If this second controller uses the same plugin
116 * as the first controller, the reference to the controller inside the
117 * plugin is lost.
118 *
119 * @param string $name
120 * @return DispatchableInterface
121 */
122 public function get($name, array $options = null)
123 {
124 $plugin = parent::get($name, $options);
125 $this->injectController($plugin);
126
127 return $plugin;
128 }
129
130 /**
131 * Set controller
132 *
133 * @param DispatchableInterface $controller
134 * @return PluginManager
135 */
136 public function setController(DispatchableInterface $controller)
137 {
138 $this->controller = $controller;
139
140 return $this;
141 }
142
143 /**
144 * Retrieve controller instance
145 *
146 * @return null|DispatchableInterface
147 */
148 public function getController()
149 {
150 return $this->controller;
151 }
152
153 /**
154 * Inject a helper instance with the registered controller
155 *
156 * @param object $plugin
157 * @return void
158 */
159 public function injectController($plugin)
160 {
161 if (!is_object($plugin)) {
162 return;
163 }
164 if (!method_exists($plugin, 'setController')) {
165 return;
166 }
167
168 $controller = $this->getController();
169 if (!$controller instanceof DispatchableInterface) {
170 return;
171 }
172
173 $plugin->setController($controller);
174 }
175
176 /**
177 * Validate a plugin (v3)
178 *
179 * {@inheritDoc}
180 */
181 public function validate($plugin)
182 {
183 if (! $plugin instanceof $this->instanceOf) {
184 throw new InvalidServiceException(sprintf(
185 'Plugin of type "%s" is invalid; must implement %s',
186 (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
187 $this->instanceOf
188 ));
189 }
190 }
191
192 /**
193 * Validate a plugin (v2)
194 *
195 * {@inheritDoc}
196 *
197 * @throws Exception\InvalidPluginException
198 */
199 public function validatePlugin($plugin)
200 {
201 try {
202 $this->validate($plugin);
203 } catch (InvalidServiceException $e) {
204 throw new Exception\InvalidPluginException(
205 $e->getMessage(),
206 $e->getCode(),
207 $e
208 );
209 }
210 }
211 }