Permalink
Browse files

Use an application factory (#260)

  • Loading branch information...
theofidry committed Aug 10, 2018
1 parent d458756 commit f595614c73243fa084caa30a6e75f2442277e631
Showing with 139 additions and 87 deletions.
  1. +102 −0 src/Console/ApplicationFactory.php
  2. +22 −87 src/functions.php
  3. +15 −0 tests/functions.php
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Humbug\PhpScoper\Console;
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
use Humbug\PhpScoper\Console\Command\InitCommand;
use Humbug\PhpScoper\PhpParser\TraverserFactory;
use Humbug\PhpScoper\Reflector;
use Humbug\PhpScoper\Scoper;
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 PackageVersions\Versions;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\FunctionReflector;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Symfony\Component\Filesystem\Filesystem;
class ApplicationFactory
{
public function create(): Application
{
$app = new Application('PHP Scoper', static::getVersion());
$app->addCommands([
new AddPrefixCommand(
new Filesystem(),
static::createScoper()
),
new InitCommand(),
]);
return $app;
}
protected static function getVersion(): string
{
if (0 === strpos(__FILE__, 'phar:')) {
return '@git_version_placeholder@';
}
$rawVersion = Versions::getVersion('humbug/php-scoper');
[$prettyVersion, $commitHash] = explode('@', $rawVersion);
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
}
protected static function createScoper(): Scoper
{
return new PatchScoper(
new PhpScoper(
static::createParser(),
new JsonFileScoper(
new InstalledPackagesScoper(
new NullScoper()
)
),
new TraverserFactory(static::createReflector())
)
);
}
protected static function createParser(): Parser
{
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
}
protected static function createReflector(): Reflector
{
$phpParser = static::createParser();
$astLocator = new Locator($phpParser);
$sourceLocator = new MemoizingSourceLocator(
new PhpInternalSourceLocator($astLocator)
);
$classReflector = new ClassReflector($sourceLocator);
return new Reflector(
$classReflector,
new FunctionReflector($sourceLocator, $classReflector)
);
}
}
View
@@ -15,108 +15,43 @@
namespace Humbug\PhpScoper;
use Humbug\PhpScoper\Console\Application;
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
use Humbug\PhpScoper\Console\Command\InitCommand;
use Humbug\PhpScoper\PhpParser\TraverserFactory;
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\Console\ApplicationFactory;
use Iterator;
use PackageVersions\Versions;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\FunctionReflector;
use Roave\BetterReflection\SourceLocator\Ast\Locator;
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Filesystem\Filesystem;
use function array_map;
use function count;
use function is_array;
use function is_object;
use function is_scalar;
use function is_string;
use function method_exists;
use function serialize;
use function strlen;
use function strpos;
use function substr;
use function unserialize;
// TODO: register this file to the list of functions if possible to be autoloaded
/**
* @private
*/
function create_application(): SymfonyApplication
function create_application(): Application
{
$app = new Application('PHP Scoper', get_version());
$app->addCommands([
new AddPrefixCommand(
new Filesystem(),
create_scoper()
),
new InitCommand(),
]);
return $app;
}
/**
* @private
*/
function get_version(): string
{
if ('phar:' === substr(__FILE__, 0, 5)) {
return '@git_version_placeholder@';
}
$rawVersion = Versions::getVersion('humbug/php-scoper');
[$prettyVersion, $commitHash] = explode('@', $rawVersion);
return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
return (new ApplicationFactory())->create();
}
/**
* @private
*
* @deprecated Will be removed in future releases.
*/
function create_scoper(): Scoper
{
return new PatchScoper(
new PhpScoper(
create_parser(),
new JsonFileScoper(
new InstalledPackagesScoper(
new NullScoper()
)
),
new TraverserFactory(create_reflector())
)
);
}
/**
* @private
*/
function create_parser(): Parser
{
return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
}
function create_reflector(): Reflector
{
$phpParser = create_parser();
$astLocator = new Locator($phpParser);
$sourceLocator = new MemoizingSourceLocator(
new PhpInternalSourceLocator($astLocator)
);
$classReflector = new ClassReflector($sourceLocator);
return new Reflector(
$classReflector,
new FunctionReflector($sourceLocator, $classReflector)
);
return (new class() extends ApplicationFactory {
public static function createScoper(): Scoper
{
return parent::createScoper();
}
})::createScoper();
}
/**
@@ -140,7 +75,7 @@ function get_common_path(array $paths): string
foreach ($paths as $path) {
if (substr($path, $lastOffset, $dirLen) !== $dir) {
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
$common = substr($common, 0, strlen($common) - 1);
$common = substr($common, 0, -1);
}
return $common;
@@ -154,7 +89,7 @@ function get_common_path(array $paths): string
$common = substr($common, 0, -1);
if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
$common = substr($common, 0, strlen($common) - 1);
$common = substr($common, 0, -1);
}
return $common;
View
@@ -15,7 +15,9 @@
namespace Humbug\PhpScoper;
use Closure;
use Humbug\PhpScoper\Console\ApplicationFactory;
use LogicException;
use PhpParser\Parser;
use Symfony\Component\Filesystem\Filesystem;
/**
@@ -72,3 +74,16 @@ function create_fake_patcher(): Closure
throw new LogicException('Did not expect to be called');
};
}
/**
* @private
*/
function create_parser(): Parser
{
return (new class() extends ApplicationFactory {
public static function createParser(): Parser
{
return parent::createParser();
}
})::createParser();
}

0 comments on commit f595614

Please sign in to comment.