Exception.php 2.9 KB

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