use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Controller / AbstractController.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\EventManager\EventInterface as Event;
13 use Zend\EventManager\EventManager;
14 use Zend\EventManager\EventManagerAwareInterface;
15 use Zend\EventManager\EventManagerInterface;
16 use Zend\Http\PhpEnvironment\Response as HttpResponse;
17 use Zend\Http\Request as HttpRequest;
18 use Zend\Mvc\InjectApplicationEventInterface;
19 use Zend\Mvc\MvcEvent;
20 use Zend\ServiceManager\ServiceLocatorInterface;
21 use Zend\ServiceManager\ServiceManager;
22 use Zend\Stdlib\DispatchableInterface as Dispatchable;
23 use Zend\Stdlib\RequestInterface as Request;
24 use Zend\Stdlib\ResponseInterface as Response;
25
26 /**
27 * Abstract controller
28 *
29 * Convenience methods for pre-built plugins (@see __call):
30 *
31 * @method \Zend\View\Model\ModelInterface acceptableViewModelSelector(array $matchAgainst = null, bool $returnDefault = true, \Zend\Http\Header\Accept\FieldValuePart\AbstractFieldValuePart $resultReference = null)
32 * @method bool|array|\Zend\Http\Response fileprg(\Zend\Form\FormInterface $form, $redirect = null, $redirectToUrl = false)
33 * @method bool|array|\Zend\Http\Response filePostRedirectGet(\Zend\Form\FormInterface $form, $redirect = null, $redirectToUrl = false)
34 * @method \Zend\Mvc\Controller\Plugin\FlashMessenger flashMessenger()
35 * @method \Zend\Mvc\Controller\Plugin\Forward forward()
36 * @method mixed|null identity()
37 * @method \Zend\Mvc\Controller\Plugin\Layout|\Zend\View\Model\ModelInterface layout(string $template = null)
38 * @method \Zend\Mvc\Controller\Plugin\Params|mixed params(string $param = null, mixed $default = null)
39 * @method \Zend\Http\Response|array prg(string $redirect = null, bool $redirectToUrl = false)
40 * @method \Zend\Http\Response|array postRedirectGet(string $redirect = null, bool $redirectToUrl = false)
41 * @method \Zend\Mvc\Controller\Plugin\Redirect redirect()
42 * @method \Zend\Mvc\Controller\Plugin\Url url()
43 * @method \Zend\View\Model\ConsoleModel createConsoleNotFoundModel()
44 * @method \Zend\View\Model\ViewModel createHttpNotFoundModel(Response $response)
45 */
46 abstract class AbstractController implements
47 Dispatchable,
48 EventManagerAwareInterface,
49 InjectApplicationEventInterface
50 {
51 /**
52 * @var PluginManager
53 */
54 protected $plugins;
55
56 /**
57 * @var Request
58 */
59 protected $request;
60
61 /**
62 * @var Response
63 */
64 protected $response;
65
66 /**
67 * @var ServiceLocatorInterface
68 */
69 protected $serviceLocator;
70
71 /**
72 * @var Event
73 */
74 protected $event;
75
76 /**
77 * @var EventManagerInterface
78 */
79 protected $events;
80
81 /**
82 * @var null|string|string[]
83 */
84 protected $eventIdentifier;
85
86 /**
87 * Execute the request
88 *
89 * @param MvcEvent $e
90 * @return mixed
91 */
92 abstract public function onDispatch(MvcEvent $e);
93
94 /**
95 * Dispatch a request
96 *
97 * @events dispatch.pre, dispatch.post
98 * @param Request $request
99 * @param null|Response $response
100 * @return Response|mixed
101 */
102 public function dispatch(Request $request, Response $response = null)
103 {
104 $this->request = $request;
105 if (!$response) {
106 $response = new HttpResponse();
107 }
108 $this->response = $response;
109
110 $e = $this->getEvent();
111 $e->setName(MvcEvent::EVENT_DISPATCH);
112 $e->setRequest($request);
113 $e->setResponse($response);
114 $e->setTarget($this);
115
116 $result = $this->getEventManager()->triggerEventUntil(function ($test) {
117 return ($test instanceof Response);
118 }, $e);
119
120 if ($result->stopped()) {
121 return $result->last();
122 }
123
124 return $e->getResult();
125 }
126
127 /**
128 * Get request object
129 *
130 * @return Request
131 */
132 public function getRequest()
133 {
134 if (!$this->request) {
135 $this->request = new HttpRequest();
136 }
137
138 return $this->request;
139 }
140
141 /**
142 * Get response object
143 *
144 * @return Response
145 */
146 public function getResponse()
147 {
148 if (!$this->response) {
149 $this->response = new HttpResponse();
150 }
151
152 return $this->response;
153 }
154
155 /**
156 * Set the event manager instance used by this context
157 *
158 * @param EventManagerInterface $events
159 * @return AbstractController
160 */
161 public function setEventManager(EventManagerInterface $events)
162 {
163 $className = get_class($this);
164
165 $nsPos = strpos($className, '\\') ?: 0;
166 $events->setIdentifiers(array_merge(
167 [
168 __CLASS__,
169 $className,
170 substr($className, 0, $nsPos)
171 ],
172 array_values(class_implements($className)),
173 (array) $this->eventIdentifier
174 ));
175
176 $this->events = $events;
177 $this->attachDefaultListeners();
178
179 return $this;
180 }
181
182 /**
183 * Retrieve the event manager
184 *
185 * Lazy-loads an EventManager instance if none registered.
186 *
187 * @return EventManagerInterface
188 */
189 public function getEventManager()
190 {
191 if (!$this->events) {
192 $this->setEventManager(new EventManager());
193 }
194
195 return $this->events;
196 }
197
198 /**
199 * Set an event to use during dispatch
200 *
201 * By default, will re-cast to MvcEvent if another event type is provided.
202 *
203 * @param Event $e
204 * @return void
205 */
206 public function setEvent(Event $e)
207 {
208 if (!$e instanceof MvcEvent) {
209 $eventParams = $e->getParams();
210 $e = new MvcEvent();
211 $e->setParams($eventParams);
212 unset($eventParams);
213 }
214 $this->event = $e;
215 }
216
217 /**
218 * Get the attached event
219 *
220 * Will create a new MvcEvent if none provided.
221 *
222 * @return MvcEvent
223 */
224 public function getEvent()
225 {
226 if (!$this->event) {
227 $this->setEvent(new MvcEvent());
228 }
229
230 return $this->event;
231 }
232
233 /**
234 * Set serviceManager instance
235 *
236 * @param ServiceLocatorInterface $serviceLocator
237 * @return void
238 */
239 public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
240 {
241 $this->serviceLocator = $serviceLocator;
242 }
243
244 /**
245 * Retrieve serviceManager instance
246 *
247 * @return ServiceLocatorInterface
248 */
249 public function getServiceLocator()
250 {
251 trigger_error(sprintf(
252 'You are retrieving the service locator from within the class %s. Please be aware that '
253 . 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
254 . 'with the ServiceLocatorAwareInitializer. You will need to update your class to accept '
255 . 'all dependencies at creation, either via constructor arguments or setters, and use '
256 . 'a factory to perform the injections.',
257 get_class($this)
258 ), E_USER_DEPRECATED);
259
260 return $this->serviceLocator;
261 }
262
263 /**
264 * Get plugin manager
265 *
266 * @return PluginManager
267 */
268 public function getPluginManager()
269 {
270 if (!$this->plugins) {
271 $this->setPluginManager(new PluginManager(new ServiceManager()));
272 }
273
274 $this->plugins->setController($this);
275 return $this->plugins;
276 }
277
278 /**
279 * Set plugin manager
280 *
281 * @param PluginManager $plugins
282 * @return AbstractController
283 */
284 public function setPluginManager(PluginManager $plugins)
285 {
286 $this->plugins = $plugins;
287 $this->plugins->setController($this);
288
289 return $this;
290 }
291
292 /**
293 * Get plugin instance
294 *
295 * @param string $name Name of plugin to return
296 * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
297 * @return mixed
298 */
299 public function plugin($name, array $options = null)
300 {
301 return $this->getPluginManager()->get($name, $options);
302 }
303
304 /**
305 * Method overloading: return/call plugins
306 *
307 * If the plugin is a functor, call it, passing the parameters provided.
308 * Otherwise, return the plugin instance.
309 *
310 * @param string $method
311 * @param array $params
312 * @return mixed
313 */
314 public function __call($method, $params)
315 {
316 $plugin = $this->plugin($method);
317 if (is_callable($plugin)) {
318 return call_user_func_array($plugin, $params);
319 }
320
321 return $plugin;
322 }
323
324 /**
325 * Register the default events for this controller
326 *
327 * @return void
328 */
329 protected function attachDefaultListeners()
330 {
331 $events = $this->getEventManager();
332 $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch']);
333 }
334
335 /**
336 * Transform an "action" token into a method name
337 *
338 * @param string $action
339 * @return string
340 */
341 public static function getMethodFromAction($action)
342 {
343 $method = str_replace(['.', '-', '_'], ' ', $action);
344 $method = ucwords($method);
345 $method = str_replace(' ', '', $method);
346 $method = lcfirst($method);
347 $method .= 'Action';
348
349 return $method;
350 }
351 }