Exception.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class Exception extends RuntimeException
  19. {
  20. /**
  21. * Array of attributes that are passed in from the constructor, and
  22. * made available in the view when a development error is displayed.
  23. *
  24. * @var array
  25. */
  26. protected $_attributes = [];
  27. /**
  28. * Template string that has attributes sprintf()'ed into it.
  29. *
  30. * @var string
  31. */
  32. protected $_messageTemplate = '';
  33. /**
  34. * Array of headers to be passed to Cake\Http\Response::header()
  35. *
  36. * @var array|null
  37. */
  38. protected $_responseHeaders;
  39. /**
  40. * Default exception code
  41. *
  42. * @var int
  43. */
  44. protected $_defaultCode = 500;
  45. /**
  46. * Constructor.
  47. *
  48. * Allows you to create exceptions that are treated as framework errors and disabled
  49. * when debug = 0.
  50. *
  51. * @param string|array $message Either the string of the error message, or an array of attributes
  52. * that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
  53. * @param int|null $code The code of the error, is also the HTTP status code for the error.
  54. * @param \Exception|null $previous the previous exception.
  55. */
  56. public function __construct($message = '', $code = null, $previous = null)
  57. {
  58. if ($code === null) {
  59. $code = $this->_defaultCode;
  60. }
  61. if (is_array($message)) {
  62. $this->_attributes = $message;
  63. $message = vsprintf($this->_messageTemplate, $message);
  64. }
  65. parent::__construct($message, $code, $previous);
  66. }
  67. /**
  68. * Get the passed in attributes
  69. *
  70. * @return array
  71. */
  72. public function getAttributes()
  73. {
  74. return $this->_attributes;
  75. }
  76. /**
  77. * Get/set the response header to be used
  78. *
  79. * See also Cake\Http\Response::withHeader()
  80. *
  81. * @param string|array|null $header An array of header strings or a single header string
  82. * - an associative array of "header name" => "header value"
  83. * - an array of string headers is also accepted (deprecated)
  84. * @param string|null $value The header value.
  85. * @return array
  86. */
  87. public function responseHeader($header = null, $value = null)
  88. {
  89. if ($header === null) {
  90. return $this->_responseHeaders;
  91. }
  92. if (is_array($header)) {
  93. if (isset($header[0])) {
  94. deprecationWarning(
  95. 'Passing a list string headers to Exception::responseHeader() is deprecated. ' .
  96. 'Use an associative array instead.'
  97. );
  98. }
  99. return $this->_responseHeaders = $header;
  100. }
  101. $this->_responseHeaders = [$header => $value];
  102. }
  103. }