Skip to content

Commit

Permalink
Add file contents to the Scoper (#146)
Browse files Browse the repository at this point in the history
Continuing some work for box-project/box#31. The Scoper now takes the file contents as an argument. This allows the scoper to be used with existent files (e.g. virtual files) or files for which we are modifying the content like in Box.

Another benefit is that it removes the Scopers dependency to the file system layer.
  • Loading branch information
theofidry authored Feb 2, 2018
1 parent cb82944 commit a59b5fb
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 239 deletions.
4 changes: 1 addition & 3 deletions src/Console/Command/AddPrefixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,8 @@ private function scopeFile(
bool $stopOnFailure,
ConsoleLogger $logger
): void {
// TODO: use $inputContents instead of doing file_get_contents in the Scopers

try {
$scoppedContent = $this->scoper->scope($inputFilePath, $prefix, $patchers, $whitelist, $globalWhitelister);
$scoppedContent = $this->scoper->scope($inputFilePath, $inputContents, $prefix, $patchers, $whitelist, $globalWhitelister);
} catch (Throwable $error) {
$exception = new ParsingException(
sprintf(
Expand Down
5 changes: 3 additions & 2 deletions src/Scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface Scoper
* Scope AKA. apply the given prefix to the file in the appropriate way.
*
* @param string $filePath File to scope
* @param string $contents File contents
* @param string $prefix Prefix to apply to the file
* @param callable[] $patchers
* @param string[] $whitelist List of classes to exclude from the scoping.
Expand All @@ -31,7 +32,7 @@ interface Scoper
*
* @throws ParsingException
*
* @return string Content of the file with the prefix applied
* @return string Contents of the file with the prefix applied
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string;
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string;
}
9 changes: 3 additions & 6 deletions src/Scoper/Composer/InstalledPackagesScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ public function __construct(Scoper $decoratedScoper)
*
* {@inheritdoc}
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
{
if (1 !== preg_match(self::$filePattern, $filePath)) {
return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister);
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister);
}

$decodedJson = json_decode(
file_get_contents($filePath),
true
);
$decodedJson = json_decode($contents, true);

$decodedJson = $this->prefixLockPackages($decodedJson, $prefix);

Expand Down
9 changes: 3 additions & 6 deletions src/Scoper/Composer/JsonFileScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ public function __construct(Scoper $decoratedScoper)
*
* {@inheritdoc}
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
{
if (1 !== preg_match('/composer\.json$/', $filePath)) {
return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister);
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister);
}

$decodedJson = json_decode(
file_get_contents($filePath),
true
);
$decodedJson = json_decode($contents, true);

$decodedJson = AutoloadPrefixer::prefixPackageAutoloads($decodedJson, $prefix);

Expand Down
4 changes: 2 additions & 2 deletions src/Scoper/NullScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class NullScoper implements Scoper
/**
* @inheritdoc
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
{
return file_get_contents($filePath);
return $contents;
}
}
10 changes: 5 additions & 5 deletions src/Scoper/PatchScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public function __construct(Scoper $decoratedScoper)
/**
* @inheritdoc
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
{
$content = $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister);
$contents = $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister);

return array_reduce(
$patchers,
function (string $content, callable $patcher) use ($filePath, $prefix): string {
return $patcher($filePath, $prefix, $content);
function (string $contents, callable $patcher) use ($filePath, $prefix): string {
return $patcher($filePath, $prefix, $contents);
},
$content
$contents
);
}
}
16 changes: 6 additions & 10 deletions src/Scoper/PhpScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ public function __construct(Parser $parser, Scoper $decoratedScoper, TraverserFa
*
* @throws PhpParserError
*/
public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string
{
if (false === $this->isPhpFile($filePath)) {
return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister);
if (false === $this->isPhpFile($filePath, $contents)) {
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist, $globalWhitelister);
}

$content = file_get_contents($filePath);

$statements = $this->parser->parse($content);
$statements = $this->parser->parse($contents);

$traverser = $this->traverserFactory->create($prefix, $whitelist, $globalWhitelister);

Expand All @@ -62,7 +60,7 @@ public function scope(string $filePath, string $prefix, array $patchers, array $
return $prettyPrinter->prettyPrintFile($statements)."\n";
}

private function isPhpFile(string $filePath): bool
private function isPhpFile(string $filePath, string $contents): bool
{
if (1 === preg_match(self::FILE_PATH_PATTERN, $filePath)) {
return true;
Expand All @@ -72,8 +70,6 @@ private function isPhpFile(string $filePath): bool
return false;
}

$content = file_get_contents($filePath);

return 1 === preg_match(self::PHP_BINARY, $content);
return 1 === preg_match(self::PHP_BINARY, $contents);
}
}
31 changes: 30 additions & 1 deletion tests/Console/Command/AddPrefixCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Filesystem\Filesystem;
use function file_get_contents;
use function Humbug\PhpScoper\escape_path;
use function Humbug\PhpScoper\make_tmp_dir;
use function Humbug\PhpScoper\remove_dir;
Expand Down Expand Up @@ -195,9 +196,12 @@ public function test_scope_the_given_paths()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -255,10 +259,13 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

if (null !== $prefixedContents) {
$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand All @@ -272,6 +279,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file()
$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand All @@ -280,7 +288,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file()
->willThrow(new \RuntimeException('Scoping of the file failed'))
;

$this->fileSystemProphecy->dumpFile($outputPath, file_get_contents($inputPath))->shouldBeCalled();
$this->fileSystemProphecy->dumpFile($outputPath, $inputContents)->shouldBeCalled();
}
}

Expand Down Expand Up @@ -327,9 +335,12 @@ public function test_do_not_scope_duplicated_given_paths()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -385,11 +396,13 @@ public function test_scope_the_given_paths_and_the_ones_found_by_the_finder()
$inputPath = realpath($inputPath);
$outputPath = escape_path($outputPath);

$inputContents = file_get_contents($inputPath);
$prefixedFileContents = 'Random string';

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -434,6 +447,7 @@ public function test_applies_a_random_prefix_when_none_given()

$this->scoperProphecy
->scope(
Argument::any(),
Argument::any(),
Argument::that(
function (string $prefix): bool {
Expand Down Expand Up @@ -497,9 +511,12 @@ public function test_scope_the_current_working_directory_if_no_path_given()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -545,6 +562,7 @@ public function test_prefix_can_end_by_a_backslash()

$this->scoperProphecy
->scope(
Argument::any(),
Argument::any(),
'MyPrefix',
[],
Expand Down Expand Up @@ -588,6 +606,7 @@ public function test_prefix_can_end_by_multiple_backslashes()

$this->scoperProphecy
->scope(
Argument::any(),
Argument::any(),
'MyPrefix',
[],
Expand Down Expand Up @@ -642,9 +661,12 @@ public function test_an_output_directory_can_be_given()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($outDir.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -702,9 +724,12 @@ public function test_relative_output_directory_are_made_absolute()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.DIRECTORY_SEPARATOR.$outDir.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
[],
[],
Expand Down Expand Up @@ -822,9 +847,12 @@ public function test_attempts_to_use_patch_file_in_current_directory()
$inputPath = escape_path($root.'/'.$expectedFile);
$outputPath = escape_path($this->tmp.'/'.$expectedFile);

$inputContents = file_get_contents($inputPath);

$this->scoperProphecy
->scope(
$inputPath,
$inputContents,
'MyPrefix',
Argument::that(function ($arg) use (&$patchersFound) {
$patchersFound = $arg;
Expand Down Expand Up @@ -914,6 +942,7 @@ public function test_can_scope_projects_with_invalid_files()
$this->scoperProphecy
->scope(
$inputPath,
$fileContents,
'MyPrefix',
[],
[],
Expand Down
Loading

0 comments on commit a59b5fb

Please sign in to comment.