Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finders #89

Merged
merged 3 commits into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added fixtures/set012/dir/file1.php
Empty file.
Empty file added fixtures/set012/dir/file2.php
Empty file.
11 changes: 11 additions & 0 deletions fixtures/set012/scoper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\Finder\Finder;

return [
'finders' => [
(new Finder())
->files()
->in(__DIR__.DIRECTORY_SEPARATOR.'dir'),
],
];
33 changes: 28 additions & 5 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$input->getArgument(self::PATH_ARG)
);

$paths = $this->retrievePaths($input, $config);

try {
$this->handle->__invoke(
$input->getOption(self::PREFIX_OPT),
$input->getArgument(self::PATH_ARG),
$paths,
$input->getOption(self::OUTPUT_DIR_OPT),
$config->getPatchers(),
$config->getGlobalNamespaceWhitelisters(),
Expand Down Expand Up @@ -194,10 +196,6 @@ function (string $path) use ($cwd, $fileSystem) {
$input->getArgument(self::PATH_ARG)
);

if (0 === count($paths)) {
$paths[] = $cwd;
}

$input->setArgument(self::PATH_ARG, $paths);
}

Expand Down Expand Up @@ -305,6 +303,31 @@ private function retrieveConfig(InputInterface $input, OutputStyle $io): Configu
return Configuration::load($configFile);
}

/**
* @param InputInterface $input
* @param Configuration $configuration
*
* @return string[] List of absolute paths
*/
private function retrievePaths(InputInterface $input, Configuration $configuration): array
{
$paths = $input->getArgument(self::PATH_ARG);

$finders = $configuration->getFinders();

foreach ($finders as $finder) {
foreach ($finder as $file) {
$paths[] = $file->getRealPath();
}
}

if (0 === count($paths)) {
return [getcwd()];
}

return $paths;
}

private function makeAbsolutePath(string $path): string
{
if (false === $this->fileSystem->isAbsolutePath($path)) {
Expand Down
134 changes: 112 additions & 22 deletions src/Console/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,44 @@
namespace Humbug\PhpScoper\Console;

use InvalidArgumentException;
use Symfony\Component\Finder\Finder;

final class Configuration
{
/** @internal */
const FINDER_KEYWORD = 'finders';

/** @internal */
const PATCHERS_KEYWORD = 'patchers';

/** @internal */
const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist';

/** @internal */
const KEYWORDS = [
self::FINDER_KEYWORD,
self::PATCHERS_KEYWORD,
self::GLOBAL_NAMESPACE_KEYWORD,
];

private $path;
private $patchers;
private $finders;
private $globalNamespaceWhitelisters;

/**
* @param string|null $path Absolute path to the configuration file loaded
* @param Finder[] $finders
* @param callable[] $patchers List of closures which can alter the content of the files being
* scoped
* @param callable[]|string[] $globalNamespace List of class names from the global namespace that should be scoped
* or closures filtering if the class should be scoped or not
*/
private function __construct(string $path = null, array $patchers, array $globalNamespace)
private function __construct(string $path = null, array $finders, array $patchers, array $globalNamespace)
{
$this->path = $path;
$this->patchers = $patchers;
$this->finders = $finders;
$this->globalNamespaceWhitelisters = $globalNamespace;
}

Expand All @@ -44,7 +64,7 @@ private function __construct(string $path = null, array $patchers, array $global
public static function load(string $path = null): self
{
if (null === $path) {
return new self(null, [], []);
return new self(null, [], [], []);
}

$config = include $path;
Expand All @@ -58,17 +78,28 @@ public static function load(string $path = null): self
);
}

self::validateConfigKeys($config);

$finders = self::retrieveFinders($config);
$patchers = self::retrievePatchers($config);
$globalNamespace = self::retrieveGlobalNamespaceWhitelisters($config);

return new self($path, $patchers, $globalNamespace);
return new self($path, $finders, $patchers, $globalNamespace);
}

public function getPath(): string
{
return $this->path;
}

/**
* @return Finder[]
*/
public function getFinders(): array
{
return $this->finders;
}

/**
* @return callable[]
*/
Expand All @@ -85,13 +116,68 @@ public function getGlobalNamespaceWhitelisters()
return $this->globalNamespaceWhitelisters;
}

private static function validateConfigKeys(array $config)
{
array_map(
['self', 'validateConfigKey'],
array_keys($config)
);
}

private static function validateConfigKey(string $key)
{
if (false === in_array($key, self::KEYWORDS)) {
throw new InvalidArgumentException(
sprintf(
'Invalid configuration key value "%s" found.',
$key
)
);
}
}

private static function retrieveFinders(array $config): array
{
if (false === array_key_exists(self::FINDER_KEYWORD, $config)) {
return [];
}

$finders = $config[self::FINDER_KEYWORD];

if (false === is_array($finders)) {
throw new InvalidArgumentException(
sprintf(
'Expected finders to be an array of "%s", found "%s" instead.',
Finder::class,
gettype($finders)
)
);
}

foreach ($finders as $index => $finder) {
if ($finder instanceof Finder) {
continue;
}

throw new InvalidArgumentException(
sprintf(
'Expected finders to be an array of "%s", the "%d" element is not.',
Finder::class,
$index
)
);
}

return $finders;
}

private static function retrievePatchers(array $config): array
{
if (false === array_key_exists('patchers', $config)) {
if (false === array_key_exists(self::PATCHERS_KEYWORD, $config)) {
return [];
}

$patchers = $config['patchers'];
$patchers = $config[self::PATCHERS_KEYWORD];

if (false === is_array($patchers)) {
throw new InvalidArgumentException(
Expand All @@ -103,26 +189,28 @@ private static function retrievePatchers(array $config): array
}

foreach ($patchers as $index => $patcher) {
if (false === is_callable($patcher)) {
throw new InvalidArgumentException(
sprintf(
'Expected patchers to be an array of callables, the "%d" element is not.',
$index
)
);
if (is_callable($patcher)) {
continue;
}

throw new InvalidArgumentException(
sprintf(
'Expected patchers to be an array of callables, the "%d" element is not.',
$index
)
);
}

return $patchers;
}

private static function retrieveGlobalNamespaceWhitelisters(array $config): array
{
if (false === array_key_exists('global_namespace_whitelist', $config)) {
if (false === array_key_exists(self::GLOBAL_NAMESPACE_KEYWORD, $config)) {
return [];
}

$globalNamespace = $config['global_namespace_whitelist'];
$globalNamespace = $config[self::GLOBAL_NAMESPACE_KEYWORD];

if (false === is_array($globalNamespace)) {
throw new InvalidArgumentException(
Expand All @@ -134,15 +222,17 @@ private static function retrieveGlobalNamespaceWhitelisters(array $config): arra
}

foreach ($globalNamespace as $index => $className) {
if (false === is_string($className) && false === is_callable($className)) {
throw new InvalidArgumentException(
sprintf(
'Expected "global_namespace" to be an array of callables or strings, the "%d" element '
.'is not.',
$index
)
);
if (is_string($className) || is_callable($className)) {
continue;
}

throw new InvalidArgumentException(
sprintf(
'Expected "global_namespace" to be an array of callables or strings, the "%d" element '
.'is not.',
$index
)
);
}

return $globalNamespace;
Expand Down
50 changes: 50 additions & 0 deletions tests/Console/Command/AddPrefixCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,56 @@ public function test_scope_the_given_paths()
$this->handleProphecy->__invoke(Argument::cetera())->shouldHaveBeenCalledTimes(1);
}

public function test_scope_the_given_paths_and_the_ones_found_by_the_finder()
{
chdir($rootPath = escape_path(self::FIXTURE_PATH.'/set012'));

$input = [
'add-prefix',
'--prefix' => 'MyPrefix',
'paths' => [
escape_path('/path/to/dir1'),
escape_path('/path/to/dir2'),
escape_path('/path/to/file'),
],
'--output-dir' => $this->tmp,
'--no-interaction',
];

$this->fileSystemProphecy->isAbsolutePath('scoper.inc.php')->willReturn(false);
$this->fileSystemProphecy->isAbsolutePath(Argument::cetera())->willReturn(true);
$this->fileSystemProphecy->exists(Argument::cetera())->willReturn(false);

$this->handleProphecy
->__invoke(
'MyPrefix',
[
escape_path('/path/to/dir1'),
escape_path('/path/to/dir2'),
escape_path('/path/to/file'),
realpath(escape_path($rootPath.'/dir/file1.php')),
realpath(escape_path($rootPath.'/dir/file2.php')),
],
$this->tmp,
Argument::type('array'),
Argument::type('array'),
false,
Argument::type(ConsoleLogger::class)
)
->shouldBeCalled()
;

$this->appTester->run($input);

$this->assertSame(0, $this->appTester->getStatusCode());

$this->fileSystemProphecy->isAbsolutePath('scoper.inc.php')->shouldHaveBeenCalledTimes(1);
$this->fileSystemProphecy->isAbsolutePath(Argument::cetera())->shouldHaveBeenCalledTimes(5);
$this->fileSystemProphecy->exists(Argument::cetera())->shouldHaveBeenCalledTimes(1);

$this->handleProphecy->__invoke(Argument::cetera())->shouldHaveBeenCalledTimes(1);
}

public function test_applies_a_random_prefix_when_none_given()
{
$input = [
Expand Down