Skip to content

Commit

Permalink
Fixed dumping the PHAR contents and restoring the file limit (#306)
Browse files Browse the repository at this point in the history
- The file descriptor limit was being restored although it had not been changed
- The dumped content of the PHAR for debugging purposes was the content directly extracted from the PHAR. However, if the PHAR was being compressed, it was not uncompressing the dumped content causing the dumped files to appear as binaries. The PHAR content is now dumped before the PHAR is being compressed
  • Loading branch information
theofidry authored Oct 16, 2018
1 parent 06a6bf2 commit f5403bd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
*/
final class Box implements Countable
{
public const DEBUG_DIR = '.box_dump';

/**
* @var string The path to the PHAR file
*/
Expand Down
40 changes: 21 additions & 19 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ private function createPhar(

$this->checkComposerFiles($box, $config, $logger);

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

if ($debug) {
$box->getPhar()->extractTo(self::DEBUG_DIR, null, true);
}

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

$this->signPhar($config, $box, $config->getTmpOutputPath(), $input, $output, $logger);

if ($config->getTmpOutputPath() !== $config->getOutputPath()) {
Expand Down Expand Up @@ -649,25 +649,27 @@ private static function bumpOpenFileDescriptorLimit(Box $box, SymfonyStyle $io):
$softLimit = posix_getrlimit()['soft openfiles'];
$hardLimit = posix_getrlimit()['hard openfiles'];

if ($softLimit < $filesCount) {
$io->writeln(
sprintf(
'<info>[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")'
.'</info>',
$softLimit,
$hardLimit,
$filesCount,
'unlimited'
),
OutputInterface::VERBOSITY_DEBUG
);
if ($softLimit >= $filesCount) {
return function (): void {};
}

posix_setrlimit(
POSIX_RLIMIT_NOFILE,
$io->writeln(
sprintf(
'<info>[debug] Increased the maximum number of open file descriptors from ("%s", "%s") to ("%s", "%s")'
.'</info>',
$softLimit,
$hardLimit,
$filesCount,
'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit
);
}
'unlimited'
),
OutputInterface::VERBOSITY_DEBUG
);

posix_setrlimit(
POSIX_RLIMIT_NOFILE,
$filesCount,
'unlimited' === $hardLimit ? POSIX_RLIMIT_INFINITY : $hardLimit
);

return function () use ($io, $softLimit, $hardLimit): void {
if (function_exists('posix_setrlimit') && isset($softLimit, $hardLimit)) {
Expand Down
33 changes: 31 additions & 2 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function extension_loaded;
use function file_get_contents;
use function file_put_contents;
use function Humbug\get_contents;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\file_contents;
Expand Down Expand Up @@ -974,6 +975,9 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void
$this->assertSame($expected, $actual);
}

/**
* @requires extension zlib
*/
public function test_it_can_build_a_PHAR_file_in_debug_mode(): void
{
mirror(self::FIXTURES_DIR.'/dir000', $this->tmp);
Expand Down Expand Up @@ -1005,6 +1009,7 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void
'metadata' => ['rand' => $rand = random_int(0, mt_getrandmax())],
'output' => 'test.phar',
'shebang' => $shebang,
'compression' => 'GZ',
]
)
);
Expand Down Expand Up @@ -1077,7 +1082,8 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void
)
? Dumping the Composer autoloader
? Removing the Composer dump artefacts
? No compression
? Compressing with the algorithm "GZ"
> Warning: the extension "zlib" will now be required to execute the PHAR
? Signing using a private key
Private key passphrase:
? Setting file permissions to 0754
Expand Down Expand Up @@ -1360,7 +1366,7 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void
]
}
]
-compressionAlgorithm: null
-compressionAlgorithm: 4096
-mainScriptPath: "/path/to/run.php"
-mainScriptContents: """
<?php\\n
Expand Down Expand Up @@ -1462,6 +1468,29 @@ public function test_it_can_build_a_PHAR_file_in_debug_mode(): void
);

$this->assertSame($expectedDumpedConfig, $actualDumpedConfig);

// Checks one of the dumped file from the PHAR to ensure the encoding of the extracted file is correct
$this->assertSame(
get_contents('.box_dump/run.php'),
<<<'PHP'
<?php
declare(strict_types=1);
require 'test.php';

PHP
);
}

public function test_it_can_build_a_PHAR_file_in_quiet_mode(): void
Expand Down

0 comments on commit f5403bd

Please sign in to comment.