mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
NEW Show file path on PHP parser exceptions
This commit is contained in:
parent
2f67be61c1
commit
25759ffc5f
@ -8,6 +8,7 @@ use PhpParser\NodeTraverser;
|
|||||||
use PhpParser\NodeVisitor\NameResolver;
|
use PhpParser\NodeVisitor\NameResolver;
|
||||||
use PhpParser\Parser;
|
use PhpParser\Parser;
|
||||||
use PhpParser\ParserFactory;
|
use PhpParser\ParserFactory;
|
||||||
|
use PhpParser\ErrorHandler\ErrorHandler;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
use SilverStripe\Core\Cache\CacheFactory;
|
use SilverStripe\Core\Cache\CacheFactory;
|
||||||
use SilverStripe\Dev\TestOnly;
|
use SilverStripe\Dev\TestOnly;
|
||||||
@ -490,11 +491,13 @@ class ClassManifest
|
|||||||
$changed = true;
|
$changed = true;
|
||||||
// Build from php file parser
|
// Build from php file parser
|
||||||
$fileContents = ClassContentRemover::remove_class_content($pathname);
|
$fileContents = ClassContentRemover::remove_class_content($pathname);
|
||||||
|
// Not injectable, error handling is an implementation detail.
|
||||||
|
$errorHandler = new ClassManifestErrorHandler($pathname);
|
||||||
try {
|
try {
|
||||||
$stmts = $this->getParser()->parse($fileContents);
|
$stmts = $this->getParser()->parse($fileContents, $errorHandler);
|
||||||
} catch (Error $e) {
|
} catch (Error $e) {
|
||||||
// if our mangled contents breaks, try again with the proper file contents
|
// 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);
|
$this->getTraverser()->traverse($stmts);
|
||||||
|
|
||||||
|
33
src/Core/Manifest/ClassManifestErrorHandler.php
Normal file
33
src/Core/Manifest/ClassManifestErrorHandler.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
20
src/Core/Manifest/ClassManifestErrorHandlerTest.php
Normal file
20
src/Core/Manifest/ClassManifestErrorHandlerTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user