/library/Zend/Mvc/Controller/ActionController.php
https://github.com/kazaff/zf2 · PHP · 316 lines · 171 code · 29 blank · 116 comment · 15 complexity · d5c98ec63b967bb511ecc5c806201821 MD5 · raw file
- <?php
- namespace Zend\Mvc\Controller;
- use ArrayObject,
- Zend\Di\Locator,
- Zend\EventManager\EventCollection,
- Zend\EventManager\EventDescription as Event,
- Zend\EventManager\EventManager,
- Zend\Http\Response as HttpResponse,
- Zend\Loader\Broker,
- Zend\Loader\Pluggable,
- Zend\Stdlib\Dispatchable,
- Zend\Stdlib\IsAssocArray,
- Zend\Stdlib\RequestDescription as Request,
- Zend\Stdlib\ResponseDescription as Response,
- Zend\Mvc\InjectApplicationEvent,
- Zend\Mvc\LocatorAware,
- Zend\Mvc\MvcEvent;
- /**
- * Basic action controller
- */
- abstract class ActionController implements Dispatchable, InjectApplicationEvent, LocatorAware, Pluggable
- {
- protected $broker;
- protected $event;
- protected $events;
- protected $locator;
- protected $request;
- protected $response;
- /**
- * Default action if none provided
- *
- * @return array
- */
- public function indexAction()
- {
- return array('content' => 'Placeholder page');
- }
- /**
- * Action called if matched action does not exist
- *
- * @return array
- */
- public function notFoundAction()
- {
- $this->response->setStatusCode(404);
- return array('content' => 'Page not found');
- }
- /**
- * 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->events()->trigger('dispatch', $e, function($test) {
- return ($test instanceof Response);
- });
- if ($result->stopped()) {
- return $result->last();
- }
- return $e->getResult();
- }
- /**
- * Execute the request
- *
- * @param MvcEvent $e
- * @return mixed
- */
- public function execute(MvcEvent $e)
- {
- $routeMatch = $e->getRouteMatch();
- if (!$routeMatch) {
- /**
- * @todo Determine requirements for when route match is missing.
- * Potentially allow pulling directly from request metadata?
- */
- throw new \DomainException('Missing route matches; unsure how to retrieve action');
- }
- $action = $routeMatch->getParam('action', 'index');
- $method = static::getMethodFromAction($action);
- if (!method_exists($this, $method)) {
- $method = 'notFoundAction';
- }
- $actionResponse = $this->$method();
- if (!is_object($actionResponse)) {
- if (IsAssocArray::test($actionResponse)) {
- $actionResponse = new ArrayObject($actionResponse, ArrayObject::ARRAY_AS_PROPS);
- }
- }
- $e->setResult($actionResponse);
- return $actionResponse;
- }
- /**
- * Get the request object
- *
- * @return Request
- */
- public function getRequest()
- {
- return $this->request;
- }
- /**
- * Get the response object
- *
- * @return Response
- */
- public function getResponse()
- {
- if (null === $this->response) {
- $this->response = new HttpResponse();
- }
- return $this->response;
- }
- /**
- * Set the event manager instance used by this context
- *
- * @param EventCollection $events
- * @return AppContext
- */
- public function setEventManager(EventCollection $events)
- {
- $this->events = $events;
- return $this;
- }
- /**
- * Retrieve the event manager
- *
- * Lazy-loads an EventManager instance if none registered.
- *
- * @return EventCollection
- */
- public function events()
- {
- if (!$this->events instanceof EventCollection) {
- $this->setEventManager(new EventManager(array(
- 'Zend\Stdlib\Dispatchable',
- __CLASS__,
- get_called_class()
- )));
- $this->attachDefaultListeners();
- }
- 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 Event && !$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 Event
- */
- public function getEvent()
- {
- if (!$this->event) {
- $this->setEvent(new MvcEvent());
- }
- return $this->event;
- }
- /**
- * Set locator instance
- *
- * @param Locator $locator
- * @return void
- */
- public function setLocator(Locator $locator)
- {
- $this->locator = $locator;
- }
- /**
- * Retrieve locator instance
- *
- * @return Locator
- */
- public function getLocator()
- {
- return $this->locator;
- }
- /**
- * Get plugin broker instance
- *
- * @return Zend\Loader\Broker
- */
- public function getBroker()
- {
- if (!$this->broker) {
- $this->setBroker(new PluginBroker());
- }
- return $this->broker;
- }
- /**
- * Set plugin broker instance
- *
- * @param string|Broker $broker Plugin broker to load plugins
- * @return Zend\Loader\Pluggable
- */
- public function setBroker($broker)
- {
- if (!$broker instanceof Broker) {
- throw new Exception\InvalidArgumentException('Broker must implement Zend\Loader\Broker');
- }
- $this->broker = $broker;
- if (method_exists($broker, 'setController')) {
- $this->broker->setController($this);
- }
- return $this;
- }
- /**
- * Get plugin instance
- *
- * @param string $plugin 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->getBroker()->load($name, $options);
- }
- /**
- * Method overloading: return plugins
- *
- * @param mixed $method
- * @param mixed $params
- * @return void
- */
- public function __call($method, $params)
- {
- $options = null;
- if (0 < count($params)) {
- $options = array_shift($params);
- }
- return $this->plugin($method, $options);
- }
- /**
- * Register the default events for this controller
- *
- * @return void
- */
- protected function attachDefaultListeners()
- {
- $events = $this->events();
- $events->attach('dispatch', array($this, 'execute'));
- }
- /**
- * Transform an action name 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;
- }
- }