Skip to content

Commit

Permalink
Updated Routing folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed May 25, 2012
1 parent 43fd0fb commit 2bec0e9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 92 deletions.
85 changes: 42 additions & 43 deletions lib/Cake/Routing/Dispatcher.php
Expand Up @@ -20,16 +20,19 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('Router', 'Routing');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('Controller', 'Controller');
App::uses('Scaffold', 'Controller');
App::uses('View', 'View');
App::uses('Debugger', 'Utility');
App::uses('CakeEvent', 'Event');
App::uses('CakeEventManager', 'Event');
App::uses('CakeEventListener', 'Event');
namespace Cake\Routing;
use Cake\Core\Configure,
Cake\Core\Plugin,
Cake\Core\App,
Cake\Controller\Controller,
Cake\Event\Event,
Cake\Event\EventListener,
Cake\Event\EventManager,
Cake\Network\Request,
Cake\Network\Response,
Cake\Utility\Inflector,
Cake\View\View,
Cake\Error;

/**
* Dispatcher converts Requests into controller actions. It uses the dispatched Request
Expand All @@ -38,7 +41,7 @@
*
* @package Cake.Routing
*/
class Dispatcher implements CakeEventListener {
class Dispatcher implements EventListener {

/**
* Event manager, used to handle dispatcher filters
Expand All @@ -59,14 +62,14 @@ public function __construct($base = false) {
}

/**
* Returns the CakeEventManager instance or creates one if none was
* Returns the Cake\Event\EventManager instance or creates one if none was
* creted. Attaches the default listeners and filters
*
* @return CakeEventmanger
* @return Cake\Event\EventManager
*/
public function getEventManager() {
if (!$this->_eventManager) {
$this->_eventManager = new CakeEventManager();
$this->_eventManager = new EventManager();
$this->_eventManager->attach($this);
$this->_attachFilters($this->_eventManager);
}
Expand All @@ -86,7 +89,7 @@ public function implementedEvents() {
* Attaches all event listeners for this dispatcher instance. Loads the
* dispatcher filters from the configured locations.
*
* @param CakeEventManager $manager
* @param Cake\Event\EventManager $manager
* @return void
* @throws MissingDispatcherFilterException
*/
Expand All @@ -101,10 +104,9 @@ protected function _attachFilters($manager) {
$filter = array('callable' => $filter);
}
if (is_string($filter['callable'])) {
list($plugin, $callable) = pluginSplit($filter['callable'], true);
App::uses($callable, $plugin . 'Routing/Filter');
if (!class_exists($callable)) {
throw new MissingDispatcherFilterException($callable);
$callable = App::classname($filter['callable'], 'Routing/Filter');
if (!$callable) {
throw new Error\MissingDispatcherFilterException($filter['callable']);
}
$manager->attach(new $callable);
} else {
Expand All @@ -130,18 +132,18 @@ protected function _attachFilters($manager) {
* If no controller of given name can be found, invoke() will throw an exception.
* If the controller is found, and the action is not found an exception will be thrown.
*
* @param CakeRequest $request Request object to dispatch.
* @param CakeResponse $response Response object to put the results of the dispatch into.
* @param Cake\Network\Request $request Request object to dispatch.
* @param Cake\Network\Response $response Response object to put the results of the dispatch into.
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
* @return string|void if `$request['return']` is set then it returns response body, null otherwise
* @throws MissingControllerException When the controller is missing.
*/
public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
$beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
public function dispatch(Request $request, Response $response, $additionalParams = array()) {
$beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
$this->getEventManager()->dispatch($beforeEvent);

$request = $beforeEvent->data['request'];
if ($beforeEvent->result instanceof CakeResponse) {
if ($beforeEvent->result instanceof Response) {
if (isset($request->params['return'])) {
return $response->body();
}
Expand All @@ -152,8 +154,8 @@ public function dispatch(CakeRequest $request, CakeResponse $response, $addition
$controller = $this->_getController($request, $response);

if (!($controller instanceof Controller)) {
throw new MissingControllerException(array(
'class' => Inflector::camelize($request->params['controller']) . 'Controller',
throw new Error\MissingControllerException(array(
'class' => Inflector::camelize($request->params['controller']),
'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
));
}
Expand All @@ -163,7 +165,7 @@ public function dispatch(CakeRequest $request, CakeResponse $response, $addition
return $response->body();
}

$afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
$afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('request', 'response'));
$this->getEventManager()->dispatch($afterEvent);
$afterEvent->data['response']->send();
}
Expand All @@ -174,17 +176,17 @@ public function dispatch(CakeRequest $request, CakeResponse $response, $addition
* Otherwise the return value of the controller action are returned.
*
* @param Controller $controller Controller to invoke
* @param CakeRequest $request The request object to invoke the controller for.
* @param CakeResponse $response The response object to receive the output
* @return CakeResponse te resulting response object
* @param Cake\Network\Request $request The request object to invoke the controller for.
* @param Cake\Network\Response $response The response object to receive the output
* @return Cake\Network\Response te resulting response object
*/
protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
protected function _invoke(Controller $controller, Request $request, Response $response) {
$controller->constructClasses();
$controller->startupProcess();

$render = true;
$result = $controller->invokeAction($request);
if ($result instanceof CakeResponse) {
if ($result instanceof Response) {
$render = false;
$response = $result;
}
Expand All @@ -203,7 +205,7 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes
* Applies Routing and additionalParameters to the request to be dispatched.
* If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
*
* @param CakeEvent $event containing the request, response and additional params
* @param Cake\Event\Event $event containing the request, response and additional params
* @return void
*/
public function parseParams($event) {
Expand All @@ -226,16 +228,16 @@ public function parseParams($event) {
/**
* Get controller to use, either plugin controller or application controller
*
* @param CakeRequest $request Request object
* @param CakeResponse $response Response for the controller.
* @param Cake\Network\Request $request Request object
* @param Cake\Network\Response $response Response for the controller.
* @return mixed name of controller if not loaded, or object if loaded
*/
protected function _getController($request, $response) {
$ctrlClass = $this->_loadController($request);
if (!$ctrlClass) {
return false;
}
$reflection = new ReflectionClass($ctrlClass);
$reflection = new \ReflectionClass($ctrlClass);
if ($reflection->isAbstract() || $reflection->isInterface()) {
return false;
}
Expand All @@ -245,7 +247,7 @@ protected function _getController($request, $response) {
/**
* Load controller and return controller classname
*
* @param CakeRequest $request
* @param Cake\Network\Request $request
* @return string|bool Name of controller class name
*/
protected function _loadController($request) {
Expand All @@ -258,12 +260,9 @@ protected function _loadController($request) {
$controller = Inflector::camelize($request->params['controller']);
}
if ($pluginPath . $controller) {
$class = $controller . 'Controller';
App::uses('AppController', 'Controller');
App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
App::uses($class, $pluginPath . 'Controller');
if (class_exists($class)) {
return $class;
$controller = App::classname($pluginPath . $controller, 'Controller', 'Controller');
if ($controller) {
return $controller;
}
}
return false;
Expand Down
13 changes: 7 additions & 6 deletions lib/Cake/Routing/DispatcherFilter.php
Expand Up @@ -16,7 +16,8 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeEventListener', 'Event');
namespace Cake\Routing;
use Cake\Event\EventListener;

/**
* This abstract class represents a filter to be applied to a dispatcher cycle. It acts as as
Expand All @@ -25,7 +26,7 @@
*
* @package Cake.Routing
*/
abstract class DispatcherFilter implements CakeEventListener {
abstract class DispatcherFilter implements EventListener {

/**
* Default priority for all methods in this filter
Expand Down Expand Up @@ -55,16 +56,16 @@ public function implementedEvents() {
* If used with default priority, it will be called after the Router has parsed the
* url and set the routing params into the request object.
*
* If a CakeResponse object instance is returned, it will be served at the end of the
* If a Cake\Network\Response object instance is returned, it will be served at the end of the
* event cycle, not calling any controller as a result. This will also have the effect of
* not calling the after event in the dispatcher.
*
* If false is returned, the event will be stopped and no more listeners will be notified.
* Alternatively you can call `$event->stopPropagation()` to acheive the same result.
*
* @param CakeEvent $event container object having the `request`, `response` and `additionalParams`
* @param Cake\Event\Event $event container object having the `request`, `response` and `additionalParams`
* keys in the data property.
* @return CakeResponse|boolean
* @return Cake\Network\Response|boolean
**/
public function beforeDispatch($event) {
}
Expand All @@ -77,7 +78,7 @@ public function beforeDispatch($event) {
* If false is returned, the event will be stopped and no more listeners will be notified.
* Alternatively you can call `$event->stopPropagation()` to acheive the same result.
*
* @param CakeEvent $event container object having the `request` and `response`
* @param Cake\Event\Event $event container object having the `request` and `response`
* keys in the data property.
* @return mixed boolean to stop the event dispatching or null to continue
**/
Expand Down
24 changes: 15 additions & 9 deletions lib/Cake/Routing/Filter/AssetDispatcher.php
Expand Up @@ -16,7 +16,13 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('DispatcherFilter', 'Routing');
namespace Cake\Routing\Filter;
use Cake\Routing\DispatcherFilter;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Network\Response;
use Cake\Utility\Inflector;

/**
* Filters a request and tests whether it is a file in the webroot folder or not and
Expand All @@ -37,8 +43,8 @@ class AssetDispatcher extends DispatcherFilter {
/**
* Checks if a requested asset exists and sends it to the browser
*
* @param CakeEvent $event containing the request and response object
* @return CakeResponse if the client is requesting a recognized asset, null otherwise
* @param Cake\Event\Event $event containing the request and response object
* @return Cake\Network\Response if the client is requesting a recognized asset, null otherwise
*/
public function beforeDispatch($event) {
$url = $event->data['request']->url;
Expand Down Expand Up @@ -68,10 +74,10 @@ public function beforeDispatch($event) {
}
} else {
$plugin = Inflector::camelize($parts[0]);
if (CakePlugin::loaded($plugin)) {
if (Plugin::loaded($plugin)) {
unset($parts[0]);
$fileFragment = urldecode(implode(DS, $parts));
$pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
$pluginWebroot = Plugin::path($plugin) . 'webroot' . DS;
if (file_exists($pluginWebroot . $fileFragment)) {
$assetFile = $pluginWebroot . $fileFragment;
}
Expand All @@ -92,8 +98,8 @@ public function beforeDispatch($event) {
* Checks if the client is requeting a filtered asset and runs the corresponding
* filter if any is configured
*
* @param CakeEvent $event containing the request and response object
* @return CakeResponse if the client is requesting a recognized asset, null otherwise
* @param Cake\Event\Event $event containing the request and response object
* @return Cake\Network\Response if the client is requesting a recognized asset, null otherwise
*/
protected function _filterAsset($event) {
$url = $event->data['request']->url;
Expand Down Expand Up @@ -123,12 +129,12 @@ protected function _filterAsset($event) {
/**
* Sends an asset file to the client
*
* @param CakeResponse $response The response object to use.
* @param Cake\Network\Response $response The response object to use.
* @param string $assetFile Path to the asset file in the file system
* @param string $ext The extension of the file to determine its mime type
* @return void
*/
protected function _deliverAsset(CakeResponse $response, $assetFile, $ext) {
protected function _deliverAsset(Response $response, $assetFile, $ext) {
ob_start();
$compressionEnabled = Configure::read('Asset.compress') && $response->compress();
if ($response->type($ext) == $ext) {
Expand Down
10 changes: 7 additions & 3 deletions lib/Cake/Routing/Filter/CacheDispatcher.php
Expand Up @@ -12,7 +12,11 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('DispatcherFilter', 'Routing');
namespace Cake\Routing\Filter;
use Cake\Routing\DispatcherFilter;
use Cake\Core\Configure;
use Cake\Utility\Inflector;
use Cake\View\View;

/**
* This filter will check wheter the response was previously cached in the file system
Expand All @@ -33,8 +37,8 @@ class CacheDispatcher extends DispatcherFilter {
/**
* Checks whether the response was cached and set the body accordingly.
*
* @param CakeEvent $event containing the request and response object
* @return CakeResponse with cached content if found, null otherwise
* @param Cake\Event\Event $event containing the request and response object
* @return Cake\NetworkResponse with cached content if found, null otherwise
*/
public function beforeDispatch($event) {
if (Configure::read('Cache.check') !== true) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Routing/Route/PluginShortRoute.php
Expand Up @@ -11,16 +11,17 @@
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Routing\Route;

App::uses('CakeRoute', 'Routing/Route');
use Cake\Routing\Route\Route;

/**
* Plugin short route, that copies the plugin param to the controller parameters
* It is used for supporting /:plugin routes.
*
* @package Cake.Routing.Route
*/
class PluginShortRoute extends CakeRoute {
class PluginShortRoute extends Route {

/**
* Parses a string url into an array. If a plugin key is found, it will be copied to the
Expand Down
14 changes: 8 additions & 6 deletions lib/Cake/Routing/Route/RedirectRoute.php
Expand Up @@ -12,9 +12,11 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Routing\Route;

App::uses('CakeResponse', 'Network');
App::uses('CakeRoute', 'Routing/Route');
use Cake\Routing\Router,
Cake\Routing\Route\Route,
Cake\Network\Response;

/**
* Redirect route will perform an immediate redirect. Redirect routes
Expand All @@ -23,12 +25,12 @@
*
* @package Cake.Routing.Route
*/
class RedirectRoute extends CakeRoute {
class RedirectRoute extends Route {

/**
* A CakeResponse object
* A Response object
*
* @var CakeResponse
* @var Cake\Network\Response
*/
public $response = null;

Expand Down Expand Up @@ -71,7 +73,7 @@ public function parse($url) {
return false;
}
if (!$this->response) {
$this->response = new CakeResponse();
$this->response = new Response();
}
$redirect = $this->redirect;
if (count($this->redirect) == 1 && !isset($this->redirect['controller'])) {
Expand Down

0 comments on commit 2bec0e9

Please sign in to comment.