diff --git a/src/Core/Manifest/ClassManifest.php b/src/Core/Manifest/ClassManifest.php index 9b64c8c4c..23bdc4d52 100644 --- a/src/Core/Manifest/ClassManifest.php +++ b/src/Core/Manifest/ClassManifest.php @@ -8,6 +8,7 @@ use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\NameResolver; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\ErrorHandler\ErrorHandler; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Cache\CacheFactory; use SilverStripe\Dev\TestOnly; @@ -490,11 +491,13 @@ class ClassManifest $changed = true; // Build from php file parser $fileContents = ClassContentRemover::remove_class_content($pathname); + // Not injectable, error handling is an implementation detail. + $errorHandler = new ClassManifestErrorHandler($pathname); try { - $stmts = $this->getParser()->parse($fileContents); + $stmts = $this->getParser()->parse($fileContents, $errorHandler); } catch (Error $e) { // if our mangled contents breaks, try again with the proper file contents - $stmts = $this->getParser()->parse(file_get_contents($pathname)); + $stmts = $this->getParser()->parse(file_get_contents($pathname), $errorHandler); } $this->getTraverser()->traverse($stmts); diff --git a/src/Core/Manifest/ClassManifestErrorHandler.php b/src/Core/Manifest/ClassManifestErrorHandler.php new file mode 100644 index 000000000..b5faf50da --- /dev/null +++ b/src/Core/Manifest/ClassManifestErrorHandler.php @@ -0,0 +1,33 @@ +pathname = $pathname; + } + + public function handleError(Error $error) + { + $newMessage = sprintf('%s in %s', $error->getRawMessage(), $this->pathname); + $error->setRawMessage($newMessage); + throw $error; + } +} diff --git a/src/Core/Manifest/ClassManifestErrorHandlerTest.php b/src/Core/Manifest/ClassManifestErrorHandlerTest.php new file mode 100644 index 000000000..0a9ec22a5 --- /dev/null +++ b/src/Core/Manifest/ClassManifestErrorHandlerTest.php @@ -0,0 +1,20 @@ +handleError($e); + } +}