-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Introduce a Parser and Printer factory (#1047)
- Loading branch information
Showing
20 changed files
with
379 additions
and
95 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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\PhpParser\Parser; | ||
|
||
use PhpParser\Parser; | ||
use PhpParser\PhpVersion; | ||
|
||
interface ParserFactory | ||
{ | ||
public function createParser(?PhpVersion $phpVersion = null): Parser; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\PhpParser\Parser; | ||
|
||
use PhpParser\Lexer; | ||
use PhpParser\Lexer\Emulative; | ||
use PhpParser\Parser; | ||
use PhpParser\Parser\Php7; | ||
use PhpParser\Parser\Php8; | ||
use PhpParser\PhpVersion; | ||
|
||
final class StandardParserFactory implements ParserFactory | ||
{ | ||
public function createParser(?PhpVersion $phpVersion = null): Parser | ||
{ | ||
$version = $phpVersion ?? PhpVersion::getHostVersion(); | ||
|
||
$lexer = $version->isHostVersion() | ||
? new Lexer() | ||
: new Emulative($version); | ||
|
||
return $version->id >= 80_000 | ||
? new Php8($lexer, $version) | ||
: new Php7($lexer, $version); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\PhpParser\Printer; | ||
|
||
use PhpParser\PhpVersion; | ||
|
||
interface PrinterFactory | ||
{ | ||
public function createPrinter(?PhpVersion $phpVersion = null): Printer; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\PhpParser\Printer; | ||
|
||
use PhpParser\PhpVersion; | ||
use PhpParser\PrettyPrinter\Standard; | ||
|
||
final class StandardPrinterFactory implements PrinterFactory | ||
{ | ||
public function createPrinter(?PhpVersion $phpVersion = null): Printer | ||
{ | ||
return new StandardPrinter( | ||
new Standard([ | ||
'phpVersion' => $phpVersion, | ||
]), | ||
); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Scoper\Factory; | ||
|
||
use Humbug\PhpScoper\Configuration\Configuration; | ||
use Humbug\PhpScoper\Scoper\Scoper; | ||
use Humbug\PhpScoper\Symbol\SymbolsRegistry; | ||
use PhpParser\PhpVersion; | ||
|
||
interface ScoperFactory | ||
{ | ||
public function createScoper( | ||
Configuration $configuration, | ||
SymbolsRegistry $symbolsRegistry, | ||
?PhpVersion $phpVersion = null, | ||
): Scoper; | ||
} |
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,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Scoper\Factory; | ||
|
||
use Humbug\PhpScoper\Configuration\Configuration; | ||
use Humbug\PhpScoper\PhpParser\Parser\ParserFactory; | ||
use Humbug\PhpScoper\PhpParser\Printer\PrinterFactory; | ||
use Humbug\PhpScoper\PhpParser\TraverserFactory; | ||
use Humbug\PhpScoper\Scoper\Composer\AutoloadPrefixer; | ||
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper; | ||
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper; | ||
use Humbug\PhpScoper\Scoper\NullScoper; | ||
use Humbug\PhpScoper\Scoper\PatchScoper; | ||
use Humbug\PhpScoper\Scoper\PhpScoper; | ||
use Humbug\PhpScoper\Scoper\Scoper; | ||
use Humbug\PhpScoper\Scoper\SymfonyScoper; | ||
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory; | ||
use Humbug\PhpScoper\Symbol\SymbolsRegistry; | ||
use PhpParser\PhpVersion; | ||
|
||
final readonly class StandardScoperFactory implements ScoperFactory | ||
{ | ||
public function __construct( | ||
private EnrichedReflectorFactory $enrichedReflectorFactory, | ||
private ParserFactory $parserFactory, | ||
private PrinterFactory $printerFactory, | ||
) { | ||
} | ||
|
||
public function createScoper( | ||
Configuration $configuration, | ||
SymbolsRegistry $symbolsRegistry, | ||
?PhpVersion $phpVersion = null, | ||
): Scoper { | ||
$prefix = $configuration->getPrefix(); | ||
$symbolsConfiguration = $configuration->getSymbolsConfiguration(); | ||
$enrichedReflector = $this->enrichedReflectorFactory->create($symbolsConfiguration); | ||
|
||
$parser = $this->parserFactory->createParser($phpVersion); | ||
$printer = $this->printerFactory->createPrinter($phpVersion); | ||
|
||
$autoloadPrefixer = new AutoloadPrefixer( | ||
$prefix, | ||
$enrichedReflector, | ||
); | ||
|
||
return new PatchScoper( | ||
new PhpScoper( | ||
$parser, | ||
new JsonFileScoper( | ||
new InstalledPackagesScoper( | ||
new SymfonyScoper( | ||
new NullScoper(), | ||
$prefix, | ||
$enrichedReflector, | ||
$symbolsRegistry, | ||
), | ||
$autoloadPrefixer, | ||
), | ||
$autoloadPrefixer, | ||
), | ||
new TraverserFactory( | ||
$enrichedReflector, | ||
$prefix, | ||
$symbolsRegistry, | ||
), | ||
$printer, | ||
), | ||
$prefix, | ||
$configuration->getPatcher(), | ||
); | ||
} | ||
} |
Oops, something went wrong.