Skip to content

Commit

Permalink
Simplify PhpScoper integration & Make the scoping prefix unique (#121)
Browse files Browse the repository at this point in the history
Add an adapter for PHP-Scoper which:

- Removes the dependency on the configuration API
- Simplify the scoping signature
- Avoid to pass around the whole PHP-Scoper config

The new integration also ensure the scoping is done with a random prefix.
  • Loading branch information
theofidry authored Apr 16, 2018
1 parent 18882f5 commit 0808fff
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 104 deletions.
31 changes: 31 additions & 0 deletions fixtures/PhpScoper/FakePhpScoper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\PhpScoper;

use Humbug\PhpScoper\Scoper;
use KevinGH\Box\NotCallable;

final class FakePhpScoper implements Scoper
{
use NotCallable;

/**
* {@inheritdoc}
*/
public function scope(string $filePath, string $contents, string $prefix, array $patchers, array $whitelist): string
{
$this->__call(__METHOD__, func_get_args());
}
}
12 changes: 7 additions & 5 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
namespace KevinGH\Box;

use Assert\Assertion;
use Humbug\PhpScoper\Console\Configuration as PhpScoperConfiguration;
use KevinGH\Box\Compactor\PhpScoper;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\PhpScoper\NullScoper;
use KevinGH\Box\PhpScoper\Scoper;
use Phar;
use RecursiveDirectoryIterator;
use SplFileInfo;
Expand Down Expand Up @@ -73,9 +74,9 @@ final class Box
private $mapFile;

/**
* @var null|PhpScoperConfiguration
* @var Scoper
*/
private $phpScoperConfig;
private $scoper;

private function __construct(Phar $phar, string $file)
{
Expand All @@ -84,6 +85,7 @@ private function __construct(Phar $phar, string $file)

$this->basePath = getcwd();
$this->mapFile = function (): void { };
$this->scoper = new NullScoper();
}

/**
Expand Down Expand Up @@ -117,7 +119,7 @@ public function registerCompactors(array $compactors): void

foreach ($this->compactors as $compactor) {
if ($compactor instanceof PhpScoper) {
$this->phpScoperConfig = $compactor->getConfiguration();
$this->scoper = $compactor->getScoper();

break;
}
Expand Down Expand Up @@ -203,7 +205,7 @@ function ($file): string {
}

// Dump autoload without dev dependencies
ComposerOrchestrator::dumpAutoload($this->phpScoperConfig);
ComposerOrchestrator::dumpAutoload($this->scoper->getWhitelist(), $this->scoper->getPrefix());

chdir($cwd);

Expand Down
20 changes: 5 additions & 15 deletions src/Compactor/PhpScoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@

namespace KevinGH\Box\Compactor;

use Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
use Humbug\PhpScoper\Scoper;
use KevinGH\Box\Compactor;
use KevinGH\Box\PhpScoper\Scoper;
use Throwable;
use function uniqid;

/**
* @private
*/
final class PhpScoper implements Compactor
{
private $scoper;
private $config;

public function __construct(Scoper $scoper, PhpScoperConfiguration $config)
public function __construct(Scoper $scoper)
{
$this->scoper = $scoper;
$this->config = $config;
}

/**
Expand All @@ -40,20 +36,14 @@ public function __construct(Scoper $scoper, PhpScoperConfiguration $config)
public function compact(string $file, string $contents): string
{
try {
return $this->scoper->scope(
$file,
$contents,
'_HumbugBox',
$this->config->getPatchers(),
$this->config->getWhitelist()
);
return $this->scoper->scope($file, $contents);
} catch (Throwable $throwable) {
return $contents;
}
}

public function getConfiguration(): PhpScoperConfiguration
public function getScoper(): Scoper
{
return $this->config;
return $this->scoper;
}
}
22 changes: 16 additions & 6 deletions src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Composer\Factory;
use Composer\IO\NullIO;
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator;
use Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
use InvalidArgumentException;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\file_contents;
Expand All @@ -33,7 +32,10 @@ private function __construct()
{
}

public static function dumpAutoload(?PhpScoperConfiguration $phpScoperConfig): void
/**
* @param string[] $whitelist
*/
public static function dumpAutoload(array $whitelist, string $prefix): void
{
try {
$composer = Factory::create(new NullIO(), null, true);
Expand All @@ -56,22 +58,30 @@ public static function dumpAutoload(?PhpScoperConfiguration $phpScoperConfig): v

$generator->dump($composerConfig, $localRepository, $package, $installationManager, 'composer', true);

if (null !== $phpScoperConfig) {
if ('' !== $prefix) {
$autoloadFile = $composerConfig->get('vendor-dir').'/autoload.php';

$autoloadContents = self::generateAutoloadStatements(
$phpScoperConfig,
$whitelist,
$prefix,
file_contents($autoloadFile)
);

dump_file($autoloadFile, $autoloadContents);
}
}

private static function generateAutoloadStatements(PhpScoperConfiguration $config, string $autoload): string
/**
* @param string[] $whitelist
*/
private static function generateAutoloadStatements(array $whitelist, string $prefix, string $autoload): string
{
// TODO: make prefix configurable: https://github.com/humbug/php-scoper/issues/178
$whitelistStatements = (new ScoperAutoloadGenerator($config->getWhitelist()))->dump('_HumbugBox');
$whitelistStatements = (new ScoperAutoloadGenerator($whitelist))->dump($prefix);

if ([] === $whitelistStatements) {
return $autoload;
}

$whitelistStatements = preg_replace(
'/(\\$loader \= .*)|(return \\$loader;)/',
Expand Down
14 changes: 11 additions & 3 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
use Humbug\PhpScoper\Configuration as PhpScoperConfiguration;
use InvalidArgumentException;
use KevinGH\Box\Compactor\Php;
use KevinGH\Box\Compactor\PhpScoper;
use KevinGH\Box\Compactor\PhpScoper as PhpScoperCompactor;
use KevinGH\Box\Composer\ComposerConfiguration;
use KevinGH\Box\PhpScoper\SimpleScoper;
use Phar;
use RuntimeException;
use SplFileInfo;
Expand Down Expand Up @@ -903,10 +904,17 @@ function (string $class) use ($raw, $basePath): Compactor {
return self::createPhpCompactor($raw);
}

if (PhpScoper::class === $class) {
if (PhpScoperCompactor::class === $class) {
$phpScoperConfig = self::retrievePhpScoperConfig($raw, $basePath);

return new PhpScoper(create_scoper(), $phpScoperConfig);
return new PhpScoperCompactor(
new SimpleScoper(
create_scoper(),
uniqid('_HumbugBox', false),
$phpScoperConfig->getWhitelist(),
$phpScoperConfig->getPatchers()
)
);
}

return new $class();
Expand Down
39 changes: 39 additions & 0 deletions src/PhpScoper/NullScoper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\PhpScoper;

final class NullScoper implements Scoper
{
/**
* {@inheritdoc}
*/
public function scope(string $filePath, string $contents): string
{
return $contents;
}

/**
* @return string[]
*/
public function getWhitelist(): array
{
return [];
}

public function getPrefix(): string
{
return '';
}
}
35 changes: 35 additions & 0 deletions src/PhpScoper/Scoper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\PhpScoper;

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
*
* @return string Contents of the file with the prefix applied
*/
public function scope(string $filePath, string $contents): string;

/**
* @return string[]
*/
public function getWhitelist(): array;

public function getPrefix(): string;
}
63 changes: 63 additions & 0 deletions src/PhpScoper/SimpleScoper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\PhpScoper;

use Humbug\PhpScoper\Scoper as PhpScoper;

final class SimpleScoper implements Scoper
{
private $scoper;
private $prefix;
private $whitelist;
private $patchers;

public function __construct(PhpScoper $scoper, string $prefix, array $whitelist, array $patchers)
{
$this->scoper = $scoper;
$this->prefix = $prefix;
$this->whitelist = $whitelist;
$this->patchers = $patchers;
}

/**
* {@inheritdoc}
*/
public function scope(string $filePath, string $contents): string
{
return $this->scoper->scope(
$filePath,
$contents,
$this->prefix,
$this->patchers,
$this->whitelist
);
}

/**
* {@inheritdoc}
*/
public function getWhitelist(): array
{
return $this->whitelist;
}

/**
* {@inheritdoc}
*/
public function getPrefix(): string
{
return $this->prefix;
}
}
1 change: 1 addition & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

/**
* TODO: this function should be pushed down to the PHAR extension.
*
* @private
*/
function get_phar_compression_algorithms(): array
Expand Down
Loading

0 comments on commit 0808fff

Please sign in to comment.