Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #987 from ApiGen/better-parse-exception
[BetterReflection] improve parsing by Caching
- Loading branch information
Showing
24 changed files
with
370 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
|
||
ApiGen\Annotation\: | ||
resource: '..' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\Parser; | ||
|
||
use PhpParser\Lexer; | ||
use PhpParser\Parser; | ||
use PhpParser\ParserFactory as NikicParserFactory; | ||
use Roave\BetterReflection\SourceLocator\Ast\Parser\MemoizingParser; | ||
|
||
final class ParserFactory | ||
{ | ||
/** | ||
* @var Lexer | ||
*/ | ||
private $lexer; | ||
|
||
/** | ||
* @var NikicParserFactory | ||
*/ | ||
private $nikicParserFactory; | ||
|
||
public function __construct(Lexer $lexer, NikicParserFactory $nikicParserFactory) | ||
{ | ||
$this->lexer = $lexer; | ||
$this->nikicParserFactory = $nikicParserFactory; | ||
} | ||
|
||
public function create(): Parser | ||
{ | ||
$nativeParser = $this->nikicParserFactory->create(NikicParserFactory::PREFER_PHP7, $this->lexer); | ||
|
||
return new MemoizingParser($nativeParser); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/BetterReflection/src/SourceLocator/DirectorySourceLocatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\SourceLocator; | ||
|
||
use Roave\BetterReflection\SourceLocator\Ast\Locator as AstLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
final class DirectorySourceLocatorFactory | ||
{ | ||
/** | ||
* @var AstLocator | ||
*/ | ||
private $astLocator; | ||
/** | ||
* @var ProjectVendorSourceLocatorFactory | ||
*/ | ||
private $projectVendorSourceLocatorFactory; | ||
|
||
public function __construct( | ||
AstLocator $astLocator, | ||
ProjectVendorSourceLocatorFactory $projectVendorSourceLocatorFactory | ||
) { | ||
$this->astLocator = $astLocator; | ||
$this->projectVendorSourceLocatorFactory = $projectVendorSourceLocatorFactory; | ||
} | ||
|
||
/** | ||
* @param string[] $directories | ||
*/ | ||
public function createFromDirectories(array $directories): SourceLocator | ||
{ | ||
return new AggregateSourceLocator([ | ||
new DirectoriesSourceLocator($directories, $this->astLocator), | ||
$this->projectVendorSourceLocatorFactory->createFromDirectories($directories), | ||
]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/BetterReflection/src/SourceLocator/FallbackSourceLocatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\SourceLocator; | ||
|
||
use Roave\BetterReflection\SourceLocator\Ast\Locator as AstLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
final class FallbackSourceLocatorFactory | ||
{ | ||
/** | ||
* @var AstLocator | ||
*/ | ||
private $astLocator; | ||
|
||
/** | ||
* @var ProjectVendorSourceLocatorFactory | ||
*/ | ||
private $projectVendorSourceLocatorFactory; | ||
|
||
public function __construct( | ||
AstLocator $astLocator, | ||
ProjectVendorSourceLocatorFactory $projectVendorSourceLocatorFactory | ||
) { | ||
$this->astLocator = $astLocator; | ||
$this->projectVendorSourceLocatorFactory = $projectVendorSourceLocatorFactory; | ||
} | ||
|
||
/** | ||
* @param string[] $directories | ||
*/ | ||
public function createFromDirecotires(array $directories): SourceLocator | ||
{ | ||
return new AggregateSourceLocator([ | ||
new AutoloadSourceLocator($this->astLocator), | ||
new PhpInternalSourceLocator($this->astLocator), | ||
$this->projectVendorSourceLocatorFactory->createFromDirectories($directories), | ||
]); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/BetterReflection/src/SourceLocator/FileSourceLocatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\SourceLocator; | ||
|
||
use Roave\BetterReflection\SourceLocator\Ast\Locator as AstLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
final class FileSourceLocatorFactory | ||
{ | ||
/** | ||
* @var AstLocator | ||
*/ | ||
private $astLocator; | ||
|
||
public function __construct(AstLocator $astLocator) | ||
{ | ||
$this->astLocator = $astLocator; | ||
} | ||
|
||
/** | ||
* @param string[] $files | ||
*/ | ||
public function createFromFiles(array $files): SourceLocator | ||
{ | ||
$locators = []; | ||
|
||
foreach ($files as $file) { | ||
$locators[] = new SingleFileSourceLocator($file, $this->astLocator); | ||
} | ||
|
||
return new AggregateSourceLocator($locators); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/BetterReflection/src/SourceLocator/ProjectVendorSourceLocatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\SourceLocator; | ||
|
||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
/** | ||
* This service will try to find project's autoload, to be able to parse its classes. | ||
* It might be the same as autoload of ApiGen, but it might differ. | ||
* E.g. in case you create ApiGen with "composer create-project". | ||
*/ | ||
final class ProjectVendorSourceLocatorFactory | ||
{ | ||
/** | ||
* @param string[] $directories | ||
*/ | ||
public function createFromDirectories(array $directories): SourceLocator | ||
{ | ||
$composerSourceLocator = $this->tryToFindProjectAutoload($directories); | ||
if ($composerSourceLocator !== null) { | ||
return $composerSourceLocator; | ||
} | ||
|
||
return new AggregateSourceLocator; | ||
} | ||
|
||
/** | ||
* @param string[] $directories | ||
*/ | ||
private function tryToFindProjectAutoload(array $directories): ?ComposerSourceLocator | ||
{ | ||
foreach ($directories as $directory) { | ||
$autoload = dirname($directory) . '/vendor/autoload.php'; | ||
if (is_file($autoload)) { | ||
return new ComposerSourceLocator(include $autoload); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/BetterReflection/src/SourceLocator/SourceLocatorsFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ApiGen\BetterReflection\SourceLocator; | ||
|
||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
final class SourceLocatorsFactory | ||
{ | ||
/** | ||
* @var DirectorySourceLocatorFactory | ||
*/ | ||
private $directorySourceLocatorFactory; | ||
|
||
/** | ||
* @var FileSourceLocatorFactory | ||
*/ | ||
private $fileSourceLocatorFactory; | ||
|
||
/** | ||
* @var FallbackSourceLocatorFactory | ||
*/ | ||
private $fallbackSourceLocatorFactory; | ||
|
||
public function __construct( | ||
DirectorySourceLocatorFactory $directorySourceLocatorFactory, | ||
FileSourceLocatorFactory $fileSourceLocatorFactory, | ||
FallbackSourceLocatorFactory $fallbackSourceLocatorFactory | ||
) { | ||
$this->directorySourceLocatorFactory = $directorySourceLocatorFactory; | ||
$this->fileSourceLocatorFactory = $fileSourceLocatorFactory; | ||
$this->fallbackSourceLocatorFactory = $fallbackSourceLocatorFactory; | ||
} | ||
|
||
/** | ||
* @param string[] $directories | ||
* @param string[] $files | ||
*/ | ||
public function createFromDirectoriesAndFiles(array $directories, array $files): SourceLocator | ||
{ | ||
$locators = []; | ||
if ($directories) { | ||
$locators[] = $this->directorySourceLocatorFactory->createFromDirectories($directories); | ||
} | ||
|
||
if ($files) { | ||
$locators[] = $this->fileSourceLocatorFactory->createFromFiles($files); | ||
} | ||
|
||
$locators[] = $this->fallbackSourceLocatorFactory->createFromDirecotires($directories); | ||
|
||
return new AggregateSourceLocator($locators); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
|
||
ApiGen\BetterReflection\: | ||
resource: '..' | ||
|
||
# cached AST Locator | ||
Roave\BetterReflection\SourceLocator\Ast\PhpParserLocator: ~ | ||
Roave\BetterReflection\SourceLocator\Ast\Locator: | ||
alias: Roave\BetterReflection\SourceLocator\Ast\PhpParserLocator | ||
|
||
# nikic\PHP-Parser | ||
PhpParser\Parser: | ||
factory: ['@ApiGen\BetterReflection\Parser\ParserFactory', 'create'] | ||
PhpParser\Lexer: ~ | ||
PhpParser\ParserFactory: ~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
|
||
ApiGen\Element\: | ||
resource: '..' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
|
||
ApiGen\ModularConfiguration\: | ||
resource: '..' | ||
exclude: '../{Exception}' |
Oops, something went wrong.