PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/error.php

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