Skip to content

Commit

Permalink
Merge branch 'main' into refactor/php-compactor-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 22, 2023
2 parents 7934383 + 718f2a1 commit c3acaf6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
26 changes: 3 additions & 23 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use DateTimeZone;
use Fidry\FileSystem\FS;
use Humbug\PhpScoper\Configuration\Configuration as PhpScoperConfiguration;
use Humbug\PhpScoper\Container;
use InvalidArgumentException;
use KevinGH\Box\Compactor\Compactor;
use KevinGH\Box\Compactor\Compactors;
Expand All @@ -32,6 +31,7 @@
use KevinGH\Box\MapFile;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SigningAlgorithm;
use KevinGH\Box\PhpScoper\ConfigurationFactory as PhpScoperConfigurationFactory;
use KevinGH\Box\PhpScoper\SerializableScoper;
use Phar;
use RuntimeException;
Expand All @@ -42,7 +42,6 @@
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Throwable;
use Webmozart\Assert\Assert;
use function array_diff;
use function array_filter;
Expand Down Expand Up @@ -2543,7 +2542,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath,
$configFilePath = Path::makeAbsolute(self::PHP_SCOPER_CONFIG, $basePath);
$configFilePath = file_exists($configFilePath) ? $configFilePath : null;

return self::createPhpScoperConfig($configFilePath);
return PhpScoperConfigurationFactory::create($configFilePath);
}

$configFile = $raw->{self::PHP_SCOPER_KEY};
Expand All @@ -2555,26 +2554,7 @@ private static function retrievePhpScoperConfig(stdClass $raw, string $basePath,
Assert::file($configFilePath);
Assert::readable($configFilePath);

return self::createPhpScoperConfig($configFilePath);
}

private static function createPhpScoperConfig(?string $filePath): PhpScoperConfiguration
{
$configFactory = (new Container())->getConfigurationFactory();

try {
return $configFactory->create($filePath);
} catch (Throwable $throwable) {
throw new InvalidArgumentException(
sprintf(
'Could not create a PHP-Scoper config from the file "%s": %s',
$filePath,
$throwable->getMessage(),
),
$throwable->getCode(),
$throwable,
);
}
return PhpScoperConfigurationFactory::create($configFilePath);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/PhpScoper/ConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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\Configuration\Configuration as PhpScoperConfiguration;
use Humbug\PhpScoper\Container;
use InvalidArgumentException;
use KevinGH\Box\NotInstantiable;
use Throwable;
use function sprintf;

final class ConfigurationFactory
{
use NotInstantiable;

public static function create(?string $filePath): PhpScoperConfiguration
{
$configFactory = (new Container())->getConfigurationFactory();

try {
return $configFactory->create($filePath);
} catch (Throwable $throwable) {
throw new InvalidArgumentException(
sprintf(
'Could not create a PHP-Scoper config from the file "%s": %s',
$filePath,
$throwable->getMessage(),
),
$throwable->getCode(),
$throwable,
);
}
}
}
7 changes: 5 additions & 2 deletions tests/Compactor/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public function test_it_supports_php_files(string $file, bool $supports): void
/**
* @dataProvider phpContentProvider
*/
public function test_it_compacts_php_files(DocblockAnnotationParser $annotationParser, string $content, string $expected): void
{
public function test_it_compacts_php_files(
DocblockAnnotationParser $annotationParser,
string $content,
string $expected
): void {
$file = 'foo.php';

$actual = (new Php($annotationParser))->compact($file, $content);
Expand Down

0 comments on commit c3acaf6

Please sign in to comment.