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); $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) { Router::setRequestInfo(array($this->params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot)));
$privateAction = true;
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->base = $this->base;
$controller->here = $this->here; $controller->here = $this->here;
$controller->webroot = $this->webroot; $controller->webroot = $this->webroot;
@ -189,12 +188,16 @@ class Dispatcher extends Object {
$controller->data = null; $controller->data = null;
} }
if (array_key_exists('return', $this->params) && $this->params['return'] == 1) {
$controller->autoRender = false;
}
if (!empty($this->params['bare'])) { if (!empty($this->params['bare'])) {
$controller->autoLayout = false; $controller->autoLayout = false;
} }
if (isset($this->params['layout'])) { if (array_key_exists('layout', $this->params)) {
if ($this->params['layout'] === '') { if (empty($this->params['layout'])) {
$controller->autoLayout = false; $controller->autoLayout = false;
} else { } else {
$controller->layout = $this->params['layout']; $controller->layout = $this->params['layout'];
@ -205,28 +208,6 @@ class Dispatcher extends Object {
$controller->viewPath = $this->params['viewPath']; $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); return $this->_invoke($controller, $this->params, $missingAction);
} }
/** /**
@ -240,11 +221,21 @@ class Dispatcher extends Object {
* @access protected * @access protected
*/ */
function _invoke(&$controller, $params, $missingAction = false) { function _invoke(&$controller, $params, $missingAction = false) {
$classVars = get_object_vars($controller); $controller->constructClasses();
if ($missingAction && array_key_exists('scaffold', $classVars)) { $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'); App::import('Core', 'Scaffold');
return new Scaffold($controller, $params); return new Scaffold($controller, $params);
} elseif ($missingAction && !array_key_exists('scaffold', $classVars)) { }
return $this->cakeError('missingAction', array( return $this->cakeError('missingAction', array(
array( array(
'className' => Inflector::camelize($params['controller']."Controller"), 'className' => Inflector::camelize($params['controller']."Controller"),
@ -252,10 +243,11 @@ class Dispatcher extends Object {
'webroot' => $this->webroot, 'webroot' => $this->webroot,
'url' => $this->here, 'url' => $this->here,
'base' => $this->base))); 'base' => $this->base)));
} else {
$output = $controller->dispatchMethod($params['action'], $params['pass']);
} }
$output = $controller->dispatchMethod($params['action'], $params['pass']);
if ($controller->autoRender) { if ($controller->autoRender) {
$controller->output = $controller->render(); $controller->output = $controller->render();
} elseif (empty($controller->output)) { } elseif (empty($controller->output)) {
@ -674,7 +666,11 @@ class Dispatcher extends Object {
header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT"); header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
header("Cache-Control: cache"); header("Cache-Control: cache");
header("Pragma: cache"); header("Pragma: cache");
if ($type === 'css' || $type === 'js') {
include($assetFile);
} else {
readfile($assetFile); readfile($assetFile);
}
if(Configure::read('Asset.compress')) { if(Configure::read('Asset.compress')) {
header("Content-length: " . ob_get_length()); header("Content-length: " . ob_get_length());
@ -703,7 +699,7 @@ class Dispatcher extends Object {
App::import('Core', 'View'); App::import('Core', 'View');
} }
$controller = null; $controller = null;
$view = new View($controller, false); $view =& new View($controller, false);
return $view->renderCache($filename, getMicrotime()); return $view->renderCache($filename, getMicrotime());
} }
} }

View file

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

View file

@ -45,22 +45,14 @@ class TestDispatcher extends Dispatcher {
* @return void * @return void
*/ */
function _invoke(&$controller, $params, $missingAction) { function _invoke(&$controller, $params, $missingAction) {
$controller->params =& $params; restore_error_handler();
$classVars = get_object_vars($controller); if ($result = parent::_invoke($controller, $params, $missingAction)) {
if ($missingAction && array_key_exists('scaffold', $classVars)) { if ($result === 'missingAction') {
uses('controller'. DS . 'scaffold'); return $result;
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
)
));
} }
}
set_error_handler('simpleTestErrorHandler');
return $controller; return $controller;
} }
/** /**
@ -183,6 +175,25 @@ class SomePagesController extends AppController {
function index() { function index() {
return true; 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 * OtherPagesController class
@ -331,7 +342,11 @@ class SomePostsController extends AppController {
* @return void * @return void
*/ */
function beforeFilter() { function beforeFilter() {
if ($this->params['action'] == 'index') {
$this->params['action'] = 'view'; $this->params['action'] = 'view';
} else {
$this->params['action'] = 'change';
}
$this->params['pass'] = array('changed'); $this->params['pass'] = array('changed');
} }
/** /**
@ -343,6 +358,15 @@ class SomePostsController extends AppController {
function index() { function index() {
return true; return true;
} }
/**
* change method
*
* @access public
* @return void
*/
function change() {
return true;
}
} }
/** /**
* TestCachedPagesController class * TestCachedPagesController class
@ -947,7 +971,7 @@ class DispatcherTest extends CakeTestCase {
function testPrivate() { function testPrivate() {
$Dispatcher =& new TestDispatcher(); $Dispatcher =& new TestDispatcher();
Configure::write('App.baseUrl','/index.php'); 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)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$expected = 'privateAction'; $expected = 'privateAction';
@ -967,6 +991,14 @@ class DispatcherTest extends CakeTestCase {
$controller = $Dispatcher->dispatch($url, array('return'=> 1)); $controller = $Dispatcher->dispatch($url, array('return'=> 1));
$expected = 'missingAction'; $expected = 'missingAction';
$this->assertEqual($expected, $controller); $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 * testDispatch method
@ -1000,8 +1032,8 @@ class DispatcherTest extends CakeTestCase {
$expected = 'Pages'; $expected = 'Pages';
$this->assertEqual($expected, $controller->name); $this->assertEqual($expected, $controller->name);
unset($Dispatcher); unset($Dispatcher);
$Dispatcher =& new TestDispatcher(); $Dispatcher =& new TestDispatcher();
Configure::write('App.baseUrl','/timesheets/index.php'); Configure::write('App.baseUrl','/timesheets/index.php');
@ -1274,10 +1306,16 @@ class DispatcherTest extends CakeTestCase {
$url = 'some_posts/index/param:value/param2:value2'; $url = 'some_posts/index/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $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'; $expected = 'SomePosts';
$this->assertEqual($expected, $controller->name); $this->assertEqual($expected, $controller->name);
$expected = 'view'; $expected = 'change';
$this->assertEqual($expected, $controller->action); $this->assertEqual($expected, $controller->action);
$expected = array('changed'); $expected = array('changed');