root/trunk/server/www/cake/libs/error.php @ 310

Revision 310, 9.2 KB (checked in by sander, 6 months ago)

Do not assign the return of 'new' by reference or PHP 5.3 will yell

Line 
1<?php
2/* SVN FILE: $Id: error.php 7945 2008-12-19 02:16:01Z gwoo $ */
3/**
4 * Error handler
5 *
6 * Provides Error Capturing for Framework errors.
7 *
8 * PHP versions 4 and 5
9 *
10 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12 *
13 * Licensed under The MIT License
14 * Redistributions of files must retain the above copyright notice.
15 *
16 * @filesource
17 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19 * @package       cake
20 * @subpackage    cake.cake.libs
21 * @since         CakePHP(tm) v 0.10.5.1732
22 * @version       $Revision: 7945 $
23 * @modifiedby    $LastChangedBy: gwoo $
24 * @lastmodified  $Date: 2008-12-18 20:16:01 -0600 (Thu, 18 Dec 2008) $
25 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26 */
27App::import('Controller', 'App');
28/**
29 * Error Handling Controller
30 *
31 * Controller used by ErrorHandler to render error views.
32 *
33 * @package       cake
34 * @subpackage    cake.cake.libs
35 */
36class CakeErrorController extends AppController {
37        var $name = 'CakeError';
38/**
39 * Uses Property
40 *
41 * @var array
42 */
43        var $uses = array();
44/**
45 * __construct
46 *
47 * @access public
48 * @return void
49 */
50        function __construct() {
51                parent::__construct();
52                $this->_set(Router::getPaths());
53                $this->params = Router::getParams();
54                $this->constructClasses();
55                $this->Component->initialize($this);
56                $this->_set(array('cacheAction' => false, 'viewPath' => 'errors'));
57        }
58}
59/**
60 * Error Handler.
61 *
62 * Captures and handles all cakeError() calls.
63 * Displays helpful framework errors when debug > 1.
64 * When debug < 1 cakeError() will render 404 or 500 errors.
65 *
66 * @package       cake
67 * @subpackage    cake.cake.libs
68 */
69class ErrorHandler extends Object {
70/**
71 * Controller instance.
72 *
73 * @var object
74 * @access public
75 */
76        var $controller = null;
77/**
78 * Class constructor.
79 *
80 * @param string $method Method producing the error
81 * @param array $messages Error messages
82 */
83        function __construct($method, $messages) {
84                App::import('Core', 'Sanitize');
85                static $__previousError = null;
86
87                if ($__previousError != array($method, $messages)) {
88                        $__previousError = array($method, $messages);
89                        $this->controller = new CakeErrorController();
90                } else {
91                        $this->controller = new Controller();
92                        $this->controller->viewPath = 'errors';
93                }
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() == 0) {
112                                $method = 'error404';
113                                if (isset($code) && $code == 500) {
114                                        $method = 'error500';
115                                }
116                        }
117                }
118                $this->dispatchMethod($method, $messages);
119                $this->_stop();
120        }
121/**
122 * Displays an error page (e.g. 404 Not found).
123 *
124 * @param array $params Parameters for controller
125 * @access public
126 */
127        function error($params) {
128                extract($params, EXTR_OVERWRITE);
129                $this->controller->set(array(
130                        'code' => $code,
131                        'name' => $name,
132                        'message' => $message,
133                        'title' => $code . ' ' . $name
134                ));
135                $this->_outputMessage('error404');
136        }
137/**
138 * Convenience method to display a 404 page.
139 *
140 * @param array $params Parameters for controller
141 * @access public
142 */
143        function error404($params) {
144                extract($params, EXTR_OVERWRITE);
145
146                if (!isset($url)) {
147                        $url = $this->controller->here;
148                }
149                $url = Router::normalize($url);
150                header("HTTP/1.0 404 Not Found");
151                $this->controller->set(array(
152                        'code' => '404',
153                        'name' => __('Not Found', true),
154                        'message' => $url,
155                        'base' => $this->controller->base
156                ));
157                $this->_outputMessage('error404');
158        }
159/**
160 * Renders the Missing Controller web page.
161 *
162 * @param array $params Parameters for controller
163 * @access public
164 */
165        function missingController($params) {
166                extract($params, EXTR_OVERWRITE);
167
168                $controllerName = str_replace('Controller', '', $className);
169                $this->controller->set(array(
170                        'controller' => $className,
171                        'controllerName' => $controllerName,
172                        'title' => __('Missing Controller', true)
173                ));
174                $this->_outputMessage('missingController');
175        }
176/**
177 * Renders the Missing Action web page.
178 *
179 * @param array $params Parameters for controller
180 * @access public
181 */
182        function missingAction($params) {
183                extract($params, EXTR_OVERWRITE);
184
185                $controllerName = str_replace('Controller', '', $className);
186                $this->controller->set(array(
187                        'controller' => $className,
188                        'controllerName' => $controllerName,
189                        'action' => $action,
190                        'title' => __('Missing Method in Controller', true)
191                ));
192                $this->_outputMessage('missingAction');
193        }
194/**
195 * Renders the Private Action web page.
196 *
197 * @param array $params Parameters for controller
198 * @access public
199 */
200        function privateAction($params) {
201                extract($params, EXTR_OVERWRITE);
202
203                $this->controller->set(array(
204                        'controller' => $className,
205                        'action' => $action,
206                        'title' => __('Trying to access private method in class', true)
207                ));
208                $this->_outputMessage('privateAction');
209        }
210/**
211 * Renders the Missing Table web page.
212 *
213 * @param array $params Parameters for controller
214 * @access public
215 */
216        function missingTable($params) {
217                extract($params, EXTR_OVERWRITE);
218
219                $this->controller->set(array(
220                        'model' => $className,
221                        'table' => $table,
222                        'title' => __('Missing Database Table', true)
223                ));
224                $this->_outputMessage('missingTable');
225        }
226/**
227 * Renders the Missing Database web page.
228 *
229 * @param array $params Parameters for controller
230 * @access public
231 */
232        function missingDatabase($params = array()) {
233                extract($params, EXTR_OVERWRITE);
234
235                $this->controller->set(array(
236                        'title' => __('Scaffold Missing Database Connection', true)
237                ));
238                $this->_outputMessage('missingScaffolddb');
239        }
240/**
241 * Renders the Missing View web page.
242 *
243 * @param array $params Parameters for controller
244 * @access public
245 */
246        function missingView($params) {
247                extract($params, EXTR_OVERWRITE);
248
249                $this->controller->set(array(
250                        'controller' => $className,
251                        'action' => $action,
252                        'file' => $file,
253                        'title' => __('Missing View', true)
254                ));
255                $this->_outputMessage('missingView');
256        }
257/**
258 * Renders the Missing Layout web page.
259 *
260 * @param array $params Parameters for controller
261 * @access public
262 */
263        function missingLayout($params) {
264                extract($params, EXTR_OVERWRITE);
265
266                $this->controller->layout = 'default';
267                $this->controller->set(array(
268                        'file' => $file,
269                        'title' => __('Missing Layout', true)
270                ));
271                $this->_outputMessage('missingLayout');
272        }
273/**
274 * Renders the Database Connection web page.
275 *
276 * @param array $params Parameters for controller
277 * @access public
278 */
279        function missingConnection($params) {
280                extract($params, EXTR_OVERWRITE);
281
282                $this->controller->set(array(
283                        'model' => $className,
284                        'title' => __('Missing Database Connection', true)
285                ));
286                $this->_outputMessage('missingConnection');
287        }
288/**
289 * Renders the Missing Helper file web page.
290 *
291 * @param array $params Parameters for controller
292 * @access public
293 */
294        function missingHelperFile($params) {
295                extract($params, EXTR_OVERWRITE);
296
297                $this->controller->set(array(
298                        'helperClass' => Inflector::camelize($helper) . "Helper",
299                        'file' => $file,
300                        'title' => __('Missing Helper File', true)
301                ));
302                $this->_outputMessage('missingHelperFile');
303        }
304/**
305 * Renders the Missing Helper class web page.
306 *
307 * @param array $params Parameters for controller
308 * @access public
309 */
310        function missingHelperClass($params) {
311                extract($params, EXTR_OVERWRITE);
312
313                $this->controller->set(array(
314                        'helperClass' => Inflector::camelize($helper) . "Helper",
315                        'file' => $file,
316                        'title' => __('Missing Helper Class', true)
317                ));
318                $this->_outputMessage('missingHelperClass');
319        }
320/**
321 * Renders the Missing Component file web page.
322 *
323 * @param array $params Parameters for controller
324 * @access public
325 */
326        function missingComponentFile($params) {
327                extract($params, EXTR_OVERWRITE);
328
329                $this->controller->set(array(
330                        'controller' => $className,
331                        'component' => $component,
332                        'file' => $file,
333                        'title' => __('Missing Component File', true)
334                ));
335                $this->_outputMessage('missingComponentFile');
336        }
337/**
338 * Renders the Missing Component class web page.
339 *
340 * @param array $params Parameters for controller
341 * @access public
342 */
343        function missingComponentClass($params) {
344                extract($params, EXTR_OVERWRITE);
345
346                $this->controller->set(array(
347                        'controller' => $className,
348                        'component' => $component,
349                        'file' => $file,
350                        'title' => __('Missing Component Class', true)
351                ));
352                $this->_outputMessage('missingComponentClass');
353        }
354/**
355 * Renders the Missing Model class web page.
356 *
357 * @param unknown_type $params Parameters for controller
358 * @access public
359 */
360        function missingModel($params) {
361                extract($params, EXTR_OVERWRITE);
362
363                $this->controller->set(array(
364                        'model' => $className,
365                        'title' => __('Missing Model', true)
366                ));
367                $this->_outputMessage('missingModel');
368        }
369/**
370 * Output message
371 *
372 * @access protected
373 */
374        function _outputMessage($template) {
375                $this->controller->render($template);
376                $this->controller->afterFilter();
377                echo $this->controller->output;
378        }
379}
380?>
Note: See TracBrowser for help on using the browser.