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

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

Cakephp branch.

File size: 6.4 KB
Line 
1<?php
2/**
3 * ErrorHandler for Console Shells
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link          http://cakephp.org CakePHP(tm) Project
15 * @package       cake
16 * @subpackage    cake.cake.console
17 * @since         CakePHP(tm) v 1.2.0.5074
18 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20
21/**
22 * Error Handler for Cake console.
23 *
24 * @package       cake
25 * @subpackage    cake.cake.console
26 */
27class ErrorHandler extends Object {
28
29/**
30 * Standard output stream.
31 *
32 * @var filehandle
33 * @access public
34 */
35        var $stdout;
36
37/**
38 * Standard error stream.
39 *
40 * @var filehandle
41 * @access public
42 */
43        var $stderr;
44
45/**
46 * Class constructor.
47 *
48 * @param string $method Method dispatching an error
49 * @param array $messages Error messages
50 */
51        function __construct($method, $messages) {
52                $this->stdout = fopen('php://stdout', 'w');
53                $this->stderr = fopen('php://stderr', 'w');
54                call_user_func_array(array(&$this, $method), $messages);
55        }
56
57/**
58 * Displays an error page (e.g. 404 Not found).
59 *
60 * @param array $params Parameters (code, name, and message)
61 * @access public
62 */
63        function error($params) {
64                extract($params, EXTR_OVERWRITE);
65                $this->stderr($code . $name . $message."\n");
66                $this->_stop(1);
67        }
68
69/**
70 * Convenience method to display a 404 page.
71 *
72 * @param array $params Parameters (url, message)
73 * @access public
74 */
75        function error404($params) {
76                extract($params, EXTR_OVERWRITE);
77                $this->error(array(
78                        'code' => '404',
79                        'name' => 'Not found',
80                        'message' => sprintf(__("The requested address %s was not found on this server.", true), $url, $message)
81                ));
82                $this->_stop(1);
83        }
84
85/**
86 * Renders the Missing Controller web page.
87 *
88 * @param array $params Parameters (className)
89 * @access public
90 */
91        function missingController($params) {
92                extract($params, EXTR_OVERWRITE);
93                $controllerName = str_replace('Controller', '', $className);
94                $this->stderr(sprintf(__("Missing Controller '%s'", true), $controllerName));
95                $this->_stop(1);
96        }
97
98/**
99 * Renders the Missing Action web page.
100 *
101 * @param array $params Parameters (action, className)
102 * @access public
103 */
104        function missingAction($params) {
105                extract($params, EXTR_OVERWRITE);
106                $this->stderr(sprintf(__("Missing Method '%s' in '%s'", true), $action, $className));
107                $this->_stop(1);
108        }
109
110/**
111 * Renders the Private Action web page.
112 *
113 * @param array $params Parameters (action, className)
114 * @access public
115 */
116        function privateAction($params) {
117                extract($params, EXTR_OVERWRITE);
118                $this->stderr(sprintf(__("Trying to access private method '%s' in '%s'", true), $action, $className));
119                $this->_stop(1);
120        }
121
122/**
123 * Renders the Missing Table web page.
124 *
125 * @param array $params Parameters (table, className)
126 * @access public
127 */
128        function missingTable($params) {
129                extract($params, EXTR_OVERWRITE);
130                $this->stderr(sprintf(__("Missing database table '%s' for model '%s'", true), $table, $className));
131                $this->_stop(1);
132        }
133
134/**
135 * Renders the Missing Database web page.
136 *
137 * @param array $params Parameters
138 * @access public
139 */
140        function missingDatabase($params = array()) {
141                $this->stderr(__("Missing Database", true));
142                $this->_stop(1);
143        }
144
145/**
146 * Renders the Missing View web page.
147 *
148 * @param array $params Parameters (file, action, className)
149 * @access public
150 */
151        function missingView($params) {
152                extract($params, EXTR_OVERWRITE);
153                $this->stderr(sprintf(__("Missing View '%s' for '%s' in '%s'", true), $file, $action, $className));
154                $this->_stop(1);
155        }
156
157/**
158 * Renders the Missing Layout web page.
159 *
160 * @param array $params Parameters (file)
161 * @access public
162 */
163        function missingLayout($params) {
164                extract($params, EXTR_OVERWRITE);
165                $this->stderr(sprintf(__("Missing Layout '%s'", true), $file));
166                $this->_stop(1);
167        }
168
169/**
170 * Renders the Database Connection web page.
171 *
172 * @param array $params Parameters
173 * @access public
174 */
175        function missingConnection($params) {
176                extract($params, EXTR_OVERWRITE);
177                $this->stderr(__("Missing Database Connection. Try 'cake bake'", true));
178                $this->_stop(1);
179        }
180
181/**
182 * Renders the Missing Helper file web page.
183 *
184 * @param array $params Parameters (file, helper)
185 * @access public
186 */
187        function missingHelperFile($params) {
188                extract($params, EXTR_OVERWRITE);
189                $this->stderr(sprintf(__("Missing Helper file '%s' for '%s'", true), $file, Inflector::camelize($helper)));
190                $this->_stop(1);
191        }
192
193/**
194 * Renders the Missing Helper class web page.
195 *
196 * @param array $params Parameters (file, helper)
197 * @access public
198 */
199        function missingHelperClass($params) {
200                extract($params, EXTR_OVERWRITE);
201                $this->stderr(sprintf(__("Missing Helper class '%s' in '%s'", true), Inflector::camelize($helper), $file));
202                $this->_stop(1);
203        }
204
205/**
206 * Renders the Missing Component file web page.
207 *
208 * @param array $params Parameters (file, component)
209 * @access public
210 */
211        function missingComponentFile($params) {
212                extract($params, EXTR_OVERWRITE);
213                $this->stderr(sprintf(__("Missing Component file '%s' for '%s'", true), $file, Inflector::camelize($component)));
214                $this->_stop(1);
215        }
216
217/**
218 * Renders the Missing Component class web page.
219 *
220 * @param array $params Parameters (file, component)
221 * @access public
222 */
223        function missingComponentClass($params) {
224                extract($params, EXTR_OVERWRITE);
225                $this->stderr(sprintf(__("Missing Component class '%s' in '%s'", true), Inflector::camelize($component), $file));
226                $this->_stop(1);
227        }
228
229/**
230 * Renders the Missing Model class web page.
231 *
232 * @param array $params Parameters (className)
233 * @access public
234 */
235        function missingModel($params) {
236                extract($params, EXTR_OVERWRITE);
237                $this->stderr(sprintf(__("Missing model '%s'", true), $className));
238                $this->_stop(1);
239        }
240
241/**
242 * Outputs to the stdout filehandle.
243 *
244 * @param string $string String to output.
245 * @param boolean $newline If true, the outputs gets an added newline.
246 * @access public
247 */
248        function stdout($string, $newline = true) {
249                if ($newline) {
250                        fwrite($this->stdout, $string . "\n");
251                } else {
252                        fwrite($this->stdout, $string);
253                }
254        }
255
256/**
257 * Outputs to the stderr filehandle.
258 *
259 * @param string $string Error text to output.
260 * @access public
261 */
262        function stderr($string) {
263                fwrite($this->stderr, "Error: ". $string . "\n");
264        }
265}
Note: See TracBrowser for help on using the repository browser.