/libraries/legacy/exception/exception.php

https://github.com/pjwiseman/joomla-cms · PHP · 389 lines · 150 code · 54 blank · 185 comment · 19 complexity · bfb993220dc8d8b5c53a36cb6bcb3b36 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Legacy
  4. * @subpackage Exception
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla! Exception object.
  12. *
  13. * @since 11.1
  14. * @deprecated 12.1 (Platform) & 4.0 (CMS)
  15. */
  16. class JException extends Exception
  17. {
  18. /**
  19. * @var string Error level.
  20. * @since 11.1
  21. */
  22. protected $level = null;
  23. /**
  24. * @var string Error code.
  25. * @since 11.1
  26. */
  27. protected $code = null;
  28. /**
  29. * @var string Error message.
  30. * @since 11.1
  31. */
  32. protected $message = null;
  33. /**
  34. * Additional info about the error relevant to the developer,
  35. * for example, if a database connect fails, the dsn used
  36. *
  37. * @var string
  38. * @since 11.1
  39. */
  40. protected $info = '';
  41. /**
  42. * Name of the file the error occurred in [Available if backtrace is enabled]
  43. *
  44. * @var string
  45. * @since 11.1
  46. */
  47. protected $file = null;
  48. /**
  49. * Line number the error occurred in [Available if backtrace is enabled]
  50. *
  51. * @var int
  52. * @since 11.1
  53. */
  54. protected $line = 0;
  55. /**
  56. * Name of the method the error occurred in [Available if backtrace is enabled]
  57. *
  58. * @var string
  59. * @since 11.1
  60. */
  61. protected $function = null;
  62. /**
  63. * Name of the class the error occurred in [Available if backtrace is enabled]
  64. *
  65. * @var string
  66. * @since 11.1
  67. */
  68. protected $class = null;
  69. /**
  70. * @var string Error type.
  71. * @since 11.1
  72. */
  73. protected $type = null;
  74. /**
  75. * Arguments recieved by the method the error occurred in [Available if backtrace is enabled]
  76. *
  77. * @var array
  78. * @since 11.1
  79. */
  80. protected $args = array();
  81. /**
  82. * @var mixed Backtrace information.
  83. * @since 11.1
  84. */
  85. protected $backtrace = null;
  86. /**
  87. * Constructor
  88. * - used to set up the error with all needed error details.
  89. *
  90. * @param string $msg The error message
  91. * @param string $code The error code from the application
  92. * @param integer $level The error level (use the PHP constants E_ALL, E_NOTICE etc.).
  93. * @param string $info Optional: The additional error information.
  94. * @param boolean $backtrace True if backtrace information is to be collected
  95. *
  96. * @since 11.1
  97. *
  98. * @deprecated 12.1
  99. */
  100. public function __construct($msg, $code = 0, $level = null, $info = null, $backtrace = false)
  101. {
  102. JLog::add('JException is deprecated.', JLog::WARNING, 'deprecated');
  103. $this->level = $level;
  104. $this->code = $code;
  105. $this->message = $msg;
  106. if ($info != null)
  107. {
  108. $this->info = $info;
  109. }
  110. if ($backtrace && function_exists('debug_backtrace'))
  111. {
  112. $this->backtrace = debug_backtrace();
  113. for ($i = count($this->backtrace) - 1; $i >= 0; --$i)
  114. {
  115. ++$i;
  116. if (isset($this->backtrace[$i]['file']))
  117. {
  118. $this->file = $this->backtrace[$i]['file'];
  119. }
  120. if (isset($this->backtrace[$i]['line']))
  121. {
  122. $this->line = $this->backtrace[$i]['line'];
  123. }
  124. if (isset($this->backtrace[$i]['class']))
  125. {
  126. $this->class = $this->backtrace[$i]['class'];
  127. }
  128. if (isset($this->backtrace[$i]['function']))
  129. {
  130. $this->function = $this->backtrace[$i]['function'];
  131. }
  132. if (isset($this->backtrace[$i]['type']))
  133. {
  134. $this->type = $this->backtrace[$i]['type'];
  135. }
  136. $this->args = false;
  137. if (isset($this->backtrace[$i]['args']))
  138. {
  139. $this->args = $this->backtrace[$i]['args'];
  140. }
  141. break;
  142. }
  143. }
  144. // Store exception for debugging purposes!
  145. JError::addToStack($this);
  146. parent::__construct($msg, (int) $code);
  147. }
  148. /**
  149. * Returns to error message
  150. *
  151. * @return string Error message
  152. *
  153. * @since 11.1
  154. *
  155. * @deprecated 12.1
  156. */
  157. public function __toString()
  158. {
  159. JLog::add('JException::__toString is deprecated.', JLog::WARNING, 'deprecated');
  160. return $this->message;
  161. }
  162. /**
  163. * Returns to error message
  164. *
  165. * @return string Error message
  166. *
  167. * @since 11.1
  168. * @deprecated 12.1
  169. */
  170. public function toString()
  171. {
  172. JLog::add('JException::toString is deprecated.', JLog::WARNING, 'deprecated');
  173. return (string) $this;
  174. }
  175. /**
  176. * Returns a property of the object or the default value if the property is not set.
  177. *
  178. * @param string $property The name of the property
  179. * @param mixed $default The default value
  180. *
  181. * @return mixed The value of the property or null
  182. *
  183. * @deprecated 12.1
  184. * @see JException::getProperties()
  185. * @since 11.1
  186. */
  187. public function get($property, $default = null)
  188. {
  189. JLog::add('JException::get is deprecated.', JLog::WARNING, 'deprecated');
  190. if (isset($this->$property))
  191. {
  192. return $this->$property;
  193. }
  194. return $default;
  195. }
  196. /**
  197. * Returns an associative array of object properties
  198. *
  199. * @param boolean $public If true, returns only the public properties
  200. *
  201. * @return array Object properties
  202. *
  203. * @deprecated 12.1
  204. * @see JException::get()
  205. * @since 11.1
  206. */
  207. public function getProperties($public = true)
  208. {
  209. JLog::add('JException::getProperties is deprecated.', JLog::WARNING, 'deprecated');
  210. $vars = get_object_vars($this);
  211. if ($public)
  212. {
  213. foreach ($vars as $key => $value)
  214. {
  215. if ('_' == substr($key, 0, 1))
  216. {
  217. unset($vars[$key]);
  218. }
  219. }
  220. }
  221. return $vars;
  222. }
  223. /**
  224. * Get the most recent error message
  225. *
  226. * @param integer $i Option error index
  227. * @param boolean $toString Indicates if JError objects should return their error message
  228. *
  229. * @return string Error message
  230. *
  231. * @since 11.1
  232. *
  233. * @deprecated 12.1
  234. */
  235. public function getError($i = null, $toString = true)
  236. {
  237. JLog::add('JException::getError is deprecated.', JLog::WARNING, 'deprecated');
  238. // Find the error
  239. if ($i === null)
  240. {
  241. // Default, return the last message
  242. $error = end($this->_errors);
  243. }
  244. elseif (!array_key_exists($i, $this->_errors))
  245. {
  246. // If $i has been specified but does not exist, return false
  247. return false;
  248. }
  249. else
  250. {
  251. $error = $this->_errors[$i];
  252. }
  253. // Check if only the string is requested
  254. if ($error instanceof Exception && $toString)
  255. {
  256. return (string) $error;
  257. }
  258. return $error;
  259. }
  260. /**
  261. * Return all errors, if any
  262. *
  263. * @return array Array of error messages or JErrors
  264. *
  265. * @since 11.1
  266. *
  267. * @deprecated 12.1
  268. */
  269. public function getErrors()
  270. {
  271. JLog::add('JException::getErrors is deprecated.', JLog::WARNING, 'deprecated');
  272. return $this->_errors;
  273. }
  274. /**
  275. * Modifies a property of the object, creating it if it does not already exist.
  276. *
  277. * @param string $property The name of the property
  278. * @param mixed $value The value of the property to set
  279. *
  280. * @return mixed Previous value of the property
  281. *
  282. * @deprecated 12.1
  283. * @see JException::setProperties()
  284. * @since 11.1
  285. */
  286. public function set($property, $value = null)
  287. {
  288. JLog::add('JException::set is deprecated.', JLog::WARNING, 'deprecated');
  289. $previous = isset($this->$property) ? $this->$property : null;
  290. $this->$property = $value;
  291. return $previous;
  292. }
  293. /**
  294. * Set the object properties based on a named array/hash
  295. *
  296. * @param mixed $properties Either and associative array or another object
  297. *
  298. * @return boolean
  299. *
  300. * @deprecated 12.1
  301. * @see JException::set()
  302. * @since 11.1
  303. */
  304. public function setProperties($properties)
  305. {
  306. JLog::add('JException::setProperties is deprecated.', JLog::WARNING, 'deprecated');
  307. // Cast to an array
  308. $properties = (array) $properties;
  309. if (is_array($properties))
  310. {
  311. foreach ($properties as $k => $v)
  312. {
  313. $this->$k = $v;
  314. }
  315. return true;
  316. }
  317. return false;
  318. }
  319. /**
  320. * Add an error message
  321. *
  322. * @param string $error Error message
  323. *
  324. * @return void
  325. *
  326. * @since 11.1
  327. *
  328. * @deprecated 12.1
  329. */
  330. public function setError($error)
  331. {
  332. JLog::add('JException::setErrors is deprecated.', JLog::WARNING, 'deprecated');
  333. array_push($this->_errors, $error);
  334. }
  335. }