updating console so shells and tasks do not have to extend Shell, updated error handler params and maintained backwards compatibility closes #3538, updated Object::cakeError to add app_error, closes #3982

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6407 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-01-24 20:32:10 +00:00
parent cb27bcc21a
commit f598b58465
3 changed files with 73 additions and 122 deletions

View file

@ -274,28 +274,32 @@ class ShellDispatcher {
$this->shellCommand = Inflector::variable($command); $this->shellCommand = Inflector::variable($command);
$shell = new $this->shellClass($this); $shell = new $this->shellClass($this);
$shell->initialize(); if (get_parent_class($shell) == 'Shell') {
$shell->loadTasks(); $shell->initialize();
$shell->loadTasks();
foreach ($shell->taskNames as $task) { foreach ($shell->taskNames as $task) {
$shell->{$task}->initialize(); if (get_parent_class($shell->{$task}) == 'Shell') {
$shell->{$task}->loadTasks(); $shell->{$task}->initialize();
} $shell->{$task}->loadTasks();
$task = Inflector::camelize($command);
if (in_array($task, $shell->taskNames)) {
$this->shiftArgs();
$shell->{$task}->startup();
if (isset($this->args[0]) && $this->args[0] == 'help') {
if (method_exists($shell->{$task}, 'help')) {
$shell->{$task}->help();
exit();
} else {
$this->help();
} }
} }
$shell->{$task}->execute();
return; $task = Inflector::camelize($command);
if (in_array($task, $shell->taskNames)) {
$this->shiftArgs();
$shell->{$task}->startup();
if (isset($this->args[0]) && $this->args[0] == 'help') {
if (method_exists($shell->{$task}, 'help')) {
$shell->{$task}->help();
exit();
} else {
$this->help();
}
}
$shell->{$task}->execute();
return;
}
} }
$classMethods = get_class_methods($shell); $classMethods = get_class_methods($shell);

View file

@ -26,7 +26,6 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
uses('sanitize');
/** /**
* Short description for file. * Short description for file.
* *
@ -54,38 +53,51 @@ class ErrorHandler extends Object{
parent::__construct(); parent::__construct();
static $__previousError = null; static $__previousError = null;
if ($__previousError != array($method, $messages)) {
$__previousError = array($method, $messages);
if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php';
}
$this->__dispatch =& new Dispatcher();
$this->__dispatch->base = $this->__dispatch->baseUrl();
if (!class_exists('appcontroller')) {
App::import('Controller', 'App');
}
$this->controller =& new AppController();
$this->controller->base = $this->__dispatch->base;
$this->controller->webroot = $this->__dispatch->webroot;
$this->controller->params = Router::getParams();
$this->controller->here = $this->controller->params['url']['url'];
$this->controller->viewPath = 'errors';
$this->controller->cacheAction = false;
$this->controller->constructClasses();
$this->__dispatch->start($this->controller);
}
$allow = array('.', '/', '_', ' ', '-', '~'); $allow = array('.', '/', '_', ' ', '-', '~');
if (substr(PHP_OS,0,3) == "WIN") { if (substr(PHP_OS,0,3) == "WIN") {
$allow = array_merge($allow, array('\\', ':') ); $allow = array_merge($allow, array('\\', ':') );
} }
$clean = new Sanitize();
$messages = $clean->paranoid($messages, $allow); App::import('Core', 'Sanitize');
if (!class_exists('dispatcher')) { $messages = Sanitize::paranoid($messages, $allow);
require CAKE . 'dispatcher.php';
} if (!isset($messages[0])) {
$this->__dispatch =& new Dispatcher(); $messages = array($messages);
if (!class_exists('appcontroller')) {
App::import('Controller', 'App');
} }
if ($__previousError != array($method, $messages)) { if (method_exists($this->controller, 'apperror')) {
$__previousError = array($method, $messages); return $this->controller->appError($method, $messages);
$this->controller =& new AppController();
if (!empty($this->controller->uses)) {
$this->controller->constructClasses();
}
$this->controller->_initComponents();
$this->controller->cacheAction = false;
$this->__dispatch->start($this->controller);
if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($method, $messages);
}
} else {
$this->controller =& new AppController();
$this->controller->cacheAction = false;
} }
if (!in_array($method, get_class_methods($this))) {
$method = 'error';
}
if (Configure::read() > 0 || $method == 'error') { if (Configure::read() > 0 || $method == 'error') {
call_user_func_array(array(&$this, $method), $messages); call_user_func_array(array(&$this, $method), $messages);
} else { } else {
@ -99,10 +111,7 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function error($params) { function error($params) {
extract($params); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $this->_webroot();
$this->controller->viewPath = 'errors';
$this->controller->set(array('code' => $code, $this->controller->set(array('code' => $code,
'name' => $name, 'name' => $name,
'message' => $message, 'message' => $message,
@ -117,23 +126,17 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function error404($params) { function error404($params) {
extract($params); extract($params, EXTR_OVERWRITE);
if (!isset($url)) { if (!isset($url)) {
$url = $action; $url = $this->controller->here;
} }
if (!isset($message)) { $url = Router::normalize($url);
$message = '';
}
if (!isset($base)) {
$base = '';
}
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
$this->error(array('code' => '404', $this->error(array('code' => '404',
'name' => __('Not Found', true), 'name' => __('Not Found', true),
'message' => sprintf(__("The requested address %s was not found on this server.", true), "<strong>'{$url}'</strong>", $message), 'message' => sprintf(__("The requested address %s was not found on this server.", true), "<strong>'{$url}'</strong>"),
'base' => $base)); 'base' => $this->controller->base));
exit(); exit();
} }
/** /**
@ -143,12 +146,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingController($params) { function missingController($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath ='errors';
$controllerName = str_replace('Controller', '', $className); $controllerName = str_replace('Controller', '', $className);
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'controllerName' => $controllerName, 'controllerName' => $controllerName,
@ -163,12 +162,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingAction($params) { function missingAction($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath = 'errors';
$controllerName = str_replace('Controller', '', $className); $controllerName = str_replace('Controller', '', $className);
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'controllerName' => $controllerName, 'controllerName' => $controllerName,
@ -184,12 +179,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function privateAction($params) { function privateAction($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->webroot = $webroot;
$this->controller->viewPath = 'errors';
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'action' => $action, 'action' => $action,
'title' => __('Trying to access private method in class', true))); 'title' => __('Trying to access private method in class', true)));
@ -203,11 +194,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingTable($params) { function missingTable($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('model' => $className, $this->controller->set(array('model' => $className,
'table' => $table, 'table' => $table,
'title' => __('Missing Database Table', true))); 'title' => __('Missing Database Table', true)));
@ -221,11 +209,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingDatabase($params = array()) { function missingDatabase($params = array()) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('title' => __('Scaffold Missing Database Connection', true))); $this->controller->set(array('title' => __('Scaffold Missing Database Connection', true)));
$this->controller->render('missingScaffolddb'); $this->controller->render('missingScaffolddb');
exit(); exit();
@ -237,12 +222,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingView($params) { function missingView($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'action' => $action, 'action' => $action,
'file' => $file, 'file' => $file,
@ -257,12 +238,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingLayout($params) { function missingLayout($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->layout = 'default'; $this->controller->layout = 'default';
$this->controller->set(array('file' => $file, $this->controller->set(array('file' => $file,
'title' => __('Missing Layout', true))); 'title' => __('Missing Layout', true)));
@ -276,11 +253,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingConnection($params) { function missingConnection($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('model' => $className, $this->controller->set(array('model' => $className,
'title' => __('Missing Database Connection', true))); 'title' => __('Missing Database Connection', true)));
$this->controller->render('missingConnection'); $this->controller->render('missingConnection');
@ -293,12 +267,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingHelperFile($params) { function missingHelperFile($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper", $this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper",
'file' => $file, 'file' => $file,
'title' => __('Missing Helper File', true))); 'title' => __('Missing Helper File', true)));
@ -312,12 +282,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingHelperClass($params) { function missingHelperClass($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper", $this->controller->set(array('helperClass' => Inflector::camelize($helper) . "Helper",
'file' => $file, 'file' => $file,
'title' => __('Missing Helper Class', true))); 'title' => __('Missing Helper Class', true)));
@ -331,12 +297,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingComponentFile($params) { function missingComponentFile($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'component' => $component, 'component' => $component,
'file' => $file, 'file' => $file,
@ -351,12 +313,8 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingComponentClass($params) { function missingComponentClass($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('controller' => $className, $this->controller->set(array('controller' => $className,
'component' => $component, 'component' => $component,
'file' => $file, 'file' => $file,
@ -371,26 +329,12 @@ class ErrorHandler extends Object{
* @access public * @access public
*/ */
function missingModel($params) { function missingModel($params) {
extract(Router::getPaths());
extract($params, EXTR_OVERWRITE); extract($params, EXTR_OVERWRITE);
$this->controller->base = $base;
$this->controller->viewPath = 'errors';
$this->controller->webroot = $this->_webroot();
$this->controller->set(array('model' => $className, $this->controller->set(array('model' => $className,
'title' => __('Missing Model', true))); 'title' => __('Missing Model', true)));
$this->controller->render('missingModel'); $this->controller->render('missingModel');
exit(); exit();
} }
/**
* Path to the web root.
*
* @return string full web root path
* @access private
*/
function _webroot() {
$this->__dispatch->baseUrl();
return $this->__dispatch->webroot;
}
} }
?> ?>

View file

@ -157,9 +157,12 @@ class Object {
*/ */
function cakeError($method, $messages) { function cakeError($method, $messages) {
if (!class_exists('ErrorHandler')) { if (!class_exists('ErrorHandler')) {
uses('error');
if (file_exists(APP . 'error.php')) { if (file_exists(APP . 'error.php')) {
include_once (APP . 'error.php'); include_once (APP . 'error.php');
} elseif (file_exists(APP . 'app_error.php')) {
include_once (APP . 'app_error.php');
} else {
App::import('Core', 'Error');
} }
} }
@ -234,9 +237,9 @@ class Object {
$vars = unserialize(${$name}); $vars = unserialize(${$name});
foreach ($vars['0'] as $key => $value) { foreach ($vars['0'] as $key => $value) {
if(strpos($key, '_behavior')) { if(strpos($key, '_behavior')) {
loadBehavior(Inflector::classify(str_replace('_behavior', '', $key))); App::import('Behavior', Inflector::classify(str_replace('_behavior', '', $key)));
} else { } else {
loadModel(Inflector::classify($key)); App::import('Model', Inflector::classify($key));
} }
} }
unset($vars); unset($vars);