Adding Router path fixes for error pages (Ticket #1566), fixing code formatting (Dispatcher), and adding API enhancements for model behaviors

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3727 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-10-20 17:24:37 +00:00
parent 607158de45
commit 2993a8d412
6 changed files with 188 additions and 91 deletions

View file

@ -93,11 +93,16 @@ class Dispatcher extends Object {
$pluginName = Inflector::camelize($params['action']);
if (!loadPluginController(Inflector::underscore($ctrlName), $pluginName)) {
if(preg_match('/([\\.]+)/', $ctrlName)) {
Router::setParams(array($params, array('base' => $this->base, 'webroot' => $this->webroot)));
return $this->cakeError('error404', array(
array('url' => strtolower($ctrlName),
'message' => 'Was not found on this server',
'base' => $this->base)));
exit();
array(
'url' => strtolower($ctrlName),
'message' => 'Was not found on this server',
'base' => $this->base
)
));
exit();
} elseif(!class_exists($ctrlClass)) {
$missingController = true;
}
@ -142,16 +147,20 @@ class Dispatcher extends Object {
$params['action'] = CAKE_ADMIN.'_'.$params['action'];
}
} elseif (strpos($params['action'], CAKE_ADMIN) === 0) {
$privateAction = true;
$privateAction = true;
}
}
if ($missingController) {
Router::setParams(array($params, array('base' => $this->base, 'webroot' => $this->webroot)));
return $this->cakeError('missingController', array(
array('className' => Inflector::camelize($params['controller']."Controller"),
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
array(
'className' => Inflector::camelize($params['controller']."Controller"),
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base
)
));
} else {
$controller =& new $ctrlClass();
}
@ -172,11 +181,11 @@ class Dispatcher extends Object {
}
if (in_array(strtolower($params['action']), array('toString', 'requestAction', 'log',
'cakeError', 'constructClasses', 'redirect',
'set', 'setAction', 'validate', 'validateErrors',
'render', 'referer', 'flash', 'flashOut',
'generateFieldNames', 'postConditions', 'cleanUpFields',
'beforefilter', 'beforerender', 'afterfilter'))) {
'cakeError', 'constructClasses', 'redirect',
'set', 'setAction', 'validate', 'validateErrors',
'render', 'referer', 'flash', 'flashOut',
'generateFieldNames', 'postConditions', 'cleanUpFields',
'beforefilter', 'beforerender', 'afterfilter'))) {
$missingAction = true;
}
@ -260,27 +269,34 @@ class Dispatcher extends Object {
$controller->_initComponents();
$controller->constructClasses();
Router::setParams(array($params, array('base' => $controller->base, 'here' => $controller->here, 'webroot' => $controller->webroot, 'passedArgs' => $controller->passedArgs, 'argSeparator' => $controller->argSeparator, 'namedArgs' => $controller->namedArgs, 'webservices' => $controller->webservices)));
if ($missingAction && !in_array('scaffold', array_keys($classVars))){
$this->start($controller);
return $this->cakeError('missingAction', array(
array('className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
array(
'className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base
)
));
}
if ($privateAction) {
$this->start($controller);
return $this->cakeError('privateAction', array(
array('className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base)));
array(
'className' => Inflector::camelize($params['controller']."Controller"),
'action' => $params['action'],
'webroot' => $this->webroot,
'url' => $url,
'base' => $this->base
)
));
}
Router::setParams(array($params, array('base' => $controller->base, 'here' => $controller->here, 'webroot' => $controller->webroot, 'passedArgs' => $controller->passedArgs, 'argSeparator' => $controller->argSeparator, 'namedArgs' => $controller->namedArgs, 'webservices' => $controller->webservices)));
return $this->_invoke($controller, $params, $missingAction);
}
/**

View file

@ -373,49 +373,49 @@ class Controller extends Object {
if ($status != null) {
$codes = array(
100 => "HTTP/1.1 100 Continue",
101 => "HTTP/1.1 101 Switching Protocols",
200 => "HTTP/1.1 200 OK",
201 => "HTTP/1.1 201 Created",
202 => "HTTP/1.1 202 Accepted",
203 => "HTTP/1.1 203 Non-Authoritative Information",
204 => "HTTP/1.1 204 No Content",
205 => "HTTP/1.1 205 Reset Content",
206 => "HTTP/1.1 206 Partial Content",
300 => "HTTP/1.1 300 Multiple Choices",
301 => "HTTP/1.1 301 Moved Permanently",
302 => "HTTP/1.1 302 Found",
303 => "HTTP/1.1 303 See Other",
304 => "HTTP/1.1 304 Not Modified",
305 => "HTTP/1.1 305 Use Proxy",
307 => "HTTP/1.1 307 Temporary Redirect",
400 => "HTTP/1.1 400 Bad Request",
401 => "HTTP/1.1 401 Unauthorized",
402 => "HTTP/1.1 402 Payment Required",
403 => "HTTP/1.1 403 Forbidden",
404 => "HTTP/1.1 404 Not Found",
405 => "HTTP/1.1 405 Method Not Allowed",
406 => "HTTP/1.1 406 Not Acceptable",
407 => "HTTP/1.1 407 Proxy Authentication Required",
408 => "HTTP/1.1 408 Request Time-out",
409 => "HTTP/1.1 409 Conflict",
410 => "HTTP/1.1 410 Gone",
411 => "HTTP/1.1 411 Length Required",
412 => "HTTP/1.1 412 Precondition Failed",
413 => "HTTP/1.1 413 Request Entity Too Large",
414 => "HTTP/1.1 414 Request-URI Too Large",
415 => "HTTP/1.1 415 Unsupported Media Type",
416 => "HTTP/1.1 416 Requested range not satisfiable",
417 => "HTTP/1.1 417 Expectation Failed",
500 => "HTTP/1.1 500 Internal Server Error",
501 => "HTTP/1.1 501 Not Implemented",
502 => "HTTP/1.1 502 Bad Gateway",
503 => "HTTP/1.1 503 Service Unavailable",
504 => "HTTP/1.1 504 Gateway Time-out"
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Time-out",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Large",
415 => "Unsupported Media Type",
416 => "Requested range not satisfiable",
417 => "Expectation Failed",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Time-out"
);
if (isset($codes[$status])) {
header($codes[$status]);
header("HTTP/1.1 {$status} " . $codes[$status]);
}
}
header('Location: ' . Router::url($url, defined('SERVER_IIS')));

View file

@ -65,6 +65,7 @@ class ErrorHandler extends Object{
}
$this->controller =& new AppController();
$this->controller->_initComponents();
$this->__dispatch->start($this->controller);
@ -128,7 +129,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingController($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath ='errors';
@ -145,7 +148,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingAction($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath = 'errors';
@ -161,7 +166,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function privateAction($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath = 'errors';
@ -177,7 +184,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingTable($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('model' => $className,
@ -192,7 +201,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingDatabase($params = array()) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('title' => 'Scaffold Missing Database Connection'));
@ -205,7 +216,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingView($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -222,7 +235,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingLayout($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -238,7 +253,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingConnection($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('model' => $className,
@ -252,7 +269,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingHelperFile($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -268,7 +287,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingHelperClass($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -284,7 +305,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingComponentFile($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -301,7 +324,9 @@ class ErrorHandler extends Object{
* @param array $params
*/
function missingComponentClass($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -318,7 +343,9 @@ class ErrorHandler extends Object{
* @param unknown_type $params
*/
function missingModel($params) {
extract($params);
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
@ -337,4 +364,5 @@ class ErrorHandler extends Object{
return $this->__dispatch->webroot;
}
}
?>

View file

@ -30,7 +30,7 @@
class ModelBehavior extends Object {
var $mapMethods = array();
function setup(&$model, $config = array()) { }
function beforeFind(&$model, &$query) { }

View file

@ -392,11 +392,25 @@ class Model extends Overloadable {
$this->actsAs = normalizeList($this->actsAs);
foreach ($this->actsAs as $behavior => $config) {
$className = $behavior . 'Behavior';
if (!loadBehavior($behavior)) {
// Raise an error
} else {
$className = $behavior . 'Behavior';
$this->behaviors[$behavior] =& new $className;
if (ClassRegistry::isKeySet($className)) {
if (PHP5) {
$this->behaviors[$behavior] = ClassRegistry::getObject($className);
} else {
$this->behaviors[$behavior] =& ClassRegistry::getObject($className);
}
} else {
if (PHP5) {
$this->behaviors[$behavior] = new $className;
} else {
$this->behaviors[$behavior] =& new $className;
}
ClassRegistry::addObject($className, $this->behaviors[$behavior]);
}
$this->behaviors[$behavior]->setup($this, $config);
$methods = $this->behaviors[$behavior]->mapMethods;
@ -432,17 +446,22 @@ class Model extends Overloadable {
function __call__($method, $params) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$methods = array_keys($this->__behaviorMethods);
$methods = array_map('strtolower', array_keys($this->__behaviorMethods));
$call = array_values($this->__behaviorMethods);
$map = array();
if (!empty($methods) && !empty($call)) {
$map = array_combine($methods, $call);
}
$count = count($call);
if (in_array(low($method), $methods)) {
return $this->behaviors[$map[low($method)][1]]->{$map[low($method)][0]}($this, $params);
}
for($i = 0; $i < $count; $i++) {
if (strpos($methods[$i], '/') === 0) {
if (preg_match($methods[$i], $method)) {
return $this->behaviors[$call[$i][1]]->{$call[$i][0]}($this, $params, $method);
}
} elseif (strtolower($methods[$i]) == strtolower($method)) {
return $this->behaviors[$call[$i][1]]->{$call[$i][0]}($this, $params);
if (strpos($methods[$i], '/') === 0 && preg_match($methods[$i] . 'i', $method)) {
return $this->behaviors[$call[$i][1]]->{$call[$i][0]}($this, $params, $method);
}
}
@ -1002,8 +1021,6 @@ class Model extends Overloadable {
}
}
$this->__insertID = $db->lastInsertId($this->tablePrefix . $this->table, $this->primaryKey);
if (!$this->__insertID && $newID != null) {
$this->__insertID = $newID;
$this->id = $newID;
@ -1091,7 +1108,8 @@ class Model extends Overloadable {
* @return boolean True on success, false on failure
*/
function updateAll($conditions, $fields) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
return $db->update($this, $fields, null, $conditions);
}
/**
* Synonym for del().
@ -1656,6 +1674,15 @@ class Model extends Overloadable {
function getInsertID() {
return $this->__insertID;
}
/**
* Sets the ID of the last record this Model inserted
*
* @param mixed $id
* @return void
*/
function setInsertID($id) {
$this->__insertID = $id;
}
/**
* Returns the number of rows returned from the last query
*

View file

@ -304,6 +304,32 @@ class Router extends Overloadable {
}
list($_this->__params[], $_this->__paths[]) = $params;
}
/**
* Gets parameter information
*
* @param boolean $current
* @return array
*/
function getParams($current = false) {
$_this =& Router::getInstance();
if ($current) {
return $_this->__params[count($this->__params) - 1];
}
return $_this->__params[0];
}
/**
* Gets path information
*
* @param boolean $current
* @return array
*/
function getPaths($current = false) {
$_this =& Router::getInstance();
if ($current) {
return $_this->__paths[count($this->__paths) - 1];
}
return $_this->__paths[0];
}
/**
* Reloads default Router settings
*