Source of file AbstractController.php
Size: 8,680 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Mvc/Controller/AbstractController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Mvc\Controller; use Zend\EventManager\EventInterface as Event; use Zend\EventManager\EventManager; use Zend\EventManager\EventManagerAwareInterface; use Zend\EventManager\EventManagerInterface; use Zend\Http\PhpEnvironment\Response as HttpResponse; use Zend\Http\Request as HttpRequest; use Zend\Mvc\InjectApplicationEventInterface; use Zend\Mvc\MvcEvent; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Stdlib\DispatchableInterface as Dispatchable; use Zend\Stdlib\RequestInterface as Request; use Zend\Stdlib\ResponseInterface as Response; /** * Abstract controller * * Convenience methods for pre-built plugins (@see __call): * * @method \Zend\View\Model\ModelInterface acceptableViewModelSelector(array $matchAgainst = null, bool $returnDefault = true, \Zend\Http\Header\Accept\FieldValuePart\AbstractFieldValuePart $resultReference = null) * @method bool|array|\Zend\Http\Response fileprg(\Zend\Form\Form $form, $redirect = null, $redirectToUrl = false) * @method bool|array|\Zend\Http\Response filePostRedirectGet(\Zend\Form\Form $form, $redirect = null, $redirectToUrl = false) * @method \Zend\Mvc\Controller\Plugin\FlashMessenger flashMessenger() * @method \Zend\Mvc\Controller\Plugin\Forward forward() * @method mixed|null identity() * @method \Zend\Mvc\Controller\Plugin\Layout|\Zend\View\Model\ModelInterface layout(string $template = null) * @method \Zend\Mvc\Controller\Plugin\Params|mixed params(string $param = null, mixed $default = null) * @method \Zend\Http\Response|array prg(string $redirect = null, bool $redirectToUrl = false) * @method \Zend\Http\Response|array postRedirectGet(string $redirect = null, bool $redirectToUrl = false) * @method \Zend\Mvc\Controller\Plugin\Redirect redirect() * @method \Zend\Mvc\Controller\Plugin\Url url() */ abstract class AbstractController implements Dispatchable, EventManagerAwareInterface, InjectApplicationEventInterface, ServiceLocatorAwareInterface { /** * @var PluginManager */ protected $plugins; /** * @var Request */ protected $request; /** * @var Response */ protected $response; /** * @var Event */ protected $event; /** * @var EventManagerInterface */ protected $events; /** * @var ServiceLocatorInterface */ protected $serviceLocator; /** * @var string */ protected $eventIdentifier; /** * Execute the request * * @param MvcEvent $e * @return mixed */ abstract public function onDispatch(MvcEvent $e); /** * Dispatch a request * * @events dispatch.pre, dispatch.post * @param Request $request * @param null|Response $response * @return Response|mixed */ public function dispatch(Request $request, Response $response = null) { $this->request = $request; if (!$response) { $response = new HttpResponse(); } $this->response = $response; $e = $this->getEvent(); $e->setRequest($request) ->setResponse($response) ->setTarget($this); $result = $this->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH, $e, function ($test) { return ($test instanceof Response); }); if ($result->stopped()) { return $result->last(); } return $e->getResult(); } /** * Get request object * * @return Request */ public function getRequest() { if (!$this->request) { $this->request = new HttpRequest(); } return $this->request; } /** * Get response object * * @return Response */ public function getResponse() { if (!$this->response) { $this->response = new HttpResponse(); } return $this->response; } /** * Set the event manager instance used by this context * * @param EventManagerInterface $events * @return AbstractController */ public function setEventManager(EventManagerInterface $events) { $events->setIdentifiers(array( 'Zend\Stdlib\DispatchableInterface', __CLASS__, get_class($this), $this->eventIdentifier, substr(get_class($this), 0, strpos(get_class($this), '\\')) )); $this->events = $events; $this->attachDefaultListeners(); return $this; } /** * Retrieve the event manager * * Lazy-loads an EventManager instance if none registered. * * @return EventManagerInterface */ public function getEventManager() { if (!$this->events) { $this->setEventManager(new EventManager()); } return $this->events; } /** * Set an event to use during dispatch * * By default, will re-cast to MvcEvent if another event type is provided. * * @param Event $e * @return void */ public function setEvent(Event $e) { if (!$e instanceof MvcEvent) { $eventParams = $e->getParams(); $e = new MvcEvent(); $e->setParams($eventParams); unset($eventParams); } $this->event = $e; } /** * Get the attached event * * Will create a new MvcEvent if none provided. * * @return MvcEvent */ public function getEvent() { if (!$this->event) { $this->setEvent(new MvcEvent()); } return $this->event; } /** * Set serviceManager instance * * @param ServiceLocatorInterface $serviceLocator * @return void */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; } /** * Retrieve serviceManager instance * * @return ServiceLocatorInterface */ public function getServiceLocator() { return $this->serviceLocator; } /** * Get plugin manager * * @return PluginManager */ public function getPluginManager() { if (!$this->plugins) { $this->setPluginManager(new PluginManager()); } $this->plugins->setController($this); return $this->plugins; } /** * Set plugin manager * * @param PluginManager $plugins * @return AbstractController */ public function setPluginManager(PluginManager $plugins) { $this->plugins = $plugins; $this->plugins->setController($this); return $this; } /** * Get plugin instance * * @param string $name Name of plugin to return * @param null|array $options Options to pass to plugin constructor (if not already instantiated) * @return mixed */ public function plugin($name, array $options = null) { return $this->getPluginManager()->get($name, $options); } /** * Method overloading: return/call plugins * * If the plugin is a functor, call it, passing the parameters provided. * Otherwise, return the plugin instance. * * @param string $method * @param array $params * @return mixed */ public function __call($method, $params) { $plugin = $this->plugin($method); if (is_callable($plugin)) { return call_user_func_array($plugin, $params); } return $plugin; } /** * Register the default events for this controller * * @return void */ protected function attachDefaultListeners() { $events = $this->getEventManager(); $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch')); } /** * Transform an "action" token into a method name * * @param string $action * @return string */ public static function getMethodFromAction($action) { $method = str_replace(array('.', '-', '_'), ' ', $action); $method = ucwords($method); $method = str_replace(' ', '', $method); $method = lcfirst($method); $method .= 'Action'; return $method; } } |