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