3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Mvc\Controller
;
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
;
19 * Plugin manager implementation for controllers
21 * Registers a number of default plugins, and contains an initializer for
22 * injecting plugins with the current controller.
24 class PluginManager
extends AbstractPluginManager
27 * Plugins must be of this type.
31 protected $instanceOf = Plugin\PluginInterface
::class;
34 * @var string[] Default aliases
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,
72 * @var string[]|callable[] Default factories
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,
88 // v2 normalized names
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,
105 * @var DispatchableInterface
107 protected $controller;
110 * Retrieve a registered instance
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
119 * @param string $name
120 * @return DispatchableInterface
122 public function get($name, array $options = null)
124 $plugin = parent
::get($name, $options);
125 $this->injectController($plugin);
133 * @param DispatchableInterface $controller
134 * @return PluginManager
136 public function setController(DispatchableInterface
$controller)
138 $this->controller
= $controller;
144 * Retrieve controller instance
146 * @return null|DispatchableInterface
148 public function getController()
150 return $this->controller
;
154 * Inject a helper instance with the registered controller
156 * @param object $plugin
159 public function injectController($plugin)
161 if (!is_object($plugin)) {
164 if (!method_exists($plugin, 'setController')) {
168 $controller = $this->getController();
169 if (!$controller instanceof DispatchableInterface
) {
173 $plugin->setController($controller);
177 * Validate a plugin (v3)
181 public function validate($plugin)
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)),
193 * Validate a plugin (v2)
197 * @throws Exception\InvalidPluginException
199 public function validatePlugin($plugin)
202 $this->validate($plugin);
203 } catch (InvalidServiceException
$e) {
204 throw new Exception\
InvalidPluginException(