Skip to content

Commit

Permalink
Add a dump autoload configuration setting (#201)
Browse files Browse the repository at this point in the history
Closes #188
  • Loading branch information
theofidry authored May 4, 2018
1 parent 1b2a2e2 commit acee112
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ great things:
1. [Shebang (`shebang`)](doc/configuration.md#shebang-shebang)
1. [Banner (`banner`)](doc/configuration.md#banner-banner)
1. [Banner file (`banner-file`)](doc/configuration.md#banner-file-banner-file)
1. [Dumping the Composer autoloader (`dump-autoload`)](doc/configuration.md#dumping-the-composer-autoloader-dump-autoload)
1. [Compactors (`compactors`)][compactors]
1. [Compression algorithm (`compression`)][compression-algorithm]
1. [Signing algorithm (`algorithm`)](doc/configuration.md#signing-algorithm-algorithm)
Expand Down
17 changes: 16 additions & 1 deletion doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1. [Shebang (`shebang`)][shebang]
1. [Banner (`banner`)][banner]
1. [Banner file (`banner-file`)][banner-file]
1. [Dumping the Composer autoloader (`dump-autoload`)][dump-autoload]
1. [Compactors (`compactors`)][compactors]
1. [Compression algorithm (`compression`)][compression]
1. [Signing algorithm (`algorithm`)][algorithm]
Expand Down Expand Up @@ -449,6 +450,18 @@ Like banner, the comment must not already be enclosed in a comment block.
If this parameter is set, then the value of [`banner`][banner] will be discarded.


## Dumping the Composer autoloader (`dump-autoload`)

By default, Box will dump the Composer autoload with the [classmap authoritative mode][composer-classmap-authoritative]
and the [`--no-dev` option][composer-no-dev-option] which disables the `autoload-dev` rules. This is however done only
if a `composer.json` file could be found.

The dumping of the autoloader will be _ignored_ if the `composer.json` file could be found.

The autoloader is dumped at the end of the process to ensure it will take into account the eventual modifications done
by the [compactors][compactors] process.


## Compactors (`compactors`)

The compactors (`string[]`) setting is a list of file contents compacting classes that must be registered. A file
Expand Down Expand Up @@ -548,14 +561,16 @@ The metadata (`any`) setting can be any value. This value will be stored as meta
[phar.getmetadata]: htthttps://secure.php.net/manual/en/phar.getmetadata.php
[symfony-finder]: https://symfony.com/doc/current//components/finder.html
[phpscoper]: https://github.com/humbug/php-scoper
[dump-autoload]: #dumping-the-composer-autoloader-dump-autoload
[compactors]: #compactors-compactors
[permissions]: #permissions-chmod
[compression]: #compression-algorithm-compression
[algorithm]: #signing-algorithm-algorithm
[metadata]: #metadata-metadata
[check-requirements]: #check-requirements-check-requirements
[composer-bin]: https://getcomposer.org/doc/04-schema.md#bin

[composer-classmap-authoritative]: https://getcomposer.org/doc/articles/autoloader-optimization.md#optimization-level-2-a-authoritative-class-maps
[composer-no-dev-option]: https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-


//TODO: rework the rest
Expand Down
4 changes: 4 additions & 0 deletions res/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
},
"type": ["array"]
},
"dump-autoload": {
"description": "Will dump the optimized Composer autoloader.",
"type": ["boolean", "null"]
},
"files": {
"description": "A list of file paths to include.",
"items": {
Expand Down
25 changes: 21 additions & 4 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ final class Configuration
private $composerLock;
private $files;
private $binaryFiles;
private $dumpAutoload;
private $compactors;
private $compressionAlgorithm;
private $mainScriptPath;
Expand Down Expand Up @@ -125,10 +126,6 @@ final class Configuration
* @param string $mainScriptContents The processed content of the main script file
* @param MapFile $fileMapper Utility to map the files from outside and inside the PHAR
* @param mixed $metadata The PHAR Metadata
* @param string $tmpOutputPath
* @param string $outputPath
* @param null|string $privateKeyPassphrase
* @param null|string $privateKeyPath
* @param bool $isPrivateKeyPrompt If the user should be prompted for the private key passphrase
* @param array $processedReplacements The processed list of replacement placeholders and their values
* @param null|string $shebang The shebang line
Expand All @@ -148,6 +145,7 @@ private function __construct(
array $composerLock,
array $files,
array $binaryFiles,
bool $dumpAutoload,
array $compactors,
?int $compressionAlgorithm,
?int $fileMode,
Expand Down Expand Up @@ -186,6 +184,7 @@ private function __construct(
$this->composerLock = $composerLock;
$this->files = $files;
$this->binaryFiles = $binaryFiles;
$this->dumpAutoload = $dumpAutoload;
$this->compactors = $compactors;
$this->compressionAlgorithm = $compressionAlgorithm;
$this->fileMode = $fileMode;
Expand Down Expand Up @@ -267,6 +266,8 @@ public static function create(?string $file, stdClass $raw): self

$binaryFilesAggregate = self::retrieveFilesAggregate($binaryFiles, $binaryDirectories, ...$binaryFilesFromFinders);

$dumpAutoload = self::retrieveDumpAutoload($raw, null !== $composerJson[0]);

$compactors = self::retrieveCompactors($raw, $basePath);
$compressionAlgorithm = self::retrieveCompressionAlgorithm($raw);

Expand Down Expand Up @@ -316,6 +317,7 @@ public static function create(?string $file, stdClass $raw): self
$composerLock,
$filesAggregate,
$binaryFilesAggregate,
$dumpAutoload,
$compactors,
$compressionAlgorithm,
$fileMode,
Expand Down Expand Up @@ -391,6 +393,11 @@ public function getBinaryFiles(): array
return $this->binaryFiles;
}

public function dumpAutoload(): bool
{
return $this->dumpAutoload;
}

/**
* @return Compactor[] the list of compactors
*/
Expand Down Expand Up @@ -1207,6 +1214,16 @@ private static function normalizePath(string $file, string $basePath): string
return make_path_absolute(trim($file), $basePath);
}

private static function retrieveDumpAutoload(stdClass $raw, bool $composerJson): bool
{
$dumpAutoload = $raw->{'dump-autoload'} ?? true;

// TODO: add warning when the dump autoload parameter is explicitly set that it has been ignored because no `composer.json` file
// could have been found.

return $composerJson ? $dumpAutoload : false;
}

/**
* @return Compactor[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private function createPhar(
$this->registerStub($config, $box, $main, $check, $logger);
$this->configureMetadata($config, $box, $logger);

$box->endBuffering(null !== $config->getComposerJson());
$box->endBuffering($config->dumpAutoload());

$this->configureCompressionAlgorithm($config, $box, $input->getOption(self::DEV_OPTION), $io, $logger);

Expand Down
49 changes: 49 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,55 @@ public function test_it_throws_an_error_when_a_composer_lock_is_found_but_invali
}
}

public function test_the_autoloader_is_dumped_by_default_if_a_composer_json_file_is_found()
{
$this->assertFalse($this->config->dumpAutoload());
$this->assertFalse($this->getNoFileConfig()->dumpAutoload());

$this->setConfig(['dump-autoload' => null]);

$this->assertFalse($this->config->dumpAutoload());

file_put_contents('composer.json', '{}');

$this->setConfig([]);

$this->assertTrue($this->config->dumpAutoload());
$this->assertTrue($this->getNoFileConfig()->dumpAutoload());

$this->setConfig(['dump-autoload' => null]);

$this->assertTrue($this->config->dumpAutoload());
}

public function test_the_autoloader_is_can_be_configured()
{
file_put_contents('composer.json', '{}');

$this->setConfig([
'dump-autoload' => false,
]);

$this->assertFalse($this->config->dumpAutoload());
$this->assertTrue($this->getNoFileConfig()->dumpAutoload());

$this->setConfig([
'dump-autoload' => true,
]);

$this->assertTrue($this->config->dumpAutoload());
$this->assertTrue($this->getNoFileConfig()->dumpAutoload());
}

public function test_the_autoloader_cannot_be_dumped_if_no_composer_json_file_is_found()
{
$this->setConfig([
'dump-autoload' => true,
]);

$this->assertFalse($this->config->dumpAutoload());
}

public function test_no_compactors_is_configured_by_default(): void
{
$this->assertSame([], $this->config->getCompactors());
Expand Down

0 comments on commit acee112

Please sign in to comment.