Skip to content

Commit

Permalink
Fix PHAR minimal requirements
Browse files Browse the repository at this point in the history
When a PHAR is compressed, it also requires the PHP extension `zip`.

Closes box-project#164
  • Loading branch information
theofidry committed Apr 28, 2018
1 parent e697134 commit fd35eb1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private function registerRequirementsChecker(Configuration $config, Box $box, Bu
'Adding requirements checker'
);

$checkFiles = RequirementsDumper::dump($config->getComposerLockDecodedContents());
$checkFiles = RequirementsDumper::dump($config->getComposerLockDecodedContents(), null !== $config->getCompressionAlgorithm());

foreach ($checkFiles as $fileWithContents) {
[$file, $contents] = $fileWithContents;
Expand Down
15 changes: 10 additions & 5 deletions src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ final class AppRequirementsFactory
*
* @return array Serialized configured requirements
*/
public static function create(array $composerLockDecodedContents): array
public static function create(array $composerLockDecodedContents, bool $compressed): array
{
return self::configureExtensionRequirements(
self::configurePhpVersionRequirements([], $composerLockDecodedContents),
$composerLockDecodedContents
$composerLockDecodedContents,
$compressed
);
}

Expand Down Expand Up @@ -88,9 +89,9 @@ private static function configurePhpVersionRequirements(array $requirements, arr
return $requirements;
}

private static function configureExtensionRequirements(array $requirements, array $composerLockContents): array
private static function configureExtensionRequirements(array $requirements, array $composerLockContents, bool $compressed): array
{
$extensionRequirements = self::collectExtensionRequirements($composerLockContents);
$extensionRequirements = self::collectExtensionRequirements($composerLockContents, $compressed);

foreach ($extensionRequirements as $extension => $packages) {
foreach ($packages as $package) {
Expand Down Expand Up @@ -135,11 +136,15 @@ private static function configureExtensionRequirements(array $requirements, arra
*
* @return array Associative array containing the list of extensions required
*/
private static function collectExtensionRequirements(array $composerLockContents): array
private static function collectExtensionRequirements(array $composerLockContents, bool $compressed): array
{
$requirements = [];
$polyfills = [];

if ($compressed) {
$requirements['zip'] = [self::SELF_PACKAGE];
}

$platform = $composerLockContents['platform'] ?? [];

foreach ($platform as $package => $constraint) {
Expand Down
8 changes: 4 additions & 4 deletions src/RequirementChecker/RequirementsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ final class RequirementsDumper
/**
* @return string[][]
*/
public static function dump(array $composerLockDecodedContents): array
public static function dump(array $composerLockDecodedContents, bool $compressed): array
{
$filesWithContents = [
self::dumpRequirementsConfig($composerLockDecodedContents),
self::dumpRequirementsConfig($composerLockDecodedContents, $compressed),
[self::CHECK_FILE_NAME, self::REQUIREMENTS_CHECKER_TEMPLATE],
];

Expand All @@ -80,9 +80,9 @@ public static function dump(array $composerLockDecodedContents): array
return $filesWithContents;
}

private static function dumpRequirementsConfig(array $composerLockDecodedContents): array
private static function dumpRequirementsConfig(array $composerLockDecodedContents, bool $compressed): array
{
$config = AppRequirementsFactory::create($composerLockDecodedContents);
$config = AppRequirementsFactory::create($composerLockDecodedContents, $compressed);

return [
'.requirements.php',
Expand Down
91 changes: 88 additions & 3 deletions tests/RequirementChecker/AppRequirementsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
/**
* @coversNothing
*/
class AppRequirementsFactoryTest extends TestCase
class pAppRequirementsFactoryTest extends TestCase
{
/**
* @dataProvider provideLockContents
*/
public function test_it_can_generate_and_serialized_requirements_from_a_composer_lock_file(string $composerLockContents, array $expected): void
public function test_it_can_generate_and_serialized_requirements_from_a_composer_lock_file(string $composerLockContents, bool $compressed, array $expected): void
{
$actual = AppRequirementsFactory::create(json_decode($composerLockContents, true));
$actual = AppRequirementsFactory::create(json_decode($composerLockContents, true), $compressed);

$this->assertSame($expected, $actual);
}
Expand All @@ -36,9 +36,22 @@ public function provideLockContents()
{
yield 'empty lock file' => [
'{}',
false,
[],
];

yield 'empty lock file (compressed PHAR)' => [
'{}',
true,
[
[
"return \\extension_loaded('zip');",
'The application requires the extension "zip". Enable it or install a polyfill.',
'The application requires the extension "zip".',
],
],
];

yield 'lock file platform requirements' => [
<<<'JSON'
{
Expand All @@ -50,6 +63,7 @@ public function provideLockContents()
}
JSON
,
false,
[
[
<<<'PHP'
Expand Down Expand Up @@ -78,6 +92,51 @@ public function provideLockContents()
],
];

yield 'lock file platform requirements (compressed PHAR)' => [
<<<'JSON'
{
"platform": {
"php": "^7.1",
"ext-phar": "*"
},
"platform-dev": []
}
JSON
,
true,
[
[
<<<'PHP'
require_once __DIR__.'/../vendor/composer/semver/src/Semver.php';
require_once __DIR__.'/../vendor/composer/semver/src/VersionParser.php';
require_once __DIR__.'/../vendor/composer/semver/src/Constraint/ConstraintInterface.php';
require_once __DIR__.'/../vendor/composer/semver/src/Constraint/EmptyConstraint.php';
require_once __DIR__.'/../vendor/composer/semver/src/Constraint/MultiConstraint.php';
require_once __DIR__.'/../vendor/composer/semver/src/Constraint/Constraint.php';
return \Composer\Semver\Semver::satisfies(
sprintf('%d.%d.%d', \PHP_MAJOR_VERSION, \PHP_MINOR_VERSION, \PHP_RELEASE_VERSION),
'^7.1'
);

PHP
,
'The application requires the version "^7.1" or greater.',
'The application requires the version "^7.1" or greater.',
],
[
"return \\extension_loaded('zip');",
'The application requires the extension "zip". Enable it or install a polyfill.',
'The application requires the extension "zip".',
],
[
"return \\extension_loaded('phar');",
'The application requires the extension "phar". Enable it or install a polyfill.',
'The application requires the extension "phar".',
],
],
];

yield 'lock file platform dev requirements are ignored' => [
<<<'JSON'
{
Expand All @@ -89,9 +148,31 @@ public function provideLockContents()
}
JSON
,
false,
[],
];

yield 'lock file platform dev requirements are ignored (compressed PHAR)' => [
<<<'JSON'
{
"platform": [],
"platform-dev": {
"php": "^7.3",
"ext-json": "*"
}
}
JSON
,
true,
[
[
"return \\extension_loaded('zip');",
'The application requires the extension "zip". Enable it or install a polyfill.',
'The application requires the extension "zip".',
],
],
];

yield 'lock file packages requirements' => [
<<<'JSON'
{
Expand Down Expand Up @@ -130,6 +211,7 @@ public function provideLockContents()
}
JSON
,
false,
[
[
<<<'PHP'
Expand Down Expand Up @@ -222,6 +304,7 @@ public function provideLockContents()
}
JSON
,
false,
[],
];

Expand Down Expand Up @@ -262,6 +345,7 @@ public function provideLockContents()
}
JSON
,
false,
[
[
<<<'PHP'
Expand Down Expand Up @@ -368,6 +452,7 @@ public function provideLockContents()
}
JSON
,
false,
[
[
<<<'PHP'
Expand Down

0 comments on commit fd35eb1

Please sign in to comment.