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