source: Dev/branches/cakephp/cake/libs/error.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

File size: 10.9 KB
Line 
1<?php
2/**
3 * Error handler
4 *
5 * Provides Error Capturing for Framework errors.
6 *
7 * PHP versions 4 and 5
8 *
9 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
11 *
12 * Licensed under The MIT License
13 * Redistributions of files must retain the above copyright notice.
14 *
15 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
16 * @link          http://cakephp.org CakePHP(tm) Project
17 * @package       cake
18 * @subpackage    cake.cake.libs
19 * @since         CakePHP(tm) v 0.10.5.1732
20 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
21 */
22App::import('Controller', 'App');
23
24/**
25 * Error Handling Controller
26 *
27 * Controller used by ErrorHandler to render error views.
28 *
29 * @package       cake
30 * @subpackage    cake.cake.libs
31 */
32class CakeErrorController extends AppController {
33        var $name = 'CakeError';
34
35/**
36 * Uses Property
37 *
38 * @var array
39 */
40        var $uses = array();
41
42/**
43 * __construct
44 *
45 * @access public
46 * @return void
47 */
48        function __construct() {
49                parent::__construct();
50                $this->_set(Router::getPaths());
51                $this->params = Router::getParams();
52                $this->constructClasses();
53                $this->Component->initialize($this);
54                $this->_set(array('cacheAction' => false, 'viewPath' => 'errors'));
55        }
56}
57
58/**
59 * Error Handler.
60 *
61 * Captures and handles all cakeError() calls.
62 * Displays helpful framework errors when debug > 1.
63 * When debug < 1 cakeError() will render 404 or 500 errors.
64 *
65 * @package       cake
66 * @subpackage    cake.cake.libs
67 */
68class ErrorHandler extends Object {
69
70/**
71 * Controller instance.
72 *
73 * @var Controller
74 * @access public
75 */
76        var $controller = null;
77
78/**
79 * Class constructor.
80 *
81 * @param string $method Method producing the error
82 * @param array $messages Error messages
83 */
84        function __construct($method, $messages) {
85                App::import('Core', 'Sanitize');
86                static $__previousError = null;
87
88                if ($__previousError != array($method, $messages)) {
89                        $__previousError = array($method, $messages);
90                        $this->controller =& new CakeErrorController();
91                } else {
92                        $this->controller =& new Controller();
93                        $this->controller->viewPath = 'errors';
94                }
95                $options = array('escape' => false);
96                $messages = Sanitize::clean($messages, $options);
97
98                if (!isset($messages[0])) {
99                        $messages = array($messages);
100                }
101
102                if (method_exists($this->controller, 'apperror')) {
103                        return $this->controller->appError($method, $messages);
104                }
105
106                if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
107                        $method = 'error';
108                }
109
110                if ($method !== 'error') {
111                        if (Configure::read('debug') == 0) {
112                                $parentClass = get_parent_class($this);
113                                if (strtolower($parentClass) != 'errorhandler') {
114                                        $method = 'error404';
115                                }
116                                $parentMethods = array_map('strtolower', get_class_methods($parentClass));
117                                if (in_array(strtolower($method), $parentMethods)) {
118                                        $method = 'error404';
119                                }
120                                if (isset($messages[0]['code']) && $messages[0]['code'] == 500) {
121                                        $method = 'error500';
122                                }
123                        }
124                }
125                $this->dispatchMethod($method, $messages);
126                $this->_stop();
127        }
128
129/**
130 * Displays an error page (e.g. 404 Not found).
131 *
132 * @param array $params Parameters for controller
133 * @access public
134 */
135        function error($params) {
136                extract($params, EXTR_OVERWRITE);
137                $this->controller->set(array(
138                        'code' => $code,
139                        'name' => $name,
140                        'message' => $message,
141                        'title' => $code . ' ' . $name
142                ));
143                $this->_outputMessage('error404');
144        }
145
146/**
147 * Convenience method to display a 404 page.
148 *
149 * @param array $params Parameters for controller
150 * @access public
151 */
152        function error404($params) {
153                extract($params, EXTR_OVERWRITE);
154
155                if (!isset($url)) {
156                        $url = $this->controller->here;
157                }
158                $url = Router::normalize($url);
159                $this->controller->header("HTTP/1.0 404 Not Found");
160                $this->controller->set(array(
161                        'code' => '404',
162                        'name' => __('Not Found', true),
163                        'message' => h($url),
164                        'base' => $this->controller->base
165                ));
166                $this->_outputMessage('error404');
167        }
168
169/**
170 * Convenience method to display a 500 page.
171 *
172 * @param array $params Parameters for controller
173 * @access public
174 */
175        function error500($params) {
176                extract($params, EXTR_OVERWRITE);
177
178                if (!isset($url)) {
179                        $url = $this->controller->here;
180                }
181                $url = Router::normalize($url);
182                $this->controller->header("HTTP/1.0 500 Internal Server Error");
183                $this->controller->set(array(
184                        'code' => '500',
185                        'name' => __('An Internal Error Has Occurred', true),
186                        'message' => h($url),
187                        'base' => $this->controller->base
188                ));
189                $this->_outputMessage('error500');
190        }
191/**
192 * Renders the Missing Controller web page.
193 *
194 * @param array $params Parameters for controller
195 * @access public
196 */
197        function missingController($params) {
198                extract($params, EXTR_OVERWRITE);
199
200                $controllerName = str_replace('Controller', '', $className);
201                $this->controller->set(array(
202                        'controller' => $className,
203                        'controllerName' => $controllerName,
204                        'title' => __('Missing Controller', true)
205                ));
206                $this->_outputMessage('missingController');
207        }
208
209/**
210 * Renders the Missing Action web page.
211 *
212 * @param array $params Parameters for controller
213 * @access public
214 */
215        function missingAction($params) {
216                extract($params, EXTR_OVERWRITE);
217
218                $controllerName = str_replace('Controller', '', $className);
219                $this->controller->set(array(
220                        'controller' => $className,
221                        'controllerName' => $controllerName,
222                        'action' => $action,
223                        'title' => __('Missing Method in Controller', true)
224                ));
225                $this->_outputMessage('missingAction');
226        }
227
228/**
229 * Renders the Private Action web page.
230 *
231 * @param array $params Parameters for controller
232 * @access public
233 */
234        function privateAction($params) {
235                extract($params, EXTR_OVERWRITE);
236
237                $this->controller->set(array(
238                        'controller' => $className,
239                        'action' => $action,
240                        'title' => __('Trying to access private method in class', true)
241                ));
242                $this->_outputMessage('privateAction');
243        }
244
245/**
246 * Renders the Missing Table web page.
247 *
248 * @param array $params Parameters for controller
249 * @access public
250 */
251        function missingTable($params) {
252                extract($params, EXTR_OVERWRITE);
253
254                $this->controller->header("HTTP/1.0 500 Internal Server Error");
255                $this->controller->set(array(
256                        'code' => '500',
257                        'model' => $className,
258                        'table' => $table,
259                        'title' => __('Missing Database Table', true)
260                ));
261                $this->_outputMessage('missingTable');
262        }
263
264/**
265 * Renders the Missing Database web page.
266 *
267 * @param array $params Parameters for controller
268 * @access public
269 */
270        function missingDatabase($params = array()) {
271                $this->controller->header("HTTP/1.0 500 Internal Server Error");
272                $this->controller->set(array(
273                        'code' => '500',
274                        'title' => __('Scaffold Missing Database Connection', true)
275                ));
276                $this->_outputMessage('missingScaffolddb');
277        }
278
279/**
280 * Renders the Missing View web page.
281 *
282 * @param array $params Parameters for controller
283 * @access public
284 */
285        function missingView($params) {
286                extract($params, EXTR_OVERWRITE);
287
288                $this->controller->set(array(
289                        'controller' => $className,
290                        'action' => $action,
291                        'file' => $file,
292                        'title' => __('Missing View', true)
293                ));
294                $this->_outputMessage('missingView');
295        }
296
297/**
298 * Renders the Missing Layout web page.
299 *
300 * @param array $params Parameters for controller
301 * @access public
302 */
303        function missingLayout($params) {
304                extract($params, EXTR_OVERWRITE);
305
306                $this->controller->layout = 'default';
307                $this->controller->set(array(
308                        'file' => $file,
309                        'title' => __('Missing Layout', true)
310                ));
311                $this->_outputMessage('missingLayout');
312        }
313
314/**
315 * Renders the Database Connection web page.
316 *
317 * @param array $params Parameters for controller
318 * @access public
319 */
320        function missingConnection($params) {
321                extract($params, EXTR_OVERWRITE);
322
323                $this->controller->header("HTTP/1.0 500 Internal Server Error");
324                $this->controller->set(array(
325                        'code' => '500',
326                        'model' => $className,
327                        'title' => __('Missing Database Connection', true)
328                ));
329                $this->_outputMessage('missingConnection');
330        }
331
332/**
333 * Renders the Missing Helper file web page.
334 *
335 * @param array $params Parameters for controller
336 * @access public
337 */
338        function missingHelperFile($params) {
339                extract($params, EXTR_OVERWRITE);
340
341                $this->controller->set(array(
342                        'helperClass' => Inflector::camelize($helper) . "Helper",
343                        'file' => $file,
344                        'title' => __('Missing Helper File', true)
345                ));
346                $this->_outputMessage('missingHelperFile');
347        }
348
349/**
350 * Renders the Missing Helper class web page.
351 *
352 * @param array $params Parameters for controller
353 * @access public
354 */
355        function missingHelperClass($params) {
356                extract($params, EXTR_OVERWRITE);
357
358                $this->controller->set(array(
359                        'helperClass' => Inflector::camelize($helper) . "Helper",
360                        'file' => $file,
361                        'title' => __('Missing Helper Class', true)
362                ));
363                $this->_outputMessage('missingHelperClass');
364        }
365
366/**
367 * Renders the Missing Behavior file web page.
368 *
369 * @param array $params Parameters for controller
370 * @access public
371 */
372        function missingBehaviorFile($params) {
373                extract($params, EXTR_OVERWRITE);
374
375                $this->controller->set(array(
376                        'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
377                        'file' => $file,
378                        'title' => __('Missing Behavior File', true)
379                ));
380                $this->_outputMessage('missingBehaviorFile');
381        }
382
383/**
384 * Renders the Missing Behavior class web page.
385 *
386 * @param array $params Parameters for controller
387 * @access public
388 */
389        function missingBehaviorClass($params) {
390                extract($params, EXTR_OVERWRITE);
391
392                $this->controller->set(array(
393                        'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
394                        'file' => $file,
395                        'title' => __('Missing Behavior Class', true)
396                ));
397                $this->_outputMessage('missingBehaviorClass');
398        }
399
400/**
401 * Renders the Missing Component file web page.
402 *
403 * @param array $params Parameters for controller
404 * @access public
405 */
406        function missingComponentFile($params) {
407                extract($params, EXTR_OVERWRITE);
408
409                $this->controller->set(array(
410                        'controller' => $className,
411                        'component' => $component,
412                        'file' => $file,
413                        'title' => __('Missing Component File', true)
414                ));
415                $this->_outputMessage('missingComponentFile');
416        }
417
418/**
419 * Renders the Missing Component class web page.
420 *
421 * @param array $params Parameters for controller
422 * @access public
423 */
424        function missingComponentClass($params) {
425                extract($params, EXTR_OVERWRITE);
426
427                $this->controller->set(array(
428                        'controller' => $className,
429                        'component' => $component,
430                        'file' => $file,
431                        'title' => __('Missing Component Class', true)
432                ));
433                $this->_outputMessage('missingComponentClass');
434        }
435
436/**
437 * Renders the Missing Model class web page.
438 *
439 * @param unknown_type $params Parameters for controller
440 * @access public
441 */
442        function missingModel($params) {
443                extract($params, EXTR_OVERWRITE);
444
445                $this->controller->set(array(
446                        'model' => $className,
447                        'title' => __('Missing Model', true)
448                ));
449                $this->_outputMessage('missingModel');
450        }
451
452/**
453 * Output message
454 *
455 * @access protected
456 */
457        function _outputMessage($template) {
458                $this->controller->render($template);
459                $this->controller->afterFilter();
460                echo $this->controller->output;
461        }
462}
Note: See TracBrowser for help on using the repository browser.