diff --git a/src/Box.php b/src/Box.php index 2771c21a7..15d240659 100644 --- a/src/Box.php +++ b/src/Box.php @@ -15,6 +15,7 @@ namespace KevinGH\Box; use Assert\Assertion; +use function file_exists; use KevinGH\Box\Compactor\PhpScoper; use KevinGH\Box\Composer\ComposerOrchestrator; use KevinGH\Box\PhpScoper\NullScoper; @@ -240,7 +241,10 @@ public function addFile(string $file, string $contents = null, bool $binary = fa } if ($binary) { - $this->phar->addFile($file, $local); + true === file_exists($file) + ? $this->phar->addFile($file, $local) + : $this->phar->addFromString($local, $contents) + ; } else { $processedContents = self::compactContents( $this->compactors, diff --git a/tests/BoxTest.php b/tests/BoxTest.php index 31fd5de0c..a6da0f305 100644 --- a/tests/BoxTest.php +++ b/tests/BoxTest.php @@ -126,6 +126,23 @@ public function test_it_can_add_a_non_exitent_file_with_contents_to_the_phar(): $this->assertSame($expectedContents, $actualContents); } + public function test_it_can_add_a_non_existent_bin_file_with_contents_to_the_phar(): void + { + $file = 'foo'; + $contents = 'test'; + + $this->box->addFile($file, $contents, true); + + $expectedContents = $contents; + $expectedPharPath = 'phar://test.phar/'.$file; + + $this->assertFileExists($expectedPharPath); + + $actualContents = file_get_contents($expectedPharPath); + + $this->assertSame($expectedContents, $actualContents); + } + public function test_it_can_add_a_file_with_absolute_path_to_the_phar(): void { $relativePath = 'path-to/foo';