/library/Zend/Mvc/Controller/ActionController.php
https://github.com/MontmereLimited/zf2 · PHP · 325 lines · 174 code · 31 blank · 120 comment · 13 complexity · 9c318861a9f99ab59cf4d577f657ab3a MD5 · raw file
- <?php
- namespace Zend\Mvc\Controller;
- use Zend\Di\Locator,
- Zend\EventManager\EventCollection,
- Zend\EventManager\EventDescription as Event,
- Zend\EventManager\EventManager,
- Zend\Http\PhpEnvironment\Response as HttpResponse,
- Zend\Loader\Broker,
- Zend\Loader\Pluggable,
- Zend\Mvc\Exception,
- Zend\Mvc\InjectApplicationEvent,
- Zend\Mvc\LocatorAware,
- Zend\Mvc\MvcEvent,
- Zend\Stdlib\Dispatchable,
- Zend\Stdlib\RequestDescription as Request,
- Zend\Stdlib\ResponseDescription as Response,
- Zend\View\Model\ViewModel;
- /**
- * Basic action controller
- */
- abstract class ActionController implements Dispatchable, InjectApplicationEvent, LocatorAware, Pluggable
- {
- //use \Zend\EventManager\ProvidesEvents;
- protected $broker;
- protected $event;
- protected $events;
- protected $locator;
- protected $request;
- protected $response;
- /**
- * Default action if none provided
- *
- * @return array
- */
- public function indexAction()
- {
- return new ViewModel(array(
- 'content' => 'Placeholder page'
- ));
- }
- /**
- * Action called if matched action does not exist
- *
- * @return array
- */
- public function notFoundAction()
- {
- $response = $this->response;
- $event = $this->getEvent();
- $routeMatch = $event->getRouteMatch();
- $response->setStatusCode(404);
- $routeMatch->setParam('action', 'not-found');
- return new ViewModel(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', 'not-found');
- $method = static::getMethodFromAction($action);
- if (!method_exists($this, $method)) {
- $method = 'notFoundAction';
- }
- $actionResponse = $this->$method();
- $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/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, array $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->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;
- }
- }