This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
PluginManager.php
163 lines (149 loc) · 4.53 KB
/
PluginManager.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/
namespace Zend\Mvc\Controller;
use Zend\Mvc\Exception;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\Stdlib\DispatchableInterface;
/**
* Plugin manager implementation for controllers
*
* Registers a number of default plugins, and contains an initializer for
* injecting plugins with the current controller.
*
* @category Zend
* @package Zend_Mvc
* @subpackage Controller
*/
class PluginManager extends AbstractPluginManager
{
/**
* Default set of plugins
*
* @var array
*/
protected $invokableClasses = array(
'acceptedmodel' => 'Zend\Mvc\Controller\Plugin\AcceptedModel',
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
'forward' => 'Zend\Mvc\Controller\Plugin\Forward',
'layout' => 'Zend\Mvc\Controller\Plugin\Layout',
'params' => 'Zend\Mvc\Controller\Plugin\Params',
'postredirectget' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet',
'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect',
'url' => 'Zend\Mvc\Controller\Plugin\Url',
);
/**
* Default set of plugin aliases
*
* @var array
*/
protected $aliases = array(
'prg' => 'postredirectget',
);
/**
* @var DispatchableInterface
*/
protected $controller;
/**
* Constructor
*
* After invoking parent constructor, add an initializer to inject the
* attached controller, if any, to the currently requested plugin.
*
* @param null|ConfigInterface $configuration
*/
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
$this->addInitializer(array($this, 'injectController'));
}
/**
* Retrieve a registered instance
*
* After the plugin is retrieved from the service locator, inject the
* controller in the plugin every time it is requested. This is required
* because a controller can use a plugin and another controller can be
* dispatched afterwards. If this second controller uses the same plugin
* as the first controller, the reference to the controller inside the
* plugin is lost.
*
* @param string $name
* @param mixed $options
* @param bool $usePeeringServiceManagers
* @return mixed
*/
public function get($name, $options = array(), $usePeeringServiceManagers = true)
{
$plugin = parent::get($name, $options, $usePeeringServiceManagers);
$this->injectController($plugin);
return $plugin;
}
/**
* Set controller
*
* @param DispatchableInterface $controller
* @return PluginManager
*/
public function setController(DispatchableInterface $controller)
{
$this->controller = $controller;
return $this;
}
/**
* Retrieve controller instance
*
* @return null|DispatchableInterface
*/
public function getController()
{
return $this->controller;
}
/**
* Inject a helper instance with the registered controller
*
* @param object $plugin
* @return void
*/
public function injectController($plugin)
{
if (!is_object($plugin)) {
return;
}
if (!method_exists($plugin, 'setController')) {
return;
}
$controller = $this->getController();
if (!$controller instanceof DispatchableInterface) {
return;
}
$plugin->setController($controller);
}
/**
* Validate the plugin
*
* Any plugin is considered valid in this context.
*
* @param mixed $plugin
* @return void
* @throws Exception\InvalidPluginException
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof Plugin\PluginInterface) {
// we're okay
return;
}
throw new Exception\InvalidPluginException(sprintf(
'Plugin of type %s is invalid; must implement %s\Plugin\PluginInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
}
}