Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Controller / AbstractController.php
blob41a3ce9e38f5dc2659a839c5a5bcfbe61f5323ff
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Mvc\Controller;
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\ServiceLocatorAwareInterface;
21 use Zend\ServiceManager\ServiceLocatorInterface;
22 use Zend\Stdlib\DispatchableInterface as Dispatchable;
23 use Zend\Stdlib\RequestInterface as Request;
24 use Zend\Stdlib\ResponseInterface as Response;
26 /**
27 * Abstract controller
29 * Convenience methods for pre-built plugins (@see __call):
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\Form $form, $redirect = null, $redirectToUrl = false)
33 * @method bool|array|\Zend\Http\Response filePostRedirectGet(\Zend\Form\Form $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()
44 abstract class AbstractController implements
45 Dispatchable,
46 EventManagerAwareInterface,
47 InjectApplicationEventInterface,
48 ServiceLocatorAwareInterface
50 /**
51 * @var PluginManager
53 protected $plugins;
55 /**
56 * @var Request
58 protected $request;
60 /**
61 * @var Response
63 protected $response;
65 /**
66 * @var Event
68 protected $event;
70 /**
71 * @var EventManagerInterface
73 protected $events;
75 /**
76 * @var ServiceLocatorInterface
78 protected $serviceLocator;
80 /**
81 * @var string
83 protected $eventIdentifier;
85 /**
86 * Execute the request
88 * @param MvcEvent $e
89 * @return mixed
91 abstract public function onDispatch(MvcEvent $e);
94 /**
95 * Dispatch a request
97 * @events dispatch.pre, dispatch.post
98 * @param Request $request
99 * @param null|Response $response
100 * @return Response|mixed
102 public function dispatch(Request $request, Response $response = null)
104 $this->request = $request;
105 if (!$response) {
106 $response = new HttpResponse();
108 $this->response = $response;
110 $e = $this->getEvent();
111 $e->setRequest($request)
112 ->setResponse($response)
113 ->setTarget($this);
115 $result = $this->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH, $e, function ($test) {
116 return ($test instanceof Response);
119 if ($result->stopped()) {
120 return $result->last();
123 return $e->getResult();
127 * Get request object
129 * @return Request
131 public function getRequest()
133 if (!$this->request) {
134 $this->request = new HttpRequest();
137 return $this->request;
141 * Get response object
143 * @return Response
145 public function getResponse()
147 if (!$this->response) {
148 $this->response = new HttpResponse();
151 return $this->response;
155 * Set the event manager instance used by this context
157 * @param EventManagerInterface $events
158 * @return AbstractController
160 public function setEventManager(EventManagerInterface $events)
162 $events->setIdentifiers(array(
163 'Zend\Stdlib\DispatchableInterface',
164 __CLASS__,
165 get_class($this),
166 $this->eventIdentifier,
167 substr(get_class($this), 0, strpos(get_class($this), '\\'))
169 $this->events = $events;
170 $this->attachDefaultListeners();
172 return $this;
176 * Retrieve the event manager
178 * Lazy-loads an EventManager instance if none registered.
180 * @return EventManagerInterface
182 public function getEventManager()
184 if (!$this->events) {
185 $this->setEventManager(new EventManager());
188 return $this->events;
192 * Set an event to use during dispatch
194 * By default, will re-cast to MvcEvent if another event type is provided.
196 * @param Event $e
197 * @return void
199 public function setEvent(Event $e)
201 if (!$e instanceof MvcEvent) {
202 $eventParams = $e->getParams();
203 $e = new MvcEvent();
204 $e->setParams($eventParams);
205 unset($eventParams);
207 $this->event = $e;
211 * Get the attached event
213 * Will create a new MvcEvent if none provided.
215 * @return MvcEvent
217 public function getEvent()
219 if (!$this->event) {
220 $this->setEvent(new MvcEvent());
223 return $this->event;
227 * Set serviceManager instance
229 * @param ServiceLocatorInterface $serviceLocator
230 * @return void
232 public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
234 $this->serviceLocator = $serviceLocator;
238 * Retrieve serviceManager instance
240 * @return ServiceLocatorInterface
242 public function getServiceLocator()
244 return $this->serviceLocator;
248 * Get plugin manager
250 * @return PluginManager
252 public function getPluginManager()
254 if (!$this->plugins) {
255 $this->setPluginManager(new PluginManager());
258 $this->plugins->setController($this);
259 return $this->plugins;
263 * Set plugin manager
265 * @param PluginManager $plugins
266 * @return AbstractController
268 public function setPluginManager(PluginManager $plugins)
270 $this->plugins = $plugins;
271 $this->plugins->setController($this);
273 return $this;
277 * Get plugin instance
279 * @param string $name Name of plugin to return
280 * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
281 * @return mixed
283 public function plugin($name, array $options = null)
285 return $this->getPluginManager()->get($name, $options);
289 * Method overloading: return/call plugins
291 * If the plugin is a functor, call it, passing the parameters provided.
292 * Otherwise, return the plugin instance.
294 * @param string $method
295 * @param array $params
296 * @return mixed
298 public function __call($method, $params)
300 $plugin = $this->plugin($method);
301 if (is_callable($plugin)) {
302 return call_user_func_array($plugin, $params);
305 return $plugin;
309 * Register the default events for this controller
311 * @return void
313 protected function attachDefaultListeners()
315 $events = $this->getEventManager();
316 $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
320 * Transform an "action" token into a method name
322 * @param string $action
323 * @return string
325 public static function getMethodFromAction($action)
327 $method = str_replace(array('.', '-', '_'), ' ', $action);
328 $method = ucwords($method);
329 $method = str_replace(' ', '', $method);
330 $method = lcfirst($method);
331 $method .= 'Action';
333 return $method;