Skip to content

Commit fc76c70

Browse files
committed
added reportParseErrors()
1 parent fe67dba commit fc76c70

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ The `$loader->setAutoRefresh(true or false)` determines whether RobotLoader shou
7474
This feature should be disabled on production server.
7575

7676
If you want RobotLoader to skip some directory, use `$loader->excludeDirectory('temp')`.
77+
78+
By default, RobotLoader reports errors in PHP files by throwing exception `ParseError` (since PHP 7.0). It can be disabled via `$loader->reportParseErrors(false)`.

src/RobotLoader/RobotLoader.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class RobotLoader
3737
/** @var bool */
3838
private $autoRebuild = true;
3939

40+
/** @var bool */
41+
private $reportParseErrors = true;
42+
4043
/** @var array */
4144
private $scanPaths = [];
4245

@@ -127,6 +130,16 @@ public function addDirectory($path)
127130
}
128131

129132

133+
/**
134+
* @return static
135+
*/
136+
public function reportParseErrors($on = true)
137+
{
138+
$this->reportParseErrors = (bool) $on;
139+
return $this;
140+
}
141+
142+
130143
/**
131144
* Excludes path or paths from list.
132145
* @param string|string[] $path absolute path
@@ -185,7 +198,7 @@ private function refresh()
185198
if (isset($files[$file]) && $files[$file]['time'] == filemtime($file)) {
186199
$classes = $files[$file]['classes'];
187200
} else {
188-
$classes = $this->scanPhp(file_get_contents($file));
201+
$classes = $this->scanPhp($file);
189202
}
190203
$files[$file] = ['classes' => [], 'time' => filemtime($file)];
191204

@@ -255,7 +268,7 @@ private function updateFile($file)
255268
}
256269
}
257270

258-
$classes = is_file($file) ? $this->scanPhp(file_get_contents($file)) : [];
271+
$classes = is_file($file) ? $this->scanPhp($file) : [];
259272
foreach ($classes as $class) {
260273
$info = &$this->classes[$class];
261274
if (isset($info['file']) && @filemtime($info['file']) !== $info['time']) { // @ file may not exists
@@ -272,11 +285,12 @@ private function updateFile($file)
272285

273286
/**
274287
* Searches classes, interfaces and traits in PHP file.
275-
* @param string $code
288+
* @param string $file
276289
* @return string[]
277290
*/
278-
private function scanPhp($code)
291+
private function scanPhp($file)
279292
{
293+
$code = file_get_contents($file);
280294
$expected = false;
281295
$namespace = '';
282296
$level = $minLevel = 0;
@@ -295,6 +309,12 @@ private function scanPhp($code)
295309
? token_get_all($code, TOKEN_PARSE)
296310
: @token_get_all($code); // @ can be corrupted or can use newer syntax
297311
} catch (\ParseError $e) {
312+
if ($this->reportParseErrors) {
313+
$rp = new \ReflectionProperty($e, 'file');
314+
$rp->setAccessible(true);
315+
$rp->setValue($e, $file);
316+
throw $e;
317+
}
298318
$tokens = [];
299319
}
300320

0 commit comments

Comments
 (0)