PageRenderTime 54ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/libs/error.php

http://skygames.googlecode.com/
PHP | 380 lines | 199 code | 20 blank | 161 comment | 13 complexity | 79fddbb73cf17150dfff2e2bfc420ab2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: error.php 7861 2008-11-11 00:14:38Z mark_story $ */
  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: 7861 $
  23. * @modifiedby $LastChangedBy: mark_story $
  24. * @lastmodified $Date: 2008-11-10 18:14:38 -0600 (Mon, 10 Nov 2008) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. App::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. */
  36. class 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. */
  69. class 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. if ($__previousError != array($method, $messages)) {
  87. $__previousError = array($method, $messages);
  88. $this->controller =& new CakeErrorController();
  89. } else {
  90. $this->controller =& new Controller();
  91. $this->controller->viewPath = 'errors';
  92. }
  93. $options = array('escape' => false);
  94. $messages = Sanitize::clean($messages, $options);
  95. if (!isset($messages[0])) {
  96. $messages = array($messages);
  97. }
  98. if (method_exists($this->controller, 'apperror')) {
  99. return $this->controller->appError($method, $messages);
  100. }
  101. if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
  102. $method = 'error';
  103. }
  104. if ($method !== 'error') {
  105. if (Configure::read() == 0) {
  106. $method = 'error404';
  107. if (isset($code) && $code == 500) {
  108. $method = 'error500';
  109. }
  110. }
  111. }
  112. $this->dispatchMethod($method, $messages);
  113. $this->_stop();
  114. }
  115. /**
  116. * Displays an error page (e.g. 404 Not found).
  117. *
  118. * @param array $params Parameters for controller
  119. * @access public
  120. */
  121. function error($params) {
  122. extract($params, EXTR_OVERWRITE);
  123. $this->controller->set(array(
  124. 'code' => $code,
  125. 'name' => $name,
  126. 'message' => $message,
  127. 'title' => $code . ' ' . $name
  128. ));
  129. $this->_outputMessage('error404');
  130. }
  131. /**
  132. * Convenience method to display a 404 page.
  133. *
  134. * @param array $params Parameters for controller
  135. * @access public
  136. */
  137. function error404($params) {
  138. extract($params, EXTR_OVERWRITE);
  139. if (!isset($url)) {
  140. $url = $this->controller->here;
  141. }
  142. $url = Router::normalize($url);
  143. header("HTTP/1.0 404 Not Found");
  144. $this->controller->set(array(
  145. 'code' => '404',
  146. 'name' => __('Not Found', true),
  147. 'message' => $url,
  148. 'base' => $this->controller->base
  149. ));
  150. $this->_outputMessage('error404');
  151. }
  152. /**
  153. * Renders the Missing Controller web page.
  154. *
  155. * @param array $params Parameters for controller
  156. * @access public
  157. */
  158. function missingController($params) {
  159. extract($params, EXTR_OVERWRITE);
  160. $controllerName = str_replace('Controller', '', $className);
  161. $this->controller->set(array(
  162. 'controller' => $className,
  163. 'controllerName' => $controllerName,
  164. 'title' => __('Missing Controller', true)
  165. ));
  166. $this->_outputMessage('missingController');
  167. }
  168. /**
  169. * Renders the Missing Action web page.
  170. *
  171. * @param array $params Parameters for controller
  172. * @access public
  173. */
  174. function missingAction($params) {
  175. extract($params, EXTR_OVERWRITE);
  176. $controllerName = str_replace('Controller', '', $className);
  177. $this->controller->set(array(
  178. 'controller' => $className,
  179. 'controllerName' => $controllerName,
  180. 'action' => $action,
  181. 'title' => __('Missing Method in Controller', true)
  182. ));
  183. $this->_outputMessage('missingAction');
  184. }
  185. /**
  186. * Renders the Private Action web page.
  187. *
  188. * @param array $params Parameters for controller
  189. * @access public
  190. */
  191. function privateAction($params) {
  192. extract($params, EXTR_OVERWRITE);
  193. $this->controller->set(array(
  194. 'controller' => $className,
  195. 'action' => $action,
  196. 'title' => __('Trying to access private method in class', true)
  197. ));
  198. $this->_outputMessage('privateAction');
  199. }
  200. /**
  201. * Renders the Missing Table web page.
  202. *
  203. * @param array $params Parameters for controller
  204. * @access public
  205. */
  206. function missingTable($params) {
  207. extract($params, EXTR_OVERWRITE);
  208. $this->controller->set(array(
  209. 'model' => $className,
  210. 'table' => $table,
  211. 'title' => __('Missing Database Table', true)
  212. ));
  213. $this->_outputMessage('missingTable');
  214. }
  215. /**
  216. * Renders the Missing Database web page.
  217. *
  218. * @param array $params Parameters for controller
  219. * @access public
  220. */
  221. function missingDatabase($params = array()) {
  222. extract($params, EXTR_OVERWRITE);
  223. $this->controller->set(array(
  224. 'title' => __('Scaffold Missing Database Connection', true)
  225. ));
  226. $this->_outputMessage('missingScaffolddb');
  227. }
  228. /**
  229. * Renders the Missing View web page.
  230. *
  231. * @param array $params Parameters for controller
  232. * @access public
  233. */
  234. function missingView($params) {
  235. extract($params, EXTR_OVERWRITE);
  236. $this->controller->set(array(
  237. 'controller' => $className,
  238. 'action' => $action,
  239. 'file' => $file,
  240. 'title' => __('Missing View', true)
  241. ));
  242. $this->_outputMessage('missingView');
  243. }
  244. /**
  245. * Renders the Missing Layout web page.
  246. *
  247. * @param array $params Parameters for controller
  248. * @access public
  249. */
  250. function missingLayout($params) {
  251. extract($params, EXTR_OVERWRITE);
  252. $this->controller->layout = 'default';
  253. $this->controller->set(array(
  254. 'file' => $file,
  255. 'title' => __('Missing Layout', true)
  256. ));
  257. $this->_outputMessage('missingLayout');
  258. }
  259. /**
  260. * Renders the Database Connection web page.
  261. *
  262. * @param array $params Parameters for controller
  263. * @access public
  264. */
  265. function missingConnection($params) {
  266. extract($params, EXTR_OVERWRITE);
  267. $this->controller->set(array(
  268. 'model' => $className,
  269. 'title' => __('Missing Database Connection', true)
  270. ));
  271. $this->_outputMessage('missingConnection');
  272. }
  273. /**
  274. * Renders the Missing Helper file web page.
  275. *
  276. * @param array $params Parameters for controller
  277. * @access public
  278. */
  279. function missingHelperFile($params) {
  280. extract($params, EXTR_OVERWRITE);
  281. $this->controller->set(array(
  282. 'helperClass' => Inflector::camelize($helper) . "Helper",
  283. 'file' => $file,
  284. 'title' => __('Missing Helper File', true)
  285. ));
  286. $this->_outputMessage('missingHelperFile');
  287. }
  288. /**
  289. * Renders the Missing Helper class web page.
  290. *
  291. * @param array $params Parameters for controller
  292. * @access public
  293. */
  294. function missingHelperClass($params) {
  295. extract($params, EXTR_OVERWRITE);
  296. $this->controller->set(array(
  297. 'helperClass' => Inflector::camelize($helper) . "Helper",
  298. 'file' => $file,
  299. 'title' => __('Missing Helper Class', true)
  300. ));
  301. $this->_outputMessage('missingHelperClass');
  302. }
  303. /**
  304. * Renders the Missing Component file web page.
  305. *
  306. * @param array $params Parameters for controller
  307. * @access public
  308. */
  309. function missingComponentFile($params) {
  310. extract($params, EXTR_OVERWRITE);
  311. $this->controller->set(array(
  312. 'controller' => $className,
  313. 'component' => $component,
  314. 'file' => $file,
  315. 'title' => __('Missing Component File', true)
  316. ));
  317. $this->_outputMessage('missingComponentFile');
  318. }
  319. /**
  320. * Renders the Missing Component class web page.
  321. *
  322. * @param array $params Parameters for controller
  323. * @access public
  324. */
  325. function missingComponentClass($params) {
  326. extract($params, EXTR_OVERWRITE);
  327. $this->controller->set(array(
  328. 'controller' => $className,
  329. 'component' => $component,
  330. 'file' => $file,
  331. 'title' => __('Missing Component Class', true)
  332. ));
  333. $this->_outputMessage('missingComponentClass');
  334. }
  335. /**
  336. * Renders the Missing Model class web page.
  337. *
  338. * @param unknown_type $params Parameters for controller
  339. * @access public
  340. */
  341. function missingModel($params) {
  342. extract($params, EXTR_OVERWRITE);
  343. $this->controller->set(array(
  344. 'model' => $className,
  345. 'title' => __('Missing Model', true)
  346. ));
  347. $this->_outputMessage('missingModel');
  348. }
  349. /**
  350. * Output message
  351. *
  352. * @access protected
  353. */
  354. function _outputMessage($template) {
  355. $this->controller->render($template);
  356. $this->controller->afterFilter();
  357. echo $this->controller->output;
  358. }
  359. }
  360. ?>