Merge pull request #8413 from open-sausages/pulls/4/classmanifest-exceptions

NEW Show file path on PHP parser exceptions
This commit is contained in:
Loz Calver 2018-09-28 10:20:38 +02:00 committed by GitHub
commit 289f65fa05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 2 deletions

View File

@ -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);

View File

@ -0,0 +1,33 @@
<?php
namespace SilverStripe\Core\Manifest;
use PhpParser\Error;
use PhpParser\ErrorHandler;
/**
* Error handler which throws, but retains the original path context.
* For parsing errors, this is essential information to identify the issue.
*/
class ClassManifestErrorHandler implements ErrorHandler
{
/**
* @var String
*/
protected $pathname;
/**
* @param String $pathname
*/
public function __construct($pathname)
{
$this->pathname = $pathname;
}
public function handleError(Error $error)
{
$newMessage = sprintf('%s in %s', $error->getRawMessage(), $this->pathname);
$error->setRawMessage($newMessage);
throw $error;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace SilverStripe\Core\Tests\Manifest;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Manifest\ClassManifestErrorHandler;
use PhpParser\Error;
class ClassManifestErrorHandlerTest extends SapphireTest
{
/**
* @expectedException \PhpParser\Error
* @expectedExceptionMessage my error in /my/path
*/
public function testIncludesPathname()
{
$h = new ClassManifestErrorHandler('/my/path');
$e = new Error('my error');
$h->handleError($e);
}
}