Exception.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @since 3.0.0
  11. * @license https://opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Core\Exception;
  14. use RuntimeException;
  15. /**
  16. * Base class that all CakePHP Exceptions extend.
  17. *
  18. * @method int getCode()
  19. */
  20. class Exception extends RuntimeException
  21. {
  22. /**
  23. * Array of attributes that are passed in from the constructor, and
  24. * made available in the view when a development error is displayed.
  25. *
  26. * @var array
  27. */
  28. protected $_attributes = [];
  29. /**
  30. * Template string that has attributes sprintf()'ed into it.
  31. *
  32. * @var string
  33. */
  34. protected $_messageTemplate = '';
  35. /**
  36. * Array of headers to be passed to Cake\Http\Response::header()
  37. *
  38. * @var array|null
  39. */
  40. protected $_responseHeaders;
  41. /**
  42. * Default exception code
  43. *
  44. * @var int
  45. */
  46. protected $_defaultCode = 500;
  47. /**
  48. * Constructor.
  49. *
  50. * Allows you to create exceptions that are treated as framework errors and disabled
  51. * when debug mode is off.
  52. *
  53. * @param string|array $message Either the string of the error message, or an array of attributes
  54. * that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
  55. * @param int|null $code The code of the error, is also the HTTP status code for the error.
  56. * @param \Exception|null $previous the previous exception.
  57. */
  58. public function __construct($message = '', $code = null, $previous = null)
  59. {
  60. if ($code === null) {
  61. $code = $this->_defaultCode;
  62. }
  63. if (is_array($message)) {
  64. $this->_attributes = $message;
  65. $message = vsprintf($this->_messageTemplate, $message);
  66. }
  67. parent::__construct($message, $code, $previous);
  68. }
  69. /**
  70. * Get the passed in attributes
  71. *
  72. * @return array
  73. */
  74. public function getAttributes()
  75. {
  76. return $this->_attributes;
  77. }
  78. /**
  79. * Get/set the response header to be used
  80. *
  81. * See also Cake\Http\Response::withHeader()
  82. *
  83. * @param string|array|null $header An array of header strings or a single header string
  84. * - an associative array of "header name" => "header value"
  85. * - an array of string headers is also accepted (deprecated)
  86. * @param string|null $value The header value.
  87. * @return array|null
  88. */
  89. public function responseHeader($header = null, $value = null)
  90. {
  91. if ($header === null) {
  92. return $this->_responseHeaders;
  93. }
  94. if (is_array($header)) {
  95. if (isset($header[0])) {
  96. deprecationWarning(
  97. 'Passing a list string headers to Exception::responseHeader() is deprecated. ' .
  98. 'Use an associative array instead.'
  99. );
  100. }
  101. return $this->_responseHeaders = $header;
  102. }
  103. $this->_responseHeaders = [$header => $value];
  104. }
  105. }