@@ -13,180 +13,180 @@ |
||
13 | 13 | |
14 | 14 | class Timestamps |
15 | 15 | { |
16 | - private $contents; |
|
17 | - |
|
18 | - /** |
|
19 | - * @param string $file path to the phar file to use |
|
20 | - */ |
|
21 | - public function __construct($file) |
|
22 | - { |
|
23 | - $this->contents = file_get_contents($file); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Updates each file's unix timestamps in the PHAR |
|
28 | - * |
|
29 | - * The PHAR signature can then be produced in a reproducible manner. |
|
30 | - * |
|
31 | - * @param int|\DateTimeInterface|string $timestamp Date string or DateTime or unix timestamp to use |
|
32 | - */ |
|
33 | - public function updateTimestamps($timestamp = null) |
|
34 | - { |
|
35 | - if ($timestamp instanceof \DateTime || $timestamp instanceof \DateTimeInterface) { |
|
36 | - $timestamp = $timestamp->getTimestamp(); |
|
37 | - } elseif (is_string($timestamp)) { |
|
38 | - $timestamp = strtotime($timestamp); |
|
39 | - } elseif (!is_int($timestamp)) { |
|
40 | - $timestamp = strtotime('1984-12-24T00:00:00Z'); |
|
41 | - } |
|
42 | - |
|
43 | - // detect manifest offset / end of stub |
|
44 | - if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) { |
|
45 | - throw new \RuntimeException('Could not detect the stub\'s end in the phar'); |
|
46 | - } |
|
47 | - |
|
48 | - // set starting position and skip past manifest length |
|
49 | - $pos = $match[0][1] + strlen($match[0][0]); |
|
50 | - $stubEnd = $pos + $this->readUint($pos, 4); |
|
51 | - $pos += 4; |
|
52 | - |
|
53 | - $numFiles = $this->readUint($pos, 4); |
|
54 | - $pos += 4; |
|
55 | - |
|
56 | - // skip API version (YOLO) |
|
57 | - $pos += 2; |
|
58 | - |
|
59 | - // skip PHAR flags |
|
60 | - $pos += 4; |
|
61 | - |
|
62 | - $aliasLength = $this->readUint($pos, 4); |
|
63 | - $pos += 4 + $aliasLength; |
|
64 | - |
|
65 | - $metadataLength = $this->readUint($pos, 4); |
|
66 | - $pos += 4 + $metadataLength; |
|
67 | - |
|
68 | - while ($pos < $stubEnd) { |
|
69 | - $filenameLength = $this->readUint($pos, 4); |
|
70 | - $pos += 4 + $filenameLength; |
|
71 | - |
|
72 | - // skip filesize |
|
73 | - $pos += 4; |
|
74 | - |
|
75 | - // update timestamp to a fixed value |
|
76 | - $this->contents = substr_replace($this->contents, pack('L', $timestamp), $pos, 4); |
|
77 | - |
|
78 | - // skip timestamp, compressed file size, crc32 checksum and file flags |
|
79 | - $pos += 4*4; |
|
80 | - |
|
81 | - $metadataLength = $this->readUint($pos, 4); |
|
82 | - $pos += 4 + $metadataLength; |
|
83 | - |
|
84 | - $numFiles--; |
|
85 | - } |
|
86 | - |
|
87 | - if ($numFiles !== 0) { |
|
88 | - throw new \LogicException('All files were not processed, something must have gone wrong'); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Saves the updated phar file, optionally with an updated signature. |
|
94 | - * |
|
95 | - * @param string $path |
|
96 | - * @param int $signatureAlgo One of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512 |
|
97 | - * @return bool |
|
98 | - */ |
|
99 | - public function save($path, $signatureAlgo) |
|
100 | - { |
|
101 | - $pos = $this->determineSignatureBegin(); |
|
102 | - |
|
103 | - $algos = array( |
|
104 | - \Phar::MD5 => 'md5', |
|
105 | - \Phar::SHA1 => 'sha1', |
|
106 | - \Phar::SHA256 => 'sha256', |
|
107 | - \Phar::SHA512 => 'sha512', |
|
108 | - ); |
|
109 | - |
|
110 | - if (!isset($algos[$signatureAlgo])) { |
|
111 | - throw new \UnexpectedValueException('Invalid hash algorithm given: '.$signatureAlgo.' expected one of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512'); |
|
112 | - } |
|
113 | - $algo = $algos[$signatureAlgo]; |
|
114 | - |
|
115 | - // re-sign phar |
|
116 | - // signature |
|
117 | - $signature = hash($algo, substr($this->contents, 0, $pos), true) |
|
118 | - // sig type |
|
119 | - . pack('L', $signatureAlgo) |
|
120 | - // ohai Greg & Marcus |
|
121 | - . 'GBMB'; |
|
122 | - |
|
123 | - $this->contents = substr($this->contents, 0, $pos) . $signature; |
|
124 | - |
|
125 | - return file_put_contents($path, $this->contents); |
|
126 | - } |
|
127 | - |
|
128 | - private function readUint($pos, $bytes) |
|
129 | - { |
|
130 | - $res = unpack('V', substr($this->contents, $pos, $bytes)); |
|
131 | - |
|
132 | - return $res[1]; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Determine the beginning of the signature. |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - private function determineSignatureBegin() |
|
141 | - { |
|
142 | - // detect signature position |
|
143 | - if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) { |
|
144 | - throw new \RuntimeException('Could not detect the stub\'s end in the phar'); |
|
145 | - } |
|
16 | + private $contents; |
|
17 | + |
|
18 | + /** |
|
19 | + * @param string $file path to the phar file to use |
|
20 | + */ |
|
21 | + public function __construct($file) |
|
22 | + { |
|
23 | + $this->contents = file_get_contents($file); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Updates each file's unix timestamps in the PHAR |
|
28 | + * |
|
29 | + * The PHAR signature can then be produced in a reproducible manner. |
|
30 | + * |
|
31 | + * @param int|\DateTimeInterface|string $timestamp Date string or DateTime or unix timestamp to use |
|
32 | + */ |
|
33 | + public function updateTimestamps($timestamp = null) |
|
34 | + { |
|
35 | + if ($timestamp instanceof \DateTime || $timestamp instanceof \DateTimeInterface) { |
|
36 | + $timestamp = $timestamp->getTimestamp(); |
|
37 | + } elseif (is_string($timestamp)) { |
|
38 | + $timestamp = strtotime($timestamp); |
|
39 | + } elseif (!is_int($timestamp)) { |
|
40 | + $timestamp = strtotime('1984-12-24T00:00:00Z'); |
|
41 | + } |
|
42 | + |
|
43 | + // detect manifest offset / end of stub |
|
44 | + if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) { |
|
45 | + throw new \RuntimeException('Could not detect the stub\'s end in the phar'); |
|
46 | + } |
|
47 | + |
|
48 | + // set starting position and skip past manifest length |
|
49 | + $pos = $match[0][1] + strlen($match[0][0]); |
|
50 | + $stubEnd = $pos + $this->readUint($pos, 4); |
|
51 | + $pos += 4; |
|
52 | + |
|
53 | + $numFiles = $this->readUint($pos, 4); |
|
54 | + $pos += 4; |
|
55 | + |
|
56 | + // skip API version (YOLO) |
|
57 | + $pos += 2; |
|
58 | + |
|
59 | + // skip PHAR flags |
|
60 | + $pos += 4; |
|
61 | + |
|
62 | + $aliasLength = $this->readUint($pos, 4); |
|
63 | + $pos += 4 + $aliasLength; |
|
64 | + |
|
65 | + $metadataLength = $this->readUint($pos, 4); |
|
66 | + $pos += 4 + $metadataLength; |
|
67 | + |
|
68 | + while ($pos < $stubEnd) { |
|
69 | + $filenameLength = $this->readUint($pos, 4); |
|
70 | + $pos += 4 + $filenameLength; |
|
71 | + |
|
72 | + // skip filesize |
|
73 | + $pos += 4; |
|
74 | + |
|
75 | + // update timestamp to a fixed value |
|
76 | + $this->contents = substr_replace($this->contents, pack('L', $timestamp), $pos, 4); |
|
77 | + |
|
78 | + // skip timestamp, compressed file size, crc32 checksum and file flags |
|
79 | + $pos += 4*4; |
|
80 | + |
|
81 | + $metadataLength = $this->readUint($pos, 4); |
|
82 | + $pos += 4 + $metadataLength; |
|
83 | + |
|
84 | + $numFiles--; |
|
85 | + } |
|
86 | + |
|
87 | + if ($numFiles !== 0) { |
|
88 | + throw new \LogicException('All files were not processed, something must have gone wrong'); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Saves the updated phar file, optionally with an updated signature. |
|
94 | + * |
|
95 | + * @param string $path |
|
96 | + * @param int $signatureAlgo One of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512 |
|
97 | + * @return bool |
|
98 | + */ |
|
99 | + public function save($path, $signatureAlgo) |
|
100 | + { |
|
101 | + $pos = $this->determineSignatureBegin(); |
|
102 | + |
|
103 | + $algos = array( |
|
104 | + \Phar::MD5 => 'md5', |
|
105 | + \Phar::SHA1 => 'sha1', |
|
106 | + \Phar::SHA256 => 'sha256', |
|
107 | + \Phar::SHA512 => 'sha512', |
|
108 | + ); |
|
109 | + |
|
110 | + if (!isset($algos[$signatureAlgo])) { |
|
111 | + throw new \UnexpectedValueException('Invalid hash algorithm given: '.$signatureAlgo.' expected one of Phar::MD5, Phar::SHA1, Phar::SHA256 or Phar::SHA512'); |
|
112 | + } |
|
113 | + $algo = $algos[$signatureAlgo]; |
|
114 | + |
|
115 | + // re-sign phar |
|
116 | + // signature |
|
117 | + $signature = hash($algo, substr($this->contents, 0, $pos), true) |
|
118 | + // sig type |
|
119 | + . pack('L', $signatureAlgo) |
|
120 | + // ohai Greg & Marcus |
|
121 | + . 'GBMB'; |
|
122 | + |
|
123 | + $this->contents = substr($this->contents, 0, $pos) . $signature; |
|
124 | + |
|
125 | + return file_put_contents($path, $this->contents); |
|
126 | + } |
|
127 | + |
|
128 | + private function readUint($pos, $bytes) |
|
129 | + { |
|
130 | + $res = unpack('V', substr($this->contents, $pos, $bytes)); |
|
131 | + |
|
132 | + return $res[1]; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Determine the beginning of the signature. |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + private function determineSignatureBegin() |
|
141 | + { |
|
142 | + // detect signature position |
|
143 | + if (!preg_match('{__HALT_COMPILER\(\);(?: +\?>)?\r?\n}', $this->contents, $match, PREG_OFFSET_CAPTURE)) { |
|
144 | + throw new \RuntimeException('Could not detect the stub\'s end in the phar'); |
|
145 | + } |
|
146 | 146 | |
147 | - // set starting position and skip past manifest length |
|
148 | - $pos = $match[0][1] + strlen($match[0][0]); |
|
149 | - $manifestEnd = $pos + 4 + $this->readUint($pos, 4); |
|
147 | + // set starting position and skip past manifest length |
|
148 | + $pos = $match[0][1] + strlen($match[0][0]); |
|
149 | + $manifestEnd = $pos + 4 + $this->readUint($pos, 4); |
|
150 | 150 | |
151 | - $pos += 4; |
|
152 | - $numFiles = $this->readUint($pos, 4); |
|
151 | + $pos += 4; |
|
152 | + $numFiles = $this->readUint($pos, 4); |
|
153 | 153 | |
154 | - $pos += 4; |
|
154 | + $pos += 4; |
|
155 | 155 | |
156 | - // skip API version (YOLO) |
|
157 | - $pos += 2; |
|
156 | + // skip API version (YOLO) |
|
157 | + $pos += 2; |
|
158 | 158 | |
159 | - // skip PHAR flags |
|
160 | - $pos += 4; |
|
159 | + // skip PHAR flags |
|
160 | + $pos += 4; |
|
161 | 161 | |
162 | - $aliasLength = $this->readUint($pos, 4); |
|
163 | - $pos += 4 + $aliasLength; |
|
164 | - |
|
165 | - $metadataLength = $this->readUint($pos, 4); |
|
166 | - $pos += 4 + $metadataLength; |
|
167 | - |
|
168 | - $compressedSizes = 0; |
|
169 | - while (($numFiles > 0) && ($pos < $manifestEnd - 24)) { |
|
170 | - $filenameLength = $this->readUint($pos, 4); |
|
171 | - $pos += 4 + $filenameLength; |
|
172 | - |
|
173 | - // skip filesize and timestamp |
|
174 | - $pos += 2*4; |
|
175 | - |
|
176 | - $compressedSizes += $this->readUint($pos, 4); |
|
177 | - // skip compressed file size, crc32 checksum and file flags |
|
178 | - $pos += 3*4; |
|
179 | - |
|
180 | - $metadataLength = $this->readUint($pos, 4); |
|
181 | - $pos += 4 + $metadataLength; |
|
182 | - |
|
183 | - $numFiles--; |
|
184 | - } |
|
185 | - |
|
186 | - if ($numFiles !== 0) { |
|
187 | - throw new \LogicException('All files were not processed, something must have gone wrong'); |
|
188 | - } |
|
189 | - |
|
190 | - return $manifestEnd + $compressedSizes; |
|
191 | - } |
|
162 | + $aliasLength = $this->readUint($pos, 4); |
|
163 | + $pos += 4 + $aliasLength; |
|
164 | + |
|
165 | + $metadataLength = $this->readUint($pos, 4); |
|
166 | + $pos += 4 + $metadataLength; |
|
167 | + |
|
168 | + $compressedSizes = 0; |
|
169 | + while (($numFiles > 0) && ($pos < $manifestEnd - 24)) { |
|
170 | + $filenameLength = $this->readUint($pos, 4); |
|
171 | + $pos += 4 + $filenameLength; |
|
172 | + |
|
173 | + // skip filesize and timestamp |
|
174 | + $pos += 2*4; |
|
175 | + |
|
176 | + $compressedSizes += $this->readUint($pos, 4); |
|
177 | + // skip compressed file size, crc32 checksum and file flags |
|
178 | + $pos += 3*4; |
|
179 | + |
|
180 | + $metadataLength = $this->readUint($pos, 4); |
|
181 | + $pos += 4 + $metadataLength; |
|
182 | + |
|
183 | + $numFiles--; |
|
184 | + } |
|
185 | + |
|
186 | + if ($numFiles !== 0) { |
|
187 | + throw new \LogicException('All files were not processed, something must have gone wrong'); |
|
188 | + } |
|
189 | + |
|
190 | + return $manifestEnd + $compressedSizes; |
|
191 | + } |
|
192 | 192 | } |
@@ -13,98 +13,98 @@ |
||
13 | 13 | |
14 | 14 | class Linter |
15 | 15 | { |
16 | - /** |
|
17 | - * Lints all php files inside a given phar with the current PHP version |
|
18 | - * |
|
19 | - * @param string $path Phar file path |
|
20 | - */ |
|
21 | - public static function lint($path) |
|
22 | - { |
|
23 | - $php = defined('PHP_BINARY') ? PHP_BINARY : 'php'; |
|
24 | - |
|
25 | - if ($isWindows = defined('PHP_WINDOWS_VERSION_BUILD')) { |
|
26 | - $tmpFile = @tempnam(sys_get_temp_dir(), ''); |
|
27 | - |
|
28 | - if (!$tmpFile || !is_writable($tmpFile)) { |
|
29 | - throw new \RuntimeException('Unable to create temp file'); |
|
30 | - } |
|
31 | - |
|
32 | - $php = self::escapeWindowsPath($php); |
|
33 | - $tmpFile = self::escapeWindowsPath($tmpFile); |
|
34 | - |
|
35 | - // PHP 8 encloses the command in double-quotes |
|
36 | - if (PHP_VERSION_ID >= 80000) { |
|
37 | - $format = '%s -l %s'; |
|
38 | - } else { |
|
39 | - $format = '"%s -l %s"'; |
|
40 | - } |
|
41 | - |
|
42 | - $command = sprintf($format, $php, $tmpFile); |
|
43 | - } else { |
|
44 | - $command = "'".$php."' -l"; |
|
45 | - } |
|
46 | - |
|
47 | - $descriptorspec = array( |
|
48 | - 0 => array('pipe', 'r'), |
|
49 | - 1 => array('pipe', 'w'), |
|
50 | - 2 => array('pipe', 'w') |
|
51 | - ); |
|
52 | - |
|
53 | - foreach (new \RecursiveIteratorIterator(new \Phar($path)) as $file) { |
|
54 | - if ($file->isDir()) { |
|
55 | - continue; |
|
56 | - } |
|
57 | - if (substr($file, -4) === '.php') { |
|
58 | - $filename = (string) $file; |
|
59 | - |
|
60 | - if ($isWindows) { |
|
61 | - file_put_contents($tmpFile, file_get_contents($filename)); |
|
62 | - } |
|
63 | - |
|
64 | - $process = proc_open($command, $descriptorspec, $pipes); |
|
65 | - if (is_resource($process)) { |
|
66 | - if (!$isWindows) { |
|
67 | - fwrite($pipes[0], file_get_contents($filename)); |
|
68 | - } |
|
69 | - fclose($pipes[0]); |
|
70 | - |
|
71 | - $stdout = stream_get_contents($pipes[1]); |
|
72 | - fclose($pipes[1]); |
|
73 | - $stderr = stream_get_contents($pipes[2]); |
|
74 | - fclose($pipes[2]); |
|
75 | - |
|
76 | - $exitCode = proc_close($process); |
|
77 | - |
|
78 | - if ($exitCode !== 0) { |
|
79 | - if ($isWindows) { |
|
80 | - $stderr = str_replace($tmpFile, $filename, $stderr); |
|
81 | - } |
|
82 | - throw new \UnexpectedValueException('Failed linting '.$file.': '.$stderr); |
|
83 | - } |
|
84 | - } else { |
|
85 | - throw new \RuntimeException('Could not start linter process'); |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - if ($isWindows) { |
|
91 | - @unlink($tmpFile); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Escapes a Windows file path |
|
97 | - * |
|
98 | - * @param string $path |
|
99 | - * @return string The escaped path |
|
100 | - */ |
|
101 | - private static function escapeWindowsPath($path) |
|
102 | - { |
|
103 | - // Quote if path contains spaces or brackets |
|
104 | - if (strpbrk($path, " ()") !== false) { |
|
105 | - $path = '"'.$path.'"'; |
|
106 | - } |
|
107 | - |
|
108 | - return $path; |
|
109 | - } |
|
16 | + /** |
|
17 | + * Lints all php files inside a given phar with the current PHP version |
|
18 | + * |
|
19 | + * @param string $path Phar file path |
|
20 | + */ |
|
21 | + public static function lint($path) |
|
22 | + { |
|
23 | + $php = defined('PHP_BINARY') ? PHP_BINARY : 'php'; |
|
24 | + |
|
25 | + if ($isWindows = defined('PHP_WINDOWS_VERSION_BUILD')) { |
|
26 | + $tmpFile = @tempnam(sys_get_temp_dir(), ''); |
|
27 | + |
|
28 | + if (!$tmpFile || !is_writable($tmpFile)) { |
|
29 | + throw new \RuntimeException('Unable to create temp file'); |
|
30 | + } |
|
31 | + |
|
32 | + $php = self::escapeWindowsPath($php); |
|
33 | + $tmpFile = self::escapeWindowsPath($tmpFile); |
|
34 | + |
|
35 | + // PHP 8 encloses the command in double-quotes |
|
36 | + if (PHP_VERSION_ID >= 80000) { |
|
37 | + $format = '%s -l %s'; |
|
38 | + } else { |
|
39 | + $format = '"%s -l %s"'; |
|
40 | + } |
|
41 | + |
|
42 | + $command = sprintf($format, $php, $tmpFile); |
|
43 | + } else { |
|
44 | + $command = "'".$php."' -l"; |
|
45 | + } |
|
46 | + |
|
47 | + $descriptorspec = array( |
|
48 | + 0 => array('pipe', 'r'), |
|
49 | + 1 => array('pipe', 'w'), |
|
50 | + 2 => array('pipe', 'w') |
|
51 | + ); |
|
52 | + |
|
53 | + foreach (new \RecursiveIteratorIterator(new \Phar($path)) as $file) { |
|
54 | + if ($file->isDir()) { |
|
55 | + continue; |
|
56 | + } |
|
57 | + if (substr($file, -4) === '.php') { |
|
58 | + $filename = (string) $file; |
|
59 | + |
|
60 | + if ($isWindows) { |
|
61 | + file_put_contents($tmpFile, file_get_contents($filename)); |
|
62 | + } |
|
63 | + |
|
64 | + $process = proc_open($command, $descriptorspec, $pipes); |
|
65 | + if (is_resource($process)) { |
|
66 | + if (!$isWindows) { |
|
67 | + fwrite($pipes[0], file_get_contents($filename)); |
|
68 | + } |
|
69 | + fclose($pipes[0]); |
|
70 | + |
|
71 | + $stdout = stream_get_contents($pipes[1]); |
|
72 | + fclose($pipes[1]); |
|
73 | + $stderr = stream_get_contents($pipes[2]); |
|
74 | + fclose($pipes[2]); |
|
75 | + |
|
76 | + $exitCode = proc_close($process); |
|
77 | + |
|
78 | + if ($exitCode !== 0) { |
|
79 | + if ($isWindows) { |
|
80 | + $stderr = str_replace($tmpFile, $filename, $stderr); |
|
81 | + } |
|
82 | + throw new \UnexpectedValueException('Failed linting '.$file.': '.$stderr); |
|
83 | + } |
|
84 | + } else { |
|
85 | + throw new \RuntimeException('Could not start linter process'); |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + if ($isWindows) { |
|
91 | + @unlink($tmpFile); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Escapes a Windows file path |
|
97 | + * |
|
98 | + * @param string $path |
|
99 | + * @return string The escaped path |
|
100 | + */ |
|
101 | + private static function escapeWindowsPath($path) |
|
102 | + { |
|
103 | + // Quote if path contains spaces or brackets |
|
104 | + if (strpbrk($path, " ()") !== false) { |
|
105 | + $path = '"'.$path.'"'; |
|
106 | + } |
|
107 | + |
|
108 | + return $path; |
|
109 | + } |
|
110 | 110 | } |
@@ -27,481 +27,481 @@ |
||
27 | 27 | */ |
28 | 28 | class JsonParser |
29 | 29 | { |
30 | - const DETECT_KEY_CONFLICTS = 1; |
|
31 | - const ALLOW_DUPLICATE_KEYS = 2; |
|
32 | - const PARSE_TO_ASSOC = 4; |
|
33 | - |
|
34 | - private $lexer; |
|
35 | - |
|
36 | - private $flags; |
|
37 | - private $stack; |
|
38 | - private $vstack; // semantic value stack |
|
39 | - private $lstack; // location stack |
|
40 | - |
|
41 | - /** |
|
42 | - * @phpstan-var array<string, int> |
|
43 | - */ |
|
44 | - private $symbols = array( |
|
45 | - 'error' => 2, |
|
46 | - 'JSONString' => 3, |
|
47 | - 'STRING' => 4, |
|
48 | - 'JSONNumber' => 5, |
|
49 | - 'NUMBER' => 6, |
|
50 | - 'JSONNullLiteral' => 7, |
|
51 | - 'NULL' => 8, |
|
52 | - 'JSONBooleanLiteral' => 9, |
|
53 | - 'TRUE' => 10, |
|
54 | - 'FALSE' => 11, |
|
55 | - 'JSONText' => 12, |
|
56 | - 'JSONValue' => 13, |
|
57 | - 'EOF' => 14, |
|
58 | - 'JSONObject' => 15, |
|
59 | - 'JSONArray' => 16, |
|
60 | - '{' => 17, |
|
61 | - '}' => 18, |
|
62 | - 'JSONMemberList' => 19, |
|
63 | - 'JSONMember' => 20, |
|
64 | - ':' => 21, |
|
65 | - ',' => 22, |
|
66 | - '[' => 23, |
|
67 | - ']' => 24, |
|
68 | - 'JSONElementList' => 25, |
|
69 | - '$accept' => 0, |
|
70 | - '$end' => 1, |
|
71 | - ); |
|
72 | - |
|
73 | - /** |
|
74 | - * @phpstan-var array<int, string> |
|
75 | - */ |
|
76 | - private $terminals_ = array( |
|
77 | - 2 => "error", |
|
78 | - 4 => "STRING", |
|
79 | - 6 => "NUMBER", |
|
80 | - 8 => "NULL", |
|
81 | - 10 => "TRUE", |
|
82 | - 11 => "FALSE", |
|
83 | - 14 => "EOF", |
|
84 | - 17 => "{", |
|
85 | - 18 => "}", |
|
86 | - 21 => ":", |
|
87 | - 22 => ",", |
|
88 | - 23 => "[", |
|
89 | - 24 => "]", |
|
90 | - ); |
|
91 | - |
|
92 | - private $productions_ = array( |
|
93 | - 0, |
|
94 | - array(3, 1), |
|
95 | - array(5, 1), |
|
96 | - array(7, 1), |
|
97 | - array(9, 1), |
|
98 | - array(9, 1), |
|
99 | - array(12, 2), |
|
100 | - array(13, 1), |
|
101 | - array(13, 1), |
|
102 | - array(13, 1), |
|
103 | - array(13, 1), |
|
104 | - array(13, 1), |
|
105 | - array(13, 1), |
|
106 | - array(15, 2), |
|
107 | - array(15, 3), |
|
108 | - array(20, 3), |
|
109 | - array(19, 1), |
|
110 | - array(19, 3), |
|
111 | - array(16, 2), |
|
112 | - array(16, 3), |
|
113 | - array(25, 1), |
|
114 | - array(25, 3) |
|
115 | - ); |
|
116 | - |
|
117 | - private $table = array(array(3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 12 => 1, 13 => 2, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 1 => array(3)), array( 14 => array(1,16)), array( 14 => array(2,7), 18 => array(2,7), 22 => array(2,7), 24 => array(2,7)), array( 14 => array(2,8), 18 => array(2,8), 22 => array(2,8), 24 => array(2,8)), array( 14 => array(2,9), 18 => array(2,9), 22 => array(2,9), 24 => array(2,9)), array( 14 => array(2,10), 18 => array(2,10), 22 => array(2,10), 24 => array(2,10)), array( 14 => array(2,11), 18 => array(2,11), 22 => array(2,11), 24 => array(2,11)), array( 14 => array(2,12), 18 => array(2,12), 22 => array(2,12), 24 => array(2,12)), array( 14 => array(2,3), 18 => array(2,3), 22 => array(2,3), 24 => array(2,3)), array( 14 => array(2,4), 18 => array(2,4), 22 => array(2,4), 24 => array(2,4)), array( 14 => array(2,5), 18 => array(2,5), 22 => array(2,5), 24 => array(2,5)), array( 14 => array(2,1), 18 => array(2,1), 21 => array(2,1), 22 => array(2,1), 24 => array(2,1)), array( 14 => array(2,2), 18 => array(2,2), 22 => array(2,2), 24 => array(2,2)), array( 3 => 20, 4 => array(1,12), 18 => array(1,17), 19 => 18, 20 => 19 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 23, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15), 24 => array(1,21), 25 => 22 ), array( 1 => array(2,6)), array( 14 => array(2,13), 18 => array(2,13), 22 => array(2,13), 24 => array(2,13)), array( 18 => array(1,24), 22 => array(1,25)), array( 18 => array(2,16), 22 => array(2,16)), array( 21 => array(1,26)), array( 14 => array(2,18), 18 => array(2,18), 22 => array(2,18), 24 => array(2,18)), array( 22 => array(1,28), 24 => array(1,27)), array( 22 => array(2,20), 24 => array(2,20)), array( 14 => array(2,14), 18 => array(2,14), 22 => array(2,14), 24 => array(2,14)), array( 3 => 20, 4 => array(1,12), 20 => 29 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 30, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 14 => array(2,19), 18 => array(2,19), 22 => array(2,19), 24 => array(2,19)), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 31, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 18 => array(2,17), 22 => array(2,17)), array( 18 => array(2,15), 22 => array(2,15)), array( 22 => array(2,21), 24 => array(2,21)), |
|
118 | - ); |
|
119 | - |
|
120 | - private $defaultActions = array( |
|
121 | - 16 => array(2, 6) |
|
122 | - ); |
|
123 | - |
|
124 | - /** |
|
125 | - * @param string $input JSON string |
|
126 | - * @param int $flags Bitmask of parse/lint options (see constants of this class) |
|
127 | - * @return null|ParsingException null if no error is found, a ParsingException containing all details otherwise |
|
128 | - */ |
|
129 | - public function lint($input, $flags = 0) |
|
130 | - { |
|
131 | - try { |
|
132 | - $this->parse($input, $flags); |
|
133 | - } catch (ParsingException $e) { |
|
134 | - return $e; |
|
135 | - } |
|
136 | - return null; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @param string $input JSON string |
|
141 | - * @param int $flags Bitmask of parse/lint options (see constants of this class) |
|
142 | - * @return mixed |
|
143 | - * @throws ParsingException |
|
144 | - */ |
|
145 | - public function parse($input, $flags = 0) |
|
146 | - { |
|
147 | - $this->failOnBOM($input); |
|
148 | - |
|
149 | - $this->flags = $flags; |
|
150 | - |
|
151 | - $this->stack = array(0); |
|
152 | - $this->vstack = array(null); |
|
153 | - $this->lstack = array(); |
|
154 | - |
|
155 | - $yytext = ''; |
|
156 | - $yylineno = 0; |
|
157 | - $yyleng = 0; |
|
158 | - $recovering = 0; |
|
159 | - $TERROR = 2; |
|
160 | - $EOF = 1; |
|
161 | - |
|
162 | - $this->lexer = new Lexer(); |
|
163 | - $this->lexer->setInput($input); |
|
164 | - |
|
165 | - $yyloc = $this->lexer->yylloc; |
|
166 | - $this->lstack[] = $yyloc; |
|
167 | - |
|
168 | - $symbol = null; |
|
169 | - $preErrorSymbol = null; |
|
170 | - $state = null; |
|
171 | - $action = null; |
|
172 | - $a = null; |
|
173 | - $r = null; |
|
174 | - $yyval = new stdClass; |
|
175 | - $p = null; |
|
176 | - $len = null; |
|
177 | - $newState = null; |
|
178 | - $expected = null; |
|
179 | - $errStr = null; |
|
180 | - |
|
181 | - while (true) { |
|
182 | - // retrieve state number from top of stack |
|
183 | - $state = $this->stack[\count($this->stack)-1]; |
|
184 | - |
|
185 | - // use default actions if available |
|
186 | - if (isset($this->defaultActions[$state])) { |
|
187 | - $action = $this->defaultActions[$state]; |
|
188 | - } else { |
|
189 | - if ($symbol == null) { |
|
190 | - $symbol = $this->lex(); |
|
191 | - } |
|
192 | - // read action for current state and first input |
|
193 | - $action = isset($this->table[$state][$symbol]) ? $this->table[$state][$symbol] : false; |
|
194 | - } |
|
195 | - |
|
196 | - // handle parse error |
|
197 | - if (!$action || !$action[0]) { |
|
198 | - if (!$recovering) { |
|
199 | - // Report error |
|
200 | - $expected = array(); |
|
201 | - foreach ($this->table[$state] as $p => $ignore) { |
|
202 | - if (isset($this->terminals_[$p]) && $p > 2) { |
|
203 | - $expected[] = "'" . $this->terminals_[$p] . "'"; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - $message = null; |
|
208 | - if (\in_array("'STRING'", $expected) && \in_array(substr($this->lexer->match, 0, 1), array('"', "'"))) { |
|
209 | - $message = "Invalid string"; |
|
210 | - if ("'" === substr($this->lexer->match, 0, 1)) { |
|
211 | - $message .= ", it appears you used single quotes instead of double quotes"; |
|
212 | - } elseif (preg_match('{".+?(\\\\[^"bfnrt/\\\\u](...)?)}', $this->lexer->getFullUpcomingInput(), $match)) { |
|
213 | - $message .= ", it appears you have an unescaped backslash at: ".$match[1]; |
|
214 | - } elseif (preg_match('{"(?:[^"]+|\\\\")*$}m', $this->lexer->getFullUpcomingInput())) { |
|
215 | - $message .= ", it appears you forgot to terminate a string, or attempted to write a multiline string which is invalid"; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
220 | - $errStr .= $this->lexer->showPosition() . "\n"; |
|
221 | - if ($message) { |
|
222 | - $errStr .= $message; |
|
223 | - } else { |
|
224 | - $errStr .= (\count($expected) > 1) ? "Expected one of: " : "Expected: "; |
|
225 | - $errStr .= implode(', ', $expected); |
|
226 | - } |
|
227 | - |
|
228 | - if (',' === substr(trim($this->lexer->getPastInput()), -1)) { |
|
229 | - $errStr .= " - It appears you have an extra trailing comma"; |
|
230 | - } |
|
231 | - |
|
232 | - $this->parseError($errStr, array( |
|
233 | - 'text' => $this->lexer->match, |
|
234 | - 'token' => !empty($this->terminals_[$symbol]) ? $this->terminals_[$symbol] : $symbol, |
|
235 | - 'line' => $this->lexer->yylineno, |
|
236 | - 'loc' => $yyloc, |
|
237 | - 'expected' => $expected, |
|
238 | - )); |
|
239 | - } |
|
240 | - |
|
241 | - // just recovered from another error |
|
242 | - if ($recovering == 3) { |
|
243 | - if ($symbol == $EOF) { |
|
244 | - throw new ParsingException($errStr ?: 'Parsing halted.'); |
|
245 | - } |
|
246 | - |
|
247 | - // discard current lookahead and grab another |
|
248 | - $yyleng = $this->lexer->yyleng; |
|
249 | - $yytext = $this->lexer->yytext; |
|
250 | - $yylineno = $this->lexer->yylineno; |
|
251 | - $yyloc = $this->lexer->yylloc; |
|
252 | - $symbol = $this->lex(); |
|
253 | - } |
|
254 | - |
|
255 | - // try to recover from error |
|
256 | - while (true) { |
|
257 | - // check for error recovery rule in this state |
|
258 | - if (\array_key_exists($TERROR, $this->table[$state])) { |
|
259 | - break; |
|
260 | - } |
|
261 | - if ($state == 0) { |
|
262 | - throw new ParsingException($errStr ?: 'Parsing halted.'); |
|
263 | - } |
|
264 | - $this->popStack(1); |
|
265 | - $state = $this->stack[\count($this->stack)-1]; |
|
266 | - } |
|
267 | - |
|
268 | - $preErrorSymbol = $symbol; // save the lookahead token |
|
269 | - $symbol = $TERROR; // insert generic error symbol as new lookahead |
|
270 | - $state = $this->stack[\count($this->stack)-1]; |
|
271 | - $action = isset($this->table[$state][$TERROR]) ? $this->table[$state][$TERROR] : false; |
|
272 | - $recovering = 3; // allow 3 real symbols to be shifted before reporting a new error |
|
273 | - } |
|
274 | - |
|
275 | - // this shouldn't happen, unless resolve defaults are off |
|
276 | - if (\is_array($action[0]) && \count($action) > 1) { |
|
277 | - throw new ParsingException('Parse Error: multiple actions possible at state: ' . $state . ', token: ' . $symbol); |
|
278 | - } |
|
279 | - |
|
280 | - switch ($action[0]) { |
|
281 | - case 1: // shift |
|
282 | - $this->stack[] = $symbol; |
|
283 | - $this->vstack[] = $this->lexer->yytext; |
|
284 | - $this->lstack[] = $this->lexer->yylloc; |
|
285 | - $this->stack[] = $action[1]; // push state |
|
286 | - $symbol = null; |
|
287 | - if (!$preErrorSymbol) { // normal execution/no error |
|
288 | - $yyleng = $this->lexer->yyleng; |
|
289 | - $yytext = $this->lexer->yytext; |
|
290 | - $yylineno = $this->lexer->yylineno; |
|
291 | - $yyloc = $this->lexer->yylloc; |
|
292 | - if ($recovering > 0) { |
|
293 | - $recovering--; |
|
294 | - } |
|
295 | - } else { // error just occurred, resume old lookahead f/ before error |
|
296 | - $symbol = $preErrorSymbol; |
|
297 | - $preErrorSymbol = null; |
|
298 | - } |
|
299 | - break; |
|
300 | - |
|
301 | - case 2: // reduce |
|
302 | - $len = $this->productions_[$action[1]][1]; |
|
303 | - |
|
304 | - // perform semantic action |
|
305 | - $yyval->token = $this->vstack[\count($this->vstack) - $len]; // default to $$ = $1 |
|
306 | - // default location, uses first token for firsts, last for lasts |
|
307 | - $yyval->store = array( // _$ = store |
|
308 | - 'first_line' => $this->lstack[\count($this->lstack) - ($len ?: 1)]['first_line'], |
|
309 | - 'last_line' => $this->lstack[\count($this->lstack) - 1]['last_line'], |
|
310 | - 'first_column' => $this->lstack[\count($this->lstack) - ($len ?: 1)]['first_column'], |
|
311 | - 'last_column' => $this->lstack[\count($this->lstack) - 1]['last_column'], |
|
312 | - ); |
|
313 | - $r = $this->performAction($yyval, $yytext, $yyleng, $yylineno, $action[1], $this->vstack); |
|
314 | - |
|
315 | - if (!$r instanceof Undefined) { |
|
316 | - return $r; |
|
317 | - } |
|
318 | - |
|
319 | - if ($len) { |
|
320 | - $this->popStack($len); |
|
321 | - } |
|
322 | - |
|
323 | - $this->stack[] = $this->productions_[$action[1]][0]; // push nonterminal (reduce) |
|
324 | - $this->vstack[] = $yyval->token; |
|
325 | - $this->lstack[] = $yyval->store; |
|
326 | - $newState = $this->table[$this->stack[\count($this->stack)-2]][$this->stack[\count($this->stack)-1]]; |
|
327 | - $this->stack[] = $newState; |
|
328 | - break; |
|
329 | - |
|
330 | - case 3: // accept |
|
331 | - |
|
332 | - return true; |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - protected function parseError($str, $hash) |
|
338 | - { |
|
339 | - throw new ParsingException($str, $hash); |
|
340 | - } |
|
341 | - |
|
342 | - // $$ = $tokens // needs to be passed by ref? |
|
343 | - // $ = $token |
|
344 | - // _$ removed, useless? |
|
345 | - private function performAction(stdClass $yyval, $yytext, $yyleng, $yylineno, $yystate, &$tokens) |
|
346 | - { |
|
347 | - // $0 = $len |
|
348 | - $len = \count($tokens) - 1; |
|
349 | - switch ($yystate) { |
|
350 | - case 1: |
|
351 | - $yytext = preg_replace_callback('{(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4})}', array($this, 'stringInterpolation'), $yytext); |
|
352 | - $yyval->token = $yytext; |
|
353 | - break; |
|
354 | - case 2: |
|
355 | - if (strpos($yytext, 'e') !== false || strpos($yytext, 'E') !== false) { |
|
356 | - $yyval->token = \floatval($yytext); |
|
357 | - } else { |
|
358 | - $yyval->token = strpos($yytext, '.') === false ? \intval($yytext) : \floatval($yytext); |
|
359 | - } |
|
360 | - break; |
|
361 | - case 3: |
|
362 | - $yyval->token = null; |
|
363 | - break; |
|
364 | - case 4: |
|
365 | - $yyval->token = true; |
|
366 | - break; |
|
367 | - case 5: |
|
368 | - $yyval->token = false; |
|
369 | - break; |
|
370 | - case 6: |
|
371 | - return $yyval->token = $tokens[$len-1]; |
|
372 | - case 13: |
|
373 | - if ($this->flags & self::PARSE_TO_ASSOC) { |
|
374 | - $yyval->token = array(); |
|
375 | - } else { |
|
376 | - $yyval->token = new stdClass; |
|
377 | - } |
|
378 | - break; |
|
379 | - case 14: |
|
380 | - $yyval->token = $tokens[$len-1]; |
|
381 | - break; |
|
382 | - case 15: |
|
383 | - $yyval->token = array($tokens[$len-2], $tokens[$len]); |
|
384 | - break; |
|
385 | - case 16: |
|
386 | - if (PHP_VERSION_ID < 70100) { |
|
387 | - $property = $tokens[$len][0] === '' ? '_empty_' : $tokens[$len][0]; |
|
388 | - } else { |
|
389 | - $property = $tokens[$len][0]; |
|
390 | - } |
|
391 | - if ($this->flags & self::PARSE_TO_ASSOC) { |
|
392 | - $yyval->token = array(); |
|
393 | - $yyval->token[$property] = $tokens[$len][1]; |
|
394 | - } else { |
|
395 | - $yyval->token = new stdClass; |
|
396 | - $yyval->token->$property = $tokens[$len][1]; |
|
397 | - } |
|
398 | - break; |
|
399 | - case 17: |
|
400 | - if ($this->flags & self::PARSE_TO_ASSOC) { |
|
401 | - $yyval->token =& $tokens[$len-2]; |
|
402 | - $key = $tokens[$len][0]; |
|
403 | - if (($this->flags & self::DETECT_KEY_CONFLICTS) && isset($tokens[$len-2][$key])) { |
|
404 | - $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
405 | - $errStr .= $this->lexer->showPosition() . "\n"; |
|
406 | - $errStr .= "Duplicate key: ".$tokens[$len][0]; |
|
407 | - throw new DuplicateKeyException($errStr, $tokens[$len][0], array('line' => $yylineno+1)); |
|
408 | - } elseif (($this->flags & self::ALLOW_DUPLICATE_KEYS) && isset($tokens[$len-2][$key])) { |
|
409 | - $duplicateCount = 1; |
|
410 | - do { |
|
411 | - $duplicateKey = $key . '.' . $duplicateCount++; |
|
412 | - } while (isset($tokens[$len-2][$duplicateKey])); |
|
413 | - $key = $duplicateKey; |
|
414 | - } |
|
415 | - $tokens[$len-2][$key] = $tokens[$len][1]; |
|
416 | - } else { |
|
417 | - $yyval->token = $tokens[$len-2]; |
|
418 | - if (PHP_VERSION_ID < 70100) { |
|
419 | - $key = $tokens[$len][0] === '' ? '_empty_' : $tokens[$len][0]; |
|
420 | - } else { |
|
421 | - $key = $tokens[$len][0]; |
|
422 | - } |
|
423 | - if (($this->flags & self::DETECT_KEY_CONFLICTS) && isset($tokens[$len-2]->{$key})) { |
|
424 | - $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
425 | - $errStr .= $this->lexer->showPosition() . "\n"; |
|
426 | - $errStr .= "Duplicate key: ".$tokens[$len][0]; |
|
427 | - throw new DuplicateKeyException($errStr, $tokens[$len][0], array('line' => $yylineno+1)); |
|
428 | - } elseif (($this->flags & self::ALLOW_DUPLICATE_KEYS) && isset($tokens[$len-2]->{$key})) { |
|
429 | - $duplicateCount = 1; |
|
430 | - do { |
|
431 | - $duplicateKey = $key . '.' . $duplicateCount++; |
|
432 | - } while (isset($tokens[$len-2]->$duplicateKey)); |
|
433 | - $key = $duplicateKey; |
|
434 | - } |
|
435 | - $tokens[$len-2]->$key = $tokens[$len][1]; |
|
436 | - } |
|
437 | - break; |
|
438 | - case 18: |
|
439 | - $yyval->token = array(); |
|
440 | - break; |
|
441 | - case 19: |
|
442 | - $yyval->token = $tokens[$len-1]; |
|
443 | - break; |
|
444 | - case 20: |
|
445 | - $yyval->token = array($tokens[$len]); |
|
446 | - break; |
|
447 | - case 21: |
|
448 | - $tokens[$len-2][] = $tokens[$len]; |
|
449 | - $yyval->token = $tokens[$len-2]; |
|
450 | - break; |
|
451 | - } |
|
452 | - |
|
453 | - return new Undefined(); |
|
454 | - } |
|
455 | - |
|
456 | - private function stringInterpolation($match) |
|
457 | - { |
|
458 | - switch ($match[0]) { |
|
459 | - case '\\\\': |
|
460 | - return '\\'; |
|
461 | - case '\"': |
|
462 | - return '"'; |
|
463 | - case '\b': |
|
464 | - return \chr(8); |
|
465 | - case '\f': |
|
466 | - return \chr(12); |
|
467 | - case '\n': |
|
468 | - return "\n"; |
|
469 | - case '\r': |
|
470 | - return "\r"; |
|
471 | - case '\t': |
|
472 | - return "\t"; |
|
473 | - case '\/': |
|
474 | - return "/"; |
|
475 | - default: |
|
476 | - return html_entity_decode('&#x'.ltrim(substr($match[0], 2), '0').';', ENT_QUOTES, 'UTF-8'); |
|
477 | - } |
|
478 | - } |
|
479 | - |
|
480 | - private function popStack($n) |
|
481 | - { |
|
482 | - $this->stack = \array_slice($this->stack, 0, - (2 * $n)); |
|
483 | - $this->vstack = \array_slice($this->vstack, 0, - $n); |
|
484 | - $this->lstack = \array_slice($this->lstack, 0, - $n); |
|
485 | - } |
|
486 | - |
|
487 | - private function lex() |
|
488 | - { |
|
489 | - $token = $this->lexer->lex() ?: 1; // $end = 1 |
|
490 | - // if token isn't its numeric value, convert |
|
491 | - if (!is_numeric($token)) { |
|
492 | - $token = isset($this->symbols[$token]) ? $this->symbols[$token] : $token; |
|
493 | - } |
|
494 | - |
|
495 | - return $token; |
|
496 | - } |
|
497 | - |
|
498 | - private function failOnBOM($input) |
|
499 | - { |
|
500 | - // UTF-8 ByteOrderMark sequence |
|
501 | - $bom = "\xEF\xBB\xBF"; |
|
502 | - |
|
503 | - if (substr($input, 0, 3) === $bom) { |
|
504 | - $this->parseError("BOM detected, make sure your input does not include a Unicode Byte-Order-Mark", array()); |
|
505 | - } |
|
506 | - } |
|
30 | + const DETECT_KEY_CONFLICTS = 1; |
|
31 | + const ALLOW_DUPLICATE_KEYS = 2; |
|
32 | + const PARSE_TO_ASSOC = 4; |
|
33 | + |
|
34 | + private $lexer; |
|
35 | + |
|
36 | + private $flags; |
|
37 | + private $stack; |
|
38 | + private $vstack; // semantic value stack |
|
39 | + private $lstack; // location stack |
|
40 | + |
|
41 | + /** |
|
42 | + * @phpstan-var array<string, int> |
|
43 | + */ |
|
44 | + private $symbols = array( |
|
45 | + 'error' => 2, |
|
46 | + 'JSONString' => 3, |
|
47 | + 'STRING' => 4, |
|
48 | + 'JSONNumber' => 5, |
|
49 | + 'NUMBER' => 6, |
|
50 | + 'JSONNullLiteral' => 7, |
|
51 | + 'NULL' => 8, |
|
52 | + 'JSONBooleanLiteral' => 9, |
|
53 | + 'TRUE' => 10, |
|
54 | + 'FALSE' => 11, |
|
55 | + 'JSONText' => 12, |
|
56 | + 'JSONValue' => 13, |
|
57 | + 'EOF' => 14, |
|
58 | + 'JSONObject' => 15, |
|
59 | + 'JSONArray' => 16, |
|
60 | + '{' => 17, |
|
61 | + '}' => 18, |
|
62 | + 'JSONMemberList' => 19, |
|
63 | + 'JSONMember' => 20, |
|
64 | + ':' => 21, |
|
65 | + ',' => 22, |
|
66 | + '[' => 23, |
|
67 | + ']' => 24, |
|
68 | + 'JSONElementList' => 25, |
|
69 | + '$accept' => 0, |
|
70 | + '$end' => 1, |
|
71 | + ); |
|
72 | + |
|
73 | + /** |
|
74 | + * @phpstan-var array<int, string> |
|
75 | + */ |
|
76 | + private $terminals_ = array( |
|
77 | + 2 => "error", |
|
78 | + 4 => "STRING", |
|
79 | + 6 => "NUMBER", |
|
80 | + 8 => "NULL", |
|
81 | + 10 => "TRUE", |
|
82 | + 11 => "FALSE", |
|
83 | + 14 => "EOF", |
|
84 | + 17 => "{", |
|
85 | + 18 => "}", |
|
86 | + 21 => ":", |
|
87 | + 22 => ",", |
|
88 | + 23 => "[", |
|
89 | + 24 => "]", |
|
90 | + ); |
|
91 | + |
|
92 | + private $productions_ = array( |
|
93 | + 0, |
|
94 | + array(3, 1), |
|
95 | + array(5, 1), |
|
96 | + array(7, 1), |
|
97 | + array(9, 1), |
|
98 | + array(9, 1), |
|
99 | + array(12, 2), |
|
100 | + array(13, 1), |
|
101 | + array(13, 1), |
|
102 | + array(13, 1), |
|
103 | + array(13, 1), |
|
104 | + array(13, 1), |
|
105 | + array(13, 1), |
|
106 | + array(15, 2), |
|
107 | + array(15, 3), |
|
108 | + array(20, 3), |
|
109 | + array(19, 1), |
|
110 | + array(19, 3), |
|
111 | + array(16, 2), |
|
112 | + array(16, 3), |
|
113 | + array(25, 1), |
|
114 | + array(25, 3) |
|
115 | + ); |
|
116 | + |
|
117 | + private $table = array(array(3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 12 => 1, 13 => 2, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 1 => array(3)), array( 14 => array(1,16)), array( 14 => array(2,7), 18 => array(2,7), 22 => array(2,7), 24 => array(2,7)), array( 14 => array(2,8), 18 => array(2,8), 22 => array(2,8), 24 => array(2,8)), array( 14 => array(2,9), 18 => array(2,9), 22 => array(2,9), 24 => array(2,9)), array( 14 => array(2,10), 18 => array(2,10), 22 => array(2,10), 24 => array(2,10)), array( 14 => array(2,11), 18 => array(2,11), 22 => array(2,11), 24 => array(2,11)), array( 14 => array(2,12), 18 => array(2,12), 22 => array(2,12), 24 => array(2,12)), array( 14 => array(2,3), 18 => array(2,3), 22 => array(2,3), 24 => array(2,3)), array( 14 => array(2,4), 18 => array(2,4), 22 => array(2,4), 24 => array(2,4)), array( 14 => array(2,5), 18 => array(2,5), 22 => array(2,5), 24 => array(2,5)), array( 14 => array(2,1), 18 => array(2,1), 21 => array(2,1), 22 => array(2,1), 24 => array(2,1)), array( 14 => array(2,2), 18 => array(2,2), 22 => array(2,2), 24 => array(2,2)), array( 3 => 20, 4 => array(1,12), 18 => array(1,17), 19 => 18, 20 => 19 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 23, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15), 24 => array(1,21), 25 => 22 ), array( 1 => array(2,6)), array( 14 => array(2,13), 18 => array(2,13), 22 => array(2,13), 24 => array(2,13)), array( 18 => array(1,24), 22 => array(1,25)), array( 18 => array(2,16), 22 => array(2,16)), array( 21 => array(1,26)), array( 14 => array(2,18), 18 => array(2,18), 22 => array(2,18), 24 => array(2,18)), array( 22 => array(1,28), 24 => array(1,27)), array( 22 => array(2,20), 24 => array(2,20)), array( 14 => array(2,14), 18 => array(2,14), 22 => array(2,14), 24 => array(2,14)), array( 3 => 20, 4 => array(1,12), 20 => 29 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 30, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 14 => array(2,19), 18 => array(2,19), 22 => array(2,19), 24 => array(2,19)), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 31, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 18 => array(2,17), 22 => array(2,17)), array( 18 => array(2,15), 22 => array(2,15)), array( 22 => array(2,21), 24 => array(2,21)), |
|
118 | + ); |
|
119 | + |
|
120 | + private $defaultActions = array( |
|
121 | + 16 => array(2, 6) |
|
122 | + ); |
|
123 | + |
|
124 | + /** |
|
125 | + * @param string $input JSON string |
|
126 | + * @param int $flags Bitmask of parse/lint options (see constants of this class) |
|
127 | + * @return null|ParsingException null if no error is found, a ParsingException containing all details otherwise |
|
128 | + */ |
|
129 | + public function lint($input, $flags = 0) |
|
130 | + { |
|
131 | + try { |
|
132 | + $this->parse($input, $flags); |
|
133 | + } catch (ParsingException $e) { |
|
134 | + return $e; |
|
135 | + } |
|
136 | + return null; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @param string $input JSON string |
|
141 | + * @param int $flags Bitmask of parse/lint options (see constants of this class) |
|
142 | + * @return mixed |
|
143 | + * @throws ParsingException |
|
144 | + */ |
|
145 | + public function parse($input, $flags = 0) |
|
146 | + { |
|
147 | + $this->failOnBOM($input); |
|
148 | + |
|
149 | + $this->flags = $flags; |
|
150 | + |
|
151 | + $this->stack = array(0); |
|
152 | + $this->vstack = array(null); |
|
153 | + $this->lstack = array(); |
|
154 | + |
|
155 | + $yytext = ''; |
|
156 | + $yylineno = 0; |
|
157 | + $yyleng = 0; |
|
158 | + $recovering = 0; |
|
159 | + $TERROR = 2; |
|
160 | + $EOF = 1; |
|
161 | + |
|
162 | + $this->lexer = new Lexer(); |
|
163 | + $this->lexer->setInput($input); |
|
164 | + |
|
165 | + $yyloc = $this->lexer->yylloc; |
|
166 | + $this->lstack[] = $yyloc; |
|
167 | + |
|
168 | + $symbol = null; |
|
169 | + $preErrorSymbol = null; |
|
170 | + $state = null; |
|
171 | + $action = null; |
|
172 | + $a = null; |
|
173 | + $r = null; |
|
174 | + $yyval = new stdClass; |
|
175 | + $p = null; |
|
176 | + $len = null; |
|
177 | + $newState = null; |
|
178 | + $expected = null; |
|
179 | + $errStr = null; |
|
180 | + |
|
181 | + while (true) { |
|
182 | + // retrieve state number from top of stack |
|
183 | + $state = $this->stack[\count($this->stack)-1]; |
|
184 | + |
|
185 | + // use default actions if available |
|
186 | + if (isset($this->defaultActions[$state])) { |
|
187 | + $action = $this->defaultActions[$state]; |
|
188 | + } else { |
|
189 | + if ($symbol == null) { |
|
190 | + $symbol = $this->lex(); |
|
191 | + } |
|
192 | + // read action for current state and first input |
|
193 | + $action = isset($this->table[$state][$symbol]) ? $this->table[$state][$symbol] : false; |
|
194 | + } |
|
195 | + |
|
196 | + // handle parse error |
|
197 | + if (!$action || !$action[0]) { |
|
198 | + if (!$recovering) { |
|
199 | + // Report error |
|
200 | + $expected = array(); |
|
201 | + foreach ($this->table[$state] as $p => $ignore) { |
|
202 | + if (isset($this->terminals_[$p]) && $p > 2) { |
|
203 | + $expected[] = "'" . $this->terminals_[$p] . "'"; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + $message = null; |
|
208 | + if (\in_array("'STRING'", $expected) && \in_array(substr($this->lexer->match, 0, 1), array('"', "'"))) { |
|
209 | + $message = "Invalid string"; |
|
210 | + if ("'" === substr($this->lexer->match, 0, 1)) { |
|
211 | + $message .= ", it appears you used single quotes instead of double quotes"; |
|
212 | + } elseif (preg_match('{".+?(\\\\[^"bfnrt/\\\\u](...)?)}', $this->lexer->getFullUpcomingInput(), $match)) { |
|
213 | + $message .= ", it appears you have an unescaped backslash at: ".$match[1]; |
|
214 | + } elseif (preg_match('{"(?:[^"]+|\\\\")*$}m', $this->lexer->getFullUpcomingInput())) { |
|
215 | + $message .= ", it appears you forgot to terminate a string, or attempted to write a multiline string which is invalid"; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
220 | + $errStr .= $this->lexer->showPosition() . "\n"; |
|
221 | + if ($message) { |
|
222 | + $errStr .= $message; |
|
223 | + } else { |
|
224 | + $errStr .= (\count($expected) > 1) ? "Expected one of: " : "Expected: "; |
|
225 | + $errStr .= implode(', ', $expected); |
|
226 | + } |
|
227 | + |
|
228 | + if (',' === substr(trim($this->lexer->getPastInput()), -1)) { |
|
229 | + $errStr .= " - It appears you have an extra trailing comma"; |
|
230 | + } |
|
231 | + |
|
232 | + $this->parseError($errStr, array( |
|
233 | + 'text' => $this->lexer->match, |
|
234 | + 'token' => !empty($this->terminals_[$symbol]) ? $this->terminals_[$symbol] : $symbol, |
|
235 | + 'line' => $this->lexer->yylineno, |
|
236 | + 'loc' => $yyloc, |
|
237 | + 'expected' => $expected, |
|
238 | + )); |
|
239 | + } |
|
240 | + |
|
241 | + // just recovered from another error |
|
242 | + if ($recovering == 3) { |
|
243 | + if ($symbol == $EOF) { |
|
244 | + throw new ParsingException($errStr ?: 'Parsing halted.'); |
|
245 | + } |
|
246 | + |
|
247 | + // discard current lookahead and grab another |
|
248 | + $yyleng = $this->lexer->yyleng; |
|
249 | + $yytext = $this->lexer->yytext; |
|
250 | + $yylineno = $this->lexer->yylineno; |
|
251 | + $yyloc = $this->lexer->yylloc; |
|
252 | + $symbol = $this->lex(); |
|
253 | + } |
|
254 | + |
|
255 | + // try to recover from error |
|
256 | + while (true) { |
|
257 | + // check for error recovery rule in this state |
|
258 | + if (\array_key_exists($TERROR, $this->table[$state])) { |
|
259 | + break; |
|
260 | + } |
|
261 | + if ($state == 0) { |
|
262 | + throw new ParsingException($errStr ?: 'Parsing halted.'); |
|
263 | + } |
|
264 | + $this->popStack(1); |
|
265 | + $state = $this->stack[\count($this->stack)-1]; |
|
266 | + } |
|
267 | + |
|
268 | + $preErrorSymbol = $symbol; // save the lookahead token |
|
269 | + $symbol = $TERROR; // insert generic error symbol as new lookahead |
|
270 | + $state = $this->stack[\count($this->stack)-1]; |
|
271 | + $action = isset($this->table[$state][$TERROR]) ? $this->table[$state][$TERROR] : false; |
|
272 | + $recovering = 3; // allow 3 real symbols to be shifted before reporting a new error |
|
273 | + } |
|
274 | + |
|
275 | + // this shouldn't happen, unless resolve defaults are off |
|
276 | + if (\is_array($action[0]) && \count($action) > 1) { |
|
277 | + throw new ParsingException('Parse Error: multiple actions possible at state: ' . $state . ', token: ' . $symbol); |
|
278 | + } |
|
279 | + |
|
280 | + switch ($action[0]) { |
|
281 | + case 1: // shift |
|
282 | + $this->stack[] = $symbol; |
|
283 | + $this->vstack[] = $this->lexer->yytext; |
|
284 | + $this->lstack[] = $this->lexer->yylloc; |
|
285 | + $this->stack[] = $action[1]; // push state |
|
286 | + $symbol = null; |
|
287 | + if (!$preErrorSymbol) { // normal execution/no error |
|
288 | + $yyleng = $this->lexer->yyleng; |
|
289 | + $yytext = $this->lexer->yytext; |
|
290 | + $yylineno = $this->lexer->yylineno; |
|
291 | + $yyloc = $this->lexer->yylloc; |
|
292 | + if ($recovering > 0) { |
|
293 | + $recovering--; |
|
294 | + } |
|
295 | + } else { // error just occurred, resume old lookahead f/ before error |
|
296 | + $symbol = $preErrorSymbol; |
|
297 | + $preErrorSymbol = null; |
|
298 | + } |
|
299 | + break; |
|
300 | + |
|
301 | + case 2: // reduce |
|
302 | + $len = $this->productions_[$action[1]][1]; |
|
303 | + |
|
304 | + // perform semantic action |
|
305 | + $yyval->token = $this->vstack[\count($this->vstack) - $len]; // default to $$ = $1 |
|
306 | + // default location, uses first token for firsts, last for lasts |
|
307 | + $yyval->store = array( // _$ = store |
|
308 | + 'first_line' => $this->lstack[\count($this->lstack) - ($len ?: 1)]['first_line'], |
|
309 | + 'last_line' => $this->lstack[\count($this->lstack) - 1]['last_line'], |
|
310 | + 'first_column' => $this->lstack[\count($this->lstack) - ($len ?: 1)]['first_column'], |
|
311 | + 'last_column' => $this->lstack[\count($this->lstack) - 1]['last_column'], |
|
312 | + ); |
|
313 | + $r = $this->performAction($yyval, $yytext, $yyleng, $yylineno, $action[1], $this->vstack); |
|
314 | + |
|
315 | + if (!$r instanceof Undefined) { |
|
316 | + return $r; |
|
317 | + } |
|
318 | + |
|
319 | + if ($len) { |
|
320 | + $this->popStack($len); |
|
321 | + } |
|
322 | + |
|
323 | + $this->stack[] = $this->productions_[$action[1]][0]; // push nonterminal (reduce) |
|
324 | + $this->vstack[] = $yyval->token; |
|
325 | + $this->lstack[] = $yyval->store; |
|
326 | + $newState = $this->table[$this->stack[\count($this->stack)-2]][$this->stack[\count($this->stack)-1]]; |
|
327 | + $this->stack[] = $newState; |
|
328 | + break; |
|
329 | + |
|
330 | + case 3: // accept |
|
331 | + |
|
332 | + return true; |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + protected function parseError($str, $hash) |
|
338 | + { |
|
339 | + throw new ParsingException($str, $hash); |
|
340 | + } |
|
341 | + |
|
342 | + // $$ = $tokens // needs to be passed by ref? |
|
343 | + // $ = $token |
|
344 | + // _$ removed, useless? |
|
345 | + private function performAction(stdClass $yyval, $yytext, $yyleng, $yylineno, $yystate, &$tokens) |
|
346 | + { |
|
347 | + // $0 = $len |
|
348 | + $len = \count($tokens) - 1; |
|
349 | + switch ($yystate) { |
|
350 | + case 1: |
|
351 | + $yytext = preg_replace_callback('{(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4})}', array($this, 'stringInterpolation'), $yytext); |
|
352 | + $yyval->token = $yytext; |
|
353 | + break; |
|
354 | + case 2: |
|
355 | + if (strpos($yytext, 'e') !== false || strpos($yytext, 'E') !== false) { |
|
356 | + $yyval->token = \floatval($yytext); |
|
357 | + } else { |
|
358 | + $yyval->token = strpos($yytext, '.') === false ? \intval($yytext) : \floatval($yytext); |
|
359 | + } |
|
360 | + break; |
|
361 | + case 3: |
|
362 | + $yyval->token = null; |
|
363 | + break; |
|
364 | + case 4: |
|
365 | + $yyval->token = true; |
|
366 | + break; |
|
367 | + case 5: |
|
368 | + $yyval->token = false; |
|
369 | + break; |
|
370 | + case 6: |
|
371 | + return $yyval->token = $tokens[$len-1]; |
|
372 | + case 13: |
|
373 | + if ($this->flags & self::PARSE_TO_ASSOC) { |
|
374 | + $yyval->token = array(); |
|
375 | + } else { |
|
376 | + $yyval->token = new stdClass; |
|
377 | + } |
|
378 | + break; |
|
379 | + case 14: |
|
380 | + $yyval->token = $tokens[$len-1]; |
|
381 | + break; |
|
382 | + case 15: |
|
383 | + $yyval->token = array($tokens[$len-2], $tokens[$len]); |
|
384 | + break; |
|
385 | + case 16: |
|
386 | + if (PHP_VERSION_ID < 70100) { |
|
387 | + $property = $tokens[$len][0] === '' ? '_empty_' : $tokens[$len][0]; |
|
388 | + } else { |
|
389 | + $property = $tokens[$len][0]; |
|
390 | + } |
|
391 | + if ($this->flags & self::PARSE_TO_ASSOC) { |
|
392 | + $yyval->token = array(); |
|
393 | + $yyval->token[$property] = $tokens[$len][1]; |
|
394 | + } else { |
|
395 | + $yyval->token = new stdClass; |
|
396 | + $yyval->token->$property = $tokens[$len][1]; |
|
397 | + } |
|
398 | + break; |
|
399 | + case 17: |
|
400 | + if ($this->flags & self::PARSE_TO_ASSOC) { |
|
401 | + $yyval->token =& $tokens[$len-2]; |
|
402 | + $key = $tokens[$len][0]; |
|
403 | + if (($this->flags & self::DETECT_KEY_CONFLICTS) && isset($tokens[$len-2][$key])) { |
|
404 | + $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
405 | + $errStr .= $this->lexer->showPosition() . "\n"; |
|
406 | + $errStr .= "Duplicate key: ".$tokens[$len][0]; |
|
407 | + throw new DuplicateKeyException($errStr, $tokens[$len][0], array('line' => $yylineno+1)); |
|
408 | + } elseif (($this->flags & self::ALLOW_DUPLICATE_KEYS) && isset($tokens[$len-2][$key])) { |
|
409 | + $duplicateCount = 1; |
|
410 | + do { |
|
411 | + $duplicateKey = $key . '.' . $duplicateCount++; |
|
412 | + } while (isset($tokens[$len-2][$duplicateKey])); |
|
413 | + $key = $duplicateKey; |
|
414 | + } |
|
415 | + $tokens[$len-2][$key] = $tokens[$len][1]; |
|
416 | + } else { |
|
417 | + $yyval->token = $tokens[$len-2]; |
|
418 | + if (PHP_VERSION_ID < 70100) { |
|
419 | + $key = $tokens[$len][0] === '' ? '_empty_' : $tokens[$len][0]; |
|
420 | + } else { |
|
421 | + $key = $tokens[$len][0]; |
|
422 | + } |
|
423 | + if (($this->flags & self::DETECT_KEY_CONFLICTS) && isset($tokens[$len-2]->{$key})) { |
|
424 | + $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n"; |
|
425 | + $errStr .= $this->lexer->showPosition() . "\n"; |
|
426 | + $errStr .= "Duplicate key: ".$tokens[$len][0]; |
|
427 | + throw new DuplicateKeyException($errStr, $tokens[$len][0], array('line' => $yylineno+1)); |
|
428 | + } elseif (($this->flags & self::ALLOW_DUPLICATE_KEYS) && isset($tokens[$len-2]->{$key})) { |
|
429 | + $duplicateCount = 1; |
|
430 | + do { |
|
431 | + $duplicateKey = $key . '.' . $duplicateCount++; |
|
432 | + } while (isset($tokens[$len-2]->$duplicateKey)); |
|
433 | + $key = $duplicateKey; |
|
434 | + } |
|
435 | + $tokens[$len-2]->$key = $tokens[$len][1]; |
|
436 | + } |
|
437 | + break; |
|
438 | + case 18: |
|
439 | + $yyval->token = array(); |
|
440 | + break; |
|
441 | + case 19: |
|
442 | + $yyval->token = $tokens[$len-1]; |
|
443 | + break; |
|
444 | + case 20: |
|
445 | + $yyval->token = array($tokens[$len]); |
|
446 | + break; |
|
447 | + case 21: |
|
448 | + $tokens[$len-2][] = $tokens[$len]; |
|
449 | + $yyval->token = $tokens[$len-2]; |
|
450 | + break; |
|
451 | + } |
|
452 | + |
|
453 | + return new Undefined(); |
|
454 | + } |
|
455 | + |
|
456 | + private function stringInterpolation($match) |
|
457 | + { |
|
458 | + switch ($match[0]) { |
|
459 | + case '\\\\': |
|
460 | + return '\\'; |
|
461 | + case '\"': |
|
462 | + return '"'; |
|
463 | + case '\b': |
|
464 | + return \chr(8); |
|
465 | + case '\f': |
|
466 | + return \chr(12); |
|
467 | + case '\n': |
|
468 | + return "\n"; |
|
469 | + case '\r': |
|
470 | + return "\r"; |
|
471 | + case '\t': |
|
472 | + return "\t"; |
|
473 | + case '\/': |
|
474 | + return "/"; |
|
475 | + default: |
|
476 | + return html_entity_decode('&#x'.ltrim(substr($match[0], 2), '0').';', ENT_QUOTES, 'UTF-8'); |
|
477 | + } |
|
478 | + } |
|
479 | + |
|
480 | + private function popStack($n) |
|
481 | + { |
|
482 | + $this->stack = \array_slice($this->stack, 0, - (2 * $n)); |
|
483 | + $this->vstack = \array_slice($this->vstack, 0, - $n); |
|
484 | + $this->lstack = \array_slice($this->lstack, 0, - $n); |
|
485 | + } |
|
486 | + |
|
487 | + private function lex() |
|
488 | + { |
|
489 | + $token = $this->lexer->lex() ?: 1; // $end = 1 |
|
490 | + // if token isn't its numeric value, convert |
|
491 | + if (!is_numeric($token)) { |
|
492 | + $token = isset($this->symbols[$token]) ? $this->symbols[$token] : $token; |
|
493 | + } |
|
494 | + |
|
495 | + return $token; |
|
496 | + } |
|
497 | + |
|
498 | + private function failOnBOM($input) |
|
499 | + { |
|
500 | + // UTF-8 ByteOrderMark sequence |
|
501 | + $bom = "\xEF\xBB\xBF"; |
|
502 | + |
|
503 | + if (substr($input, 0, 3) === $bom) { |
|
504 | + $this->parseError("BOM detected, make sure your input does not include a Unicode Byte-Order-Mark", array()); |
|
505 | + } |
|
506 | + } |
|
507 | 507 | } |
@@ -13,27 +13,27 @@ |
||
13 | 13 | |
14 | 14 | class DuplicateKeyException extends ParsingException |
15 | 15 | { |
16 | - /** |
|
17 | - * @param string $message |
|
18 | - * @param string $key |
|
19 | - * @phpstan-param array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} $details |
|
20 | - */ |
|
21 | - public function __construct($message, $key, array $details = array()) |
|
22 | - { |
|
23 | - $details['key'] = $key; |
|
24 | - parent::__construct($message, $details); |
|
25 | - } |
|
16 | + /** |
|
17 | + * @param string $message |
|
18 | + * @param string $key |
|
19 | + * @phpstan-param array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} $details |
|
20 | + */ |
|
21 | + public function __construct($message, $key, array $details = array()) |
|
22 | + { |
|
23 | + $details['key'] = $key; |
|
24 | + parent::__construct($message, $details); |
|
25 | + } |
|
26 | 26 | |
27 | - public function getKey() |
|
28 | - { |
|
29 | - return $this->details['key']; |
|
30 | - } |
|
27 | + public function getKey() |
|
28 | + { |
|
29 | + return $this->details['key']; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @phpstan-return array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[], key: string} |
|
34 | - */ |
|
35 | - public function getDetails() |
|
36 | - { |
|
37 | - return $this->details; |
|
38 | - } |
|
32 | + /** |
|
33 | + * @phpstan-return array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[], key: string} |
|
34 | + */ |
|
35 | + public function getDetails() |
|
36 | + { |
|
37 | + return $this->details; |
|
38 | + } |
|
39 | 39 | } |
@@ -18,216 +18,216 @@ |
||
18 | 18 | */ |
19 | 19 | class Lexer |
20 | 20 | { |
21 | - private $EOF = 1; |
|
22 | - /** |
|
23 | - * @phpstan-var array<int, string> |
|
24 | - */ |
|
25 | - private $rules = array( |
|
26 | - 0 => '/\G\s+/', |
|
27 | - 1 => '/\G-?([0-9]|[1-9][0-9]+)(\.[0-9]+)?([eE][+-]?[0-9]+)?\b/', |
|
28 | - 2 => '{\G"(?>\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x1f\\\\"]++)*+"}', |
|
29 | - 3 => '/\G\{/', |
|
30 | - 4 => '/\G\}/', |
|
31 | - 5 => '/\G\[/', |
|
32 | - 6 => '/\G\]/', |
|
33 | - 7 => '/\G,/', |
|
34 | - 8 => '/\G:/', |
|
35 | - 9 => '/\Gtrue\b/', |
|
36 | - 10 => '/\Gfalse\b/', |
|
37 | - 11 => '/\Gnull\b/', |
|
38 | - 12 => '/\G$/', |
|
39 | - 13 => '/\G./', |
|
40 | - ); |
|
41 | - |
|
42 | - private $conditions = array( |
|
43 | - "INITIAL" => array( |
|
44 | - "rules" => array(0,1,2,3,4,5,6,7,8,9,10,11,12,13), |
|
45 | - "inclusive" => true, |
|
46 | - ), |
|
47 | - ); |
|
48 | - |
|
49 | - private $conditionStack; |
|
50 | - private $input; |
|
51 | - private $more; |
|
52 | - private $done; |
|
53 | - private $offset; |
|
54 | - |
|
55 | - public $match; |
|
56 | - public $yylineno; |
|
57 | - public $yyleng; |
|
58 | - public $yytext; |
|
59 | - public $yylloc; |
|
60 | - |
|
61 | - public function lex() |
|
62 | - { |
|
63 | - $r = $this->next(); |
|
64 | - if (!$r instanceof Undefined) { |
|
65 | - return $r; |
|
66 | - } |
|
67 | - |
|
68 | - return $this->lex(); |
|
69 | - } |
|
70 | - |
|
71 | - public function setInput($input) |
|
72 | - { |
|
73 | - $this->input = $input; |
|
74 | - $this->more = false; |
|
75 | - $this->done = false; |
|
76 | - $this->offset = 0; |
|
77 | - $this->yylineno = $this->yyleng = 0; |
|
78 | - $this->yytext = $this->match = ''; |
|
79 | - $this->conditionStack = array('INITIAL'); |
|
80 | - $this->yylloc = array('first_line' => 1, 'first_column' => 0, 'last_line' => 1, 'last_column' => 0); |
|
81 | - |
|
82 | - return $this; |
|
83 | - } |
|
84 | - |
|
85 | - public function showPosition() |
|
86 | - { |
|
87 | - $pre = str_replace("\n", '', $this->getPastInput()); |
|
88 | - $c = str_repeat('-', max(0, \strlen($pre) - 1)); // new Array(pre.length + 1).join("-"); |
|
89 | - |
|
90 | - return $pre . str_replace("\n", '', $this->getUpcomingInput()) . "\n" . $c . "^"; |
|
91 | - } |
|
92 | - |
|
93 | - public function getPastInput() |
|
94 | - { |
|
95 | - $pastLength = $this->offset - \strlen($this->match); |
|
96 | - |
|
97 | - return ($pastLength > 20 ? '...' : '') . substr($this->input, max(0, $pastLength - 20), min(20, $pastLength)); |
|
98 | - } |
|
99 | - |
|
100 | - public function getUpcomingInput() |
|
101 | - { |
|
102 | - $next = $this->match; |
|
103 | - if (\strlen($next) < 20) { |
|
104 | - $next .= substr($this->input, $this->offset, 20 - \strlen($next)); |
|
105 | - } |
|
106 | - |
|
107 | - return substr($next, 0, 20) . (\strlen($next) > 20 ? '...' : ''); |
|
108 | - } |
|
109 | - |
|
110 | - public function getFullUpcomingInput() |
|
111 | - { |
|
112 | - $next = $this->match; |
|
113 | - if (substr($next, 0, 1) === '"' && substr_count($next, '"') === 1) { |
|
114 | - $len = \strlen($this->input); |
|
115 | - $strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len); |
|
116 | - $next .= substr($this->input, $this->offset, $strEnd - $this->offset); |
|
117 | - } elseif (\strlen($next) < 20) { |
|
118 | - $next .= substr($this->input, $this->offset, 20 - \strlen($next)); |
|
119 | - } |
|
120 | - |
|
121 | - return $next; |
|
122 | - } |
|
123 | - |
|
124 | - protected function parseError($str, $hash) |
|
125 | - { |
|
126 | - throw new \Exception($str); |
|
127 | - } |
|
128 | - |
|
129 | - private function next() |
|
130 | - { |
|
131 | - if ($this->done) { |
|
132 | - return $this->EOF; |
|
133 | - } |
|
134 | - if ($this->offset === \strlen($this->input)) { |
|
135 | - $this->done = true; |
|
136 | - } |
|
137 | - |
|
138 | - $token = null; |
|
139 | - $match = null; |
|
140 | - $col = null; |
|
141 | - $lines = null; |
|
142 | - |
|
143 | - if (!$this->more) { |
|
144 | - $this->yytext = ''; |
|
145 | - $this->match = ''; |
|
146 | - } |
|
147 | - |
|
148 | - $rules = $this->getCurrentRules(); |
|
149 | - $rulesLen = \count($rules); |
|
150 | - |
|
151 | - for ($i=0; $i < $rulesLen; $i++) { |
|
152 | - if (preg_match($this->rules[$rules[$i]], $this->input, $match, 0, $this->offset)) { |
|
153 | - preg_match_all('/\n.*/', $match[0], $lines); |
|
154 | - $lines = $lines[0]; |
|
155 | - if ($lines) { |
|
156 | - $this->yylineno += \count($lines); |
|
157 | - } |
|
158 | - |
|
159 | - $this->yylloc = array( |
|
160 | - 'first_line' => $this->yylloc['last_line'], |
|
161 | - 'last_line' => $this->yylineno+1, |
|
162 | - 'first_column' => $this->yylloc['last_column'], |
|
163 | - 'last_column' => $lines ? \strlen($lines[\count($lines) - 1]) - 1 : $this->yylloc['last_column'] + \strlen($match[0]), |
|
164 | - ); |
|
165 | - $this->yytext .= $match[0]; |
|
166 | - $this->match .= $match[0]; |
|
167 | - $this->yyleng = \strlen($this->yytext); |
|
168 | - $this->more = false; |
|
169 | - $this->offset += \strlen($match[0]); |
|
170 | - $token = $this->performAction($rules[$i], $this->conditionStack[\count($this->conditionStack)-1]); |
|
171 | - if ($token) { |
|
172 | - return $token; |
|
173 | - } |
|
174 | - |
|
175 | - return new Undefined(); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - if ($this->offset === \strlen($this->input)) { |
|
180 | - return $this->EOF; |
|
181 | - } |
|
182 | - |
|
183 | - $this->parseError( |
|
184 | - 'Lexical error on line ' . ($this->yylineno+1) . ". Unrecognized text.\n" . $this->showPosition(), |
|
185 | - array( |
|
186 | - 'text' => "", |
|
187 | - 'token' => null, |
|
188 | - 'line' => $this->yylineno, |
|
189 | - ) |
|
190 | - ); |
|
191 | - } |
|
192 | - |
|
193 | - private function getCurrentRules() |
|
194 | - { |
|
195 | - return $this->conditions[$this->conditionStack[\count($this->conditionStack)-1]]['rules']; |
|
196 | - } |
|
197 | - |
|
198 | - private function performAction($avoiding_name_collisions, $YY_START) |
|
199 | - { |
|
200 | - switch ($avoiding_name_collisions) { |
|
201 | - case 0:/* skip whitespace */ |
|
202 | - break; |
|
203 | - case 1: |
|
204 | - return 6; |
|
205 | - case 2: |
|
206 | - $this->yytext = substr($this->yytext, 1, $this->yyleng-2); |
|
207 | - |
|
208 | - return 4; |
|
209 | - case 3: |
|
210 | - return 17; |
|
211 | - case 4: |
|
212 | - return 18; |
|
213 | - case 5: |
|
214 | - return 23; |
|
215 | - case 6: |
|
216 | - return 24; |
|
217 | - case 7: |
|
218 | - return 22; |
|
219 | - case 8: |
|
220 | - return 21; |
|
221 | - case 9: |
|
222 | - return 10; |
|
223 | - case 10: |
|
224 | - return 11; |
|
225 | - case 11: |
|
226 | - return 8; |
|
227 | - case 12: |
|
228 | - return 14; |
|
229 | - case 13: |
|
230 | - return 'INVALID'; |
|
231 | - } |
|
232 | - } |
|
21 | + private $EOF = 1; |
|
22 | + /** |
|
23 | + * @phpstan-var array<int, string> |
|
24 | + */ |
|
25 | + private $rules = array( |
|
26 | + 0 => '/\G\s+/', |
|
27 | + 1 => '/\G-?([0-9]|[1-9][0-9]+)(\.[0-9]+)?([eE][+-]?[0-9]+)?\b/', |
|
28 | + 2 => '{\G"(?>\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x1f\\\\"]++)*+"}', |
|
29 | + 3 => '/\G\{/', |
|
30 | + 4 => '/\G\}/', |
|
31 | + 5 => '/\G\[/', |
|
32 | + 6 => '/\G\]/', |
|
33 | + 7 => '/\G,/', |
|
34 | + 8 => '/\G:/', |
|
35 | + 9 => '/\Gtrue\b/', |
|
36 | + 10 => '/\Gfalse\b/', |
|
37 | + 11 => '/\Gnull\b/', |
|
38 | + 12 => '/\G$/', |
|
39 | + 13 => '/\G./', |
|
40 | + ); |
|
41 | + |
|
42 | + private $conditions = array( |
|
43 | + "INITIAL" => array( |
|
44 | + "rules" => array(0,1,2,3,4,5,6,7,8,9,10,11,12,13), |
|
45 | + "inclusive" => true, |
|
46 | + ), |
|
47 | + ); |
|
48 | + |
|
49 | + private $conditionStack; |
|
50 | + private $input; |
|
51 | + private $more; |
|
52 | + private $done; |
|
53 | + private $offset; |
|
54 | + |
|
55 | + public $match; |
|
56 | + public $yylineno; |
|
57 | + public $yyleng; |
|
58 | + public $yytext; |
|
59 | + public $yylloc; |
|
60 | + |
|
61 | + public function lex() |
|
62 | + { |
|
63 | + $r = $this->next(); |
|
64 | + if (!$r instanceof Undefined) { |
|
65 | + return $r; |
|
66 | + } |
|
67 | + |
|
68 | + return $this->lex(); |
|
69 | + } |
|
70 | + |
|
71 | + public function setInput($input) |
|
72 | + { |
|
73 | + $this->input = $input; |
|
74 | + $this->more = false; |
|
75 | + $this->done = false; |
|
76 | + $this->offset = 0; |
|
77 | + $this->yylineno = $this->yyleng = 0; |
|
78 | + $this->yytext = $this->match = ''; |
|
79 | + $this->conditionStack = array('INITIAL'); |
|
80 | + $this->yylloc = array('first_line' => 1, 'first_column' => 0, 'last_line' => 1, 'last_column' => 0); |
|
81 | + |
|
82 | + return $this; |
|
83 | + } |
|
84 | + |
|
85 | + public function showPosition() |
|
86 | + { |
|
87 | + $pre = str_replace("\n", '', $this->getPastInput()); |
|
88 | + $c = str_repeat('-', max(0, \strlen($pre) - 1)); // new Array(pre.length + 1).join("-"); |
|
89 | + |
|
90 | + return $pre . str_replace("\n", '', $this->getUpcomingInput()) . "\n" . $c . "^"; |
|
91 | + } |
|
92 | + |
|
93 | + public function getPastInput() |
|
94 | + { |
|
95 | + $pastLength = $this->offset - \strlen($this->match); |
|
96 | + |
|
97 | + return ($pastLength > 20 ? '...' : '') . substr($this->input, max(0, $pastLength - 20), min(20, $pastLength)); |
|
98 | + } |
|
99 | + |
|
100 | + public function getUpcomingInput() |
|
101 | + { |
|
102 | + $next = $this->match; |
|
103 | + if (\strlen($next) < 20) { |
|
104 | + $next .= substr($this->input, $this->offset, 20 - \strlen($next)); |
|
105 | + } |
|
106 | + |
|
107 | + return substr($next, 0, 20) . (\strlen($next) > 20 ? '...' : ''); |
|
108 | + } |
|
109 | + |
|
110 | + public function getFullUpcomingInput() |
|
111 | + { |
|
112 | + $next = $this->match; |
|
113 | + if (substr($next, 0, 1) === '"' && substr_count($next, '"') === 1) { |
|
114 | + $len = \strlen($this->input); |
|
115 | + $strEnd = min(strpos($this->input, '"', $this->offset + 1) ?: $len, strpos($this->input, "\n", $this->offset + 1) ?: $len); |
|
116 | + $next .= substr($this->input, $this->offset, $strEnd - $this->offset); |
|
117 | + } elseif (\strlen($next) < 20) { |
|
118 | + $next .= substr($this->input, $this->offset, 20 - \strlen($next)); |
|
119 | + } |
|
120 | + |
|
121 | + return $next; |
|
122 | + } |
|
123 | + |
|
124 | + protected function parseError($str, $hash) |
|
125 | + { |
|
126 | + throw new \Exception($str); |
|
127 | + } |
|
128 | + |
|
129 | + private function next() |
|
130 | + { |
|
131 | + if ($this->done) { |
|
132 | + return $this->EOF; |
|
133 | + } |
|
134 | + if ($this->offset === \strlen($this->input)) { |
|
135 | + $this->done = true; |
|
136 | + } |
|
137 | + |
|
138 | + $token = null; |
|
139 | + $match = null; |
|
140 | + $col = null; |
|
141 | + $lines = null; |
|
142 | + |
|
143 | + if (!$this->more) { |
|
144 | + $this->yytext = ''; |
|
145 | + $this->match = ''; |
|
146 | + } |
|
147 | + |
|
148 | + $rules = $this->getCurrentRules(); |
|
149 | + $rulesLen = \count($rules); |
|
150 | + |
|
151 | + for ($i=0; $i < $rulesLen; $i++) { |
|
152 | + if (preg_match($this->rules[$rules[$i]], $this->input, $match, 0, $this->offset)) { |
|
153 | + preg_match_all('/\n.*/', $match[0], $lines); |
|
154 | + $lines = $lines[0]; |
|
155 | + if ($lines) { |
|
156 | + $this->yylineno += \count($lines); |
|
157 | + } |
|
158 | + |
|
159 | + $this->yylloc = array( |
|
160 | + 'first_line' => $this->yylloc['last_line'], |
|
161 | + 'last_line' => $this->yylineno+1, |
|
162 | + 'first_column' => $this->yylloc['last_column'], |
|
163 | + 'last_column' => $lines ? \strlen($lines[\count($lines) - 1]) - 1 : $this->yylloc['last_column'] + \strlen($match[0]), |
|
164 | + ); |
|
165 | + $this->yytext .= $match[0]; |
|
166 | + $this->match .= $match[0]; |
|
167 | + $this->yyleng = \strlen($this->yytext); |
|
168 | + $this->more = false; |
|
169 | + $this->offset += \strlen($match[0]); |
|
170 | + $token = $this->performAction($rules[$i], $this->conditionStack[\count($this->conditionStack)-1]); |
|
171 | + if ($token) { |
|
172 | + return $token; |
|
173 | + } |
|
174 | + |
|
175 | + return new Undefined(); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + if ($this->offset === \strlen($this->input)) { |
|
180 | + return $this->EOF; |
|
181 | + } |
|
182 | + |
|
183 | + $this->parseError( |
|
184 | + 'Lexical error on line ' . ($this->yylineno+1) . ". Unrecognized text.\n" . $this->showPosition(), |
|
185 | + array( |
|
186 | + 'text' => "", |
|
187 | + 'token' => null, |
|
188 | + 'line' => $this->yylineno, |
|
189 | + ) |
|
190 | + ); |
|
191 | + } |
|
192 | + |
|
193 | + private function getCurrentRules() |
|
194 | + { |
|
195 | + return $this->conditions[$this->conditionStack[\count($this->conditionStack)-1]]['rules']; |
|
196 | + } |
|
197 | + |
|
198 | + private function performAction($avoiding_name_collisions, $YY_START) |
|
199 | + { |
|
200 | + switch ($avoiding_name_collisions) { |
|
201 | + case 0:/* skip whitespace */ |
|
202 | + break; |
|
203 | + case 1: |
|
204 | + return 6; |
|
205 | + case 2: |
|
206 | + $this->yytext = substr($this->yytext, 1, $this->yyleng-2); |
|
207 | + |
|
208 | + return 4; |
|
209 | + case 3: |
|
210 | + return 17; |
|
211 | + case 4: |
|
212 | + return 18; |
|
213 | + case 5: |
|
214 | + return 23; |
|
215 | + case 6: |
|
216 | + return 24; |
|
217 | + case 7: |
|
218 | + return 22; |
|
219 | + case 8: |
|
220 | + return 21; |
|
221 | + case 9: |
|
222 | + return 10; |
|
223 | + case 10: |
|
224 | + return 11; |
|
225 | + case 11: |
|
226 | + return 8; |
|
227 | + case 12: |
|
228 | + return 14; |
|
229 | + case 13: |
|
230 | + return 'INVALID'; |
|
231 | + } |
|
232 | + } |
|
233 | 233 | } |
@@ -13,23 +13,23 @@ |
||
13 | 13 | |
14 | 14 | class ParsingException extends \Exception |
15 | 15 | { |
16 | - protected $details; |
|
16 | + protected $details; |
|
17 | 17 | |
18 | - /** |
|
19 | - * @param string $message |
|
20 | - * @phpstan-param array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} $details |
|
21 | - */ |
|
22 | - public function __construct($message, $details = array()) |
|
23 | - { |
|
24 | - $this->details = $details; |
|
25 | - parent::__construct($message); |
|
26 | - } |
|
18 | + /** |
|
19 | + * @param string $message |
|
20 | + * @phpstan-param array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} $details |
|
21 | + */ |
|
22 | + public function __construct($message, $details = array()) |
|
23 | + { |
|
24 | + $this->details = $details; |
|
25 | + parent::__construct($message); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * @phpstan-return array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} |
|
30 | - */ |
|
31 | - public function getDetails() |
|
32 | - { |
|
33 | - return $this->details; |
|
34 | - } |
|
28 | + /** |
|
29 | + * @phpstan-return array{text?: string, token?: string, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]} |
|
30 | + */ |
|
31 | + public function getDetails() |
|
32 | + { |
|
33 | + return $this->details; |
|
34 | + } |
|
35 | 35 | } |
@@ -9,28 +9,28 @@ |
||
9 | 9 | */ |
10 | 10 | interface ContainerInterface |
11 | 11 | { |
12 | - /** |
|
13 | - * Finds an entry of the container by its identifier and returns it. |
|
14 | - * |
|
15 | - * @param string $id Identifier of the entry to look for. |
|
16 | - * |
|
17 | - * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
|
18 | - * @throws ContainerExceptionInterface Error while retrieving the entry. |
|
19 | - * |
|
20 | - * @return mixed Entry. |
|
21 | - */ |
|
22 | - public function get(string $id); |
|
12 | + /** |
|
13 | + * Finds an entry of the container by its identifier and returns it. |
|
14 | + * |
|
15 | + * @param string $id Identifier of the entry to look for. |
|
16 | + * |
|
17 | + * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
|
18 | + * @throws ContainerExceptionInterface Error while retrieving the entry. |
|
19 | + * |
|
20 | + * @return mixed Entry. |
|
21 | + */ |
|
22 | + public function get(string $id); |
|
23 | 23 | |
24 | - /** |
|
25 | - * Returns true if the container can return an entry for the given identifier. |
|
26 | - * Returns false otherwise. |
|
27 | - * |
|
28 | - * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
29 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
30 | - * |
|
31 | - * @param string $id Identifier of the entry to look for. |
|
32 | - * |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - public function has(string $id); |
|
24 | + /** |
|
25 | + * Returns true if the container can return an entry for the given identifier. |
|
26 | + * Returns false otherwise. |
|
27 | + * |
|
28 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
29 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
30 | + * |
|
31 | + * @param string $id Identifier of the entry to look for. |
|
32 | + * |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + public function has(string $id); |
|
36 | 36 | } |
@@ -4,111 +4,111 @@ |
||
4 | 4 | |
5 | 5 | interface CacheInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Fetches a value from the cache. |
|
9 | - * |
|
10 | - * @param string $key The unique key of this item in the cache. |
|
11 | - * @param mixed $default Default value to return if the key does not exist. |
|
12 | - * |
|
13 | - * @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
14 | - * |
|
15 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
16 | - * MUST be thrown if the $key string is not a legal value. |
|
17 | - */ |
|
18 | - public function get($key, $default = null); |
|
7 | + /** |
|
8 | + * Fetches a value from the cache. |
|
9 | + * |
|
10 | + * @param string $key The unique key of this item in the cache. |
|
11 | + * @param mixed $default Default value to return if the key does not exist. |
|
12 | + * |
|
13 | + * @return mixed The value of the item from the cache, or $default in case of cache miss. |
|
14 | + * |
|
15 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
16 | + * MUST be thrown if the $key string is not a legal value. |
|
17 | + */ |
|
18 | + public function get($key, $default = null); |
|
19 | 19 | |
20 | - /** |
|
21 | - * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
22 | - * |
|
23 | - * @param string $key The key of the item to store. |
|
24 | - * @param mixed $value The value of the item to store, must be serializable. |
|
25 | - * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
26 | - * the driver supports TTL then the library may set a default value |
|
27 | - * for it or let the driver take care of that. |
|
28 | - * |
|
29 | - * @return bool True on success and false on failure. |
|
30 | - * |
|
31 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
32 | - * MUST be thrown if the $key string is not a legal value. |
|
33 | - */ |
|
34 | - public function set($key, $value, $ttl = null); |
|
20 | + /** |
|
21 | + * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
22 | + * |
|
23 | + * @param string $key The key of the item to store. |
|
24 | + * @param mixed $value The value of the item to store, must be serializable. |
|
25 | + * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
26 | + * the driver supports TTL then the library may set a default value |
|
27 | + * for it or let the driver take care of that. |
|
28 | + * |
|
29 | + * @return bool True on success and false on failure. |
|
30 | + * |
|
31 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
32 | + * MUST be thrown if the $key string is not a legal value. |
|
33 | + */ |
|
34 | + public function set($key, $value, $ttl = null); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Delete an item from the cache by its unique key. |
|
38 | - * |
|
39 | - * @param string $key The unique cache key of the item to delete. |
|
40 | - * |
|
41 | - * @return bool True if the item was successfully removed. False if there was an error. |
|
42 | - * |
|
43 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
44 | - * MUST be thrown if the $key string is not a legal value. |
|
45 | - */ |
|
46 | - public function delete($key); |
|
36 | + /** |
|
37 | + * Delete an item from the cache by its unique key. |
|
38 | + * |
|
39 | + * @param string $key The unique cache key of the item to delete. |
|
40 | + * |
|
41 | + * @return bool True if the item was successfully removed. False if there was an error. |
|
42 | + * |
|
43 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
44 | + * MUST be thrown if the $key string is not a legal value. |
|
45 | + */ |
|
46 | + public function delete($key); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Wipes clean the entire cache's keys. |
|
50 | - * |
|
51 | - * @return bool True on success and false on failure. |
|
52 | - */ |
|
53 | - public function clear(); |
|
48 | + /** |
|
49 | + * Wipes clean the entire cache's keys. |
|
50 | + * |
|
51 | + * @return bool True on success and false on failure. |
|
52 | + */ |
|
53 | + public function clear(); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Obtains multiple cache items by their unique keys. |
|
57 | - * |
|
58 | - * @param iterable $keys A list of keys that can obtained in a single operation. |
|
59 | - * @param mixed $default Default value to return for keys that do not exist. |
|
60 | - * |
|
61 | - * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
|
62 | - * |
|
63 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
64 | - * MUST be thrown if $keys is neither an array nor a Traversable, |
|
65 | - * or if any of the $keys are not a legal value. |
|
66 | - */ |
|
67 | - public function getMultiple($keys, $default = null); |
|
55 | + /** |
|
56 | + * Obtains multiple cache items by their unique keys. |
|
57 | + * |
|
58 | + * @param iterable $keys A list of keys that can obtained in a single operation. |
|
59 | + * @param mixed $default Default value to return for keys that do not exist. |
|
60 | + * |
|
61 | + * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
|
62 | + * |
|
63 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
64 | + * MUST be thrown if $keys is neither an array nor a Traversable, |
|
65 | + * or if any of the $keys are not a legal value. |
|
66 | + */ |
|
67 | + public function getMultiple($keys, $default = null); |
|
68 | 68 | |
69 | - /** |
|
70 | - * Persists a set of key => value pairs in the cache, with an optional TTL. |
|
71 | - * |
|
72 | - * @param iterable $values A list of key => value pairs for a multiple-set operation. |
|
73 | - * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
74 | - * the driver supports TTL then the library may set a default value |
|
75 | - * for it or let the driver take care of that. |
|
76 | - * |
|
77 | - * @return bool True on success and false on failure. |
|
78 | - * |
|
79 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
80 | - * MUST be thrown if $values is neither an array nor a Traversable, |
|
81 | - * or if any of the $values are not a legal value. |
|
82 | - */ |
|
83 | - public function setMultiple($values, $ttl = null); |
|
69 | + /** |
|
70 | + * Persists a set of key => value pairs in the cache, with an optional TTL. |
|
71 | + * |
|
72 | + * @param iterable $values A list of key => value pairs for a multiple-set operation. |
|
73 | + * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
|
74 | + * the driver supports TTL then the library may set a default value |
|
75 | + * for it or let the driver take care of that. |
|
76 | + * |
|
77 | + * @return bool True on success and false on failure. |
|
78 | + * |
|
79 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
80 | + * MUST be thrown if $values is neither an array nor a Traversable, |
|
81 | + * or if any of the $values are not a legal value. |
|
82 | + */ |
|
83 | + public function setMultiple($values, $ttl = null); |
|
84 | 84 | |
85 | - /** |
|
86 | - * Deletes multiple cache items in a single operation. |
|
87 | - * |
|
88 | - * @param iterable $keys A list of string-based keys to be deleted. |
|
89 | - * |
|
90 | - * @return bool True if the items were successfully removed. False if there was an error. |
|
91 | - * |
|
92 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
93 | - * MUST be thrown if $keys is neither an array nor a Traversable, |
|
94 | - * or if any of the $keys are not a legal value. |
|
95 | - */ |
|
96 | - public function deleteMultiple($keys); |
|
85 | + /** |
|
86 | + * Deletes multiple cache items in a single operation. |
|
87 | + * |
|
88 | + * @param iterable $keys A list of string-based keys to be deleted. |
|
89 | + * |
|
90 | + * @return bool True if the items were successfully removed. False if there was an error. |
|
91 | + * |
|
92 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
93 | + * MUST be thrown if $keys is neither an array nor a Traversable, |
|
94 | + * or if any of the $keys are not a legal value. |
|
95 | + */ |
|
96 | + public function deleteMultiple($keys); |
|
97 | 97 | |
98 | - /** |
|
99 | - * Determines whether an item is present in the cache. |
|
100 | - * |
|
101 | - * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
|
102 | - * and not to be used within your live applications operations for get/set, as this method |
|
103 | - * is subject to a race condition where your has() will return true and immediately after, |
|
104 | - * another script can remove it making the state of your app out of date. |
|
105 | - * |
|
106 | - * @param string $key The cache item key. |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - * |
|
110 | - * @throws \Psr\SimpleCache\InvalidArgumentException |
|
111 | - * MUST be thrown if the $key string is not a legal value. |
|
112 | - */ |
|
113 | - public function has($key); |
|
98 | + /** |
|
99 | + * Determines whether an item is present in the cache. |
|
100 | + * |
|
101 | + * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
|
102 | + * and not to be used within your live applications operations for get/set, as this method |
|
103 | + * is subject to a race condition where your has() will return true and immediately after, |
|
104 | + * another script can remove it making the state of your app out of date. |
|
105 | + * |
|
106 | + * @param string $key The cache item key. |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + * |
|
110 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
|
111 | + * MUST be thrown if the $key string is not a legal value. |
|
112 | + */ |
|
113 | + public function has($key); |
|
114 | 114 | } |
@@ -12,19 +12,19 @@ |
||
12 | 12 | */ |
13 | 13 | class NullLogger extends AbstractLogger |
14 | 14 | { |
15 | - /** |
|
16 | - * Logs with an arbitrary level. |
|
17 | - * |
|
18 | - * @param mixed $level |
|
19 | - * @param string $message |
|
20 | - * @param array $context |
|
21 | - * |
|
22 | - * @return void |
|
23 | - * |
|
24 | - * @throws \Psr\Log\InvalidArgumentException |
|
25 | - */ |
|
26 | - public function log($level, $message, array $context = array()) |
|
27 | - { |
|
28 | - // noop |
|
29 | - } |
|
15 | + /** |
|
16 | + * Logs with an arbitrary level. |
|
17 | + * |
|
18 | + * @param mixed $level |
|
19 | + * @param string $message |
|
20 | + * @param array $context |
|
21 | + * |
|
22 | + * @return void |
|
23 | + * |
|
24 | + * @throws \Psr\Log\InvalidArgumentException |
|
25 | + */ |
|
26 | + public function log($level, $message, array $context = array()) |
|
27 | + { |
|
28 | + // noop |
|
29 | + } |
|
30 | 30 | } |