refactor dispatcher, fixes #5033

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7365 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-07-26 20:09:20 +00:00
parent caa7bb6218
commit e26b7e7a63
3 changed files with 108 additions and 67 deletions

View file

@ -161,20 +161,19 @@ class Dispatcher extends Object {
$privateAction = in_array($prefix, $prefixes);
}
}
$protected = array_map('strtolower', get_class_methods('controller'));
$classMethods = array_map('strtolower', get_class_methods($controller));
if (in_array(strtolower($this->params['action']), $protected) || strpos($this->params['action'], '_', 0) === 0) {
$privateAction = true;
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot)));
if ($privateAction || strpos($this->params['action'], '_', 0) === 0) {
return $this->cakeError('privateAction', array(
array(
'className' => Inflector::camelize($this->params['controller']."Controller"),
'action' => $this->params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
}
if (!in_array(strtolower($this->params['action']), $classMethods)) {
$missingAction = true;
}
if (array_key_exists('return', $this->params) && $this->params['return'] == 1) {
$controller->autoRender = false;
}
$controller->base = $this->base;
$controller->here = $this->here;
$controller->webroot = $this->webroot;
@ -189,12 +188,16 @@ class Dispatcher extends Object {
$controller->data = null;
}
if (array_key_exists('return', $this->params) && $this->params['return'] == 1) {
$controller->autoRender = false;
}
if (!empty($this->params['bare'])) {
$controller->autoLayout = false;
}
if (isset($this->params['layout'])) {
if ($this->params['layout'] === '') {
if (array_key_exists('layout', $this->params)) {
if (empty($this->params['layout'])) {
$controller->autoLayout = false;
} else {
$controller->layout = $this->params['layout'];
@ -205,28 +208,6 @@ class Dispatcher extends Object {
$controller->viewPath = $this->params['viewPath'];
}
foreach (array('components', 'helpers') as $var) {
if (isset($this->params[$var]) && !empty($this->params[$var]) && is_array($controller->{$var})) {
$diff = array_diff($this->params[$var], $controller->{$var});
$controller->{$var} = array_merge($controller->{$var}, $diff);
}
}
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot)));
$controller->constructClasses();
if ($privateAction) {
return $this->cakeError('privateAction', array(
array(
'className' => Inflector::camelize($this->params['controller']."Controller"),
'action' => $this->params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
}
$controller->Component->initialize($controller);
$controller->beforeFilter();
$controller->Component->startup($controller);
return $this->_invoke($controller, $this->params, $missingAction);
}
/**
@ -240,11 +221,21 @@ class Dispatcher extends Object {
* @access protected
*/
function _invoke(&$controller, $params, $missingAction = false) {
$classVars = get_object_vars($controller);
if ($missingAction && array_key_exists('scaffold', $classVars)) {
App::import('Core', 'Scaffold');
return new Scaffold($controller, $params);
} elseif ($missingAction && !array_key_exists('scaffold', $classVars)) {
$controller->constructClasses();
$controller->Component->initialize($controller);
$controller->beforeFilter();
$controller->Component->startup($controller);
$classMethods = array_diff(
array_map('strtolower', get_class_methods($controller)),
array_map('strtolower', get_class_methods('controller'))
);
if (!in_array(strtolower($params['action']), $classMethods)) {
if ($controller->scaffold !== false) {
App::import('Core', 'Scaffold');
return new Scaffold($controller, $params);
}
return $this->cakeError('missingAction', array(
array(
'className' => Inflector::camelize($params['controller']."Controller"),
@ -252,10 +243,11 @@ class Dispatcher extends Object {
'webroot' => $this->webroot,
'url' => $this->here,
'base' => $this->base)));
} else {
$output = $controller->dispatchMethod($params['action'], $params['pass']);
}
$output = $controller->dispatchMethod($params['action'], $params['pass']);
if ($controller->autoRender) {
$controller->output = $controller->render();
} elseif (empty($controller->output)) {
@ -277,7 +269,7 @@ class Dispatcher extends Object {
* @return null
* @access private
* @todo commented Router::url(). this improved performance,
* will work on this more later.
* will work on this more later.
*/
function __extractParams($url, $additionalParams = array()) {
$defaults = array('pass' => array(), 'named' => array(), 'form' => array());
@ -674,7 +666,11 @@ class Dispatcher extends Object {
header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
header("Cache-Control: cache");
header("Pragma: cache");
readfile($assetFile);
if ($type === 'css' || $type === 'js') {
include($assetFile);
} else {
readfile($assetFile);
}
if(Configure::read('Asset.compress')) {
header("Content-length: " . ob_get_length());
@ -703,7 +699,7 @@ class Dispatcher extends Object {
App::import('Core', 'View');
}
$controller = null;
$view = new View($controller, false);
$view =& new View($controller, false);
return $view->renderCache($filename, getMicrotime());
}
}

View file

@ -270,6 +270,13 @@ class Controller extends Object {
* @access public
*/
var $passedArgs = array();
/**
* Triggers Scaffolding
*
* @var mixed
* @access public
*/
var $scaffold = false;
/**
* Constructor.
*

View file

@ -45,22 +45,14 @@ class TestDispatcher extends Dispatcher {
* @return void
*/
function _invoke(&$controller, $params, $missingAction) {
$controller->params =& $params;
$classVars = get_object_vars($controller);
if ($missingAction && array_key_exists('scaffold', $classVars)) {
uses('controller'. DS . 'scaffold');
return new Scaffold($controller, $params);
} elseif ($missingAction && !array_key_exists('scaffold', $classVars)) {
return $this->cakeError('missingAction', array(
array(
'className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $this->here,
'base' => $this->base
)
));
restore_error_handler();
if ($result = parent::_invoke($controller, $params, $missingAction)) {
if ($result === 'missingAction') {
return $result;
}
}
set_error_handler('simpleTestErrorHandler');
return $controller;
}
/**
@ -183,6 +175,25 @@ class SomePagesController extends AppController {
function index() {
return true;
}
/**
* protected method
*
* @access protected
* @return void
*/
function _protected() {
return true;
}
/**
* redirect method overriding
*
* @access public
* @return void
*/
function redirect() {
echo 'this should not be accessible';
}
}
/**
* OtherPagesController class
@ -331,7 +342,11 @@ class SomePostsController extends AppController {
* @return void
*/
function beforeFilter() {
$this->params['action'] = 'view';
if ($this->params['action'] == 'index') {
$this->params['action'] = 'view';
} else {
$this->params['action'] = 'change';
}
$this->params['pass'] = array('changed');
}
/**
@ -343,6 +358,15 @@ class SomePostsController extends AppController {
function index() {
return true;
}
/**
* change method
*
* @access public
* @return void
*/
function change() {
return true;
}
}
/**
* TestCachedPagesController class
@ -947,7 +971,7 @@ class DispatcherTest extends CakeTestCase {
function testPrivate() {
$Dispatcher =& new TestDispatcher();
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/redirect/param:value/param2:value2';
$url = 'some_pages/_protected/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$expected = 'privateAction';
@ -967,6 +991,14 @@ class DispatcherTest extends CakeTestCase {
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$expected = 'missingAction';
$this->assertEqual($expected, $controller);
$Dispatcher =& new TestDispatcher();
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/redirect/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return'=> 1));
$expected = 'missingAction';
$this->assertEqual($expected, $controller);
}
/**
* testDispatch method
@ -1000,8 +1032,8 @@ class DispatcherTest extends CakeTestCase {
$expected = 'Pages';
$this->assertEqual($expected, $controller->name);
unset($Dispatcher);
$Dispatcher =& new TestDispatcher();
Configure::write('App.baseUrl','/timesheets/index.php');
@ -1260,7 +1292,7 @@ class DispatcherTest extends CakeTestCase {
$this->assertTrue(class_exists('TestPluginAppController'));
$this->assertTrue(class_exists('OtherComponentComponent'));
$this->assertTrue(class_exists('PluginsComponentComponent'));
Configure::write('pluginPaths', $_back);
}
/**
@ -1274,10 +1306,16 @@ class DispatcherTest extends CakeTestCase {
$url = 'some_posts/index/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$expected = 'missingAction';
$this->assertEqual($expected, $controller);
$url = 'some_posts/something_else/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$expected = 'SomePosts';
$this->assertEqual($expected, $controller->name);
$expected = 'view';
$expected = 'change';
$this->assertEqual($expected, $controller->action);
$expected = array('changed');
@ -1305,8 +1343,8 @@ class DispatcherTest extends CakeTestCase {
$result = ob_get_clean();
$file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'img' . DS . 'test.jpg');
$this->assertEqual($file, $result);
Configure::write('debug', 0);
$Dispatcher->params = $Dispatcher->parseParams('css/test_asset.css');