diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33fc105..3c058fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,4 +65,4 @@ jobs: composer-options: --ignore-platform-reqs - name: PHPUnit tests - run: vendor/bin/phpunit + run: php -d phar.readonly=0 vendor/bin/phpunit diff --git a/lib/Release/Bundler/PharBundler.php b/lib/Release/Bundler/PharBundler.php new file mode 100644 index 0000000..08b30a2 --- /dev/null +++ b/lib/Release/Bundler/PharBundler.php @@ -0,0 +1,52 @@ +assert(is_string($filename), "Argument 1 must be a string"); + $this->defaultStubFilename = $filename; + return $this; + } + + /** + * @return void + * @throws GossamerException + */ + public function throwIfPharReadonly() + { + $readOnly = (bool) ini_get('phar.readonly'); + if ($readOnly) { + throw new GossamerException( + "Cannot work with Phars without setting readonly to false" + ); + } + } + + public function bundle($outputFile) + { + $this->throwIfPharReadonly(); + $workingDir = \getcwd(); + \chdir($this->directory); + + $phar = new Phar($outputFile); + $phar->startBuffering(); + $defaultStub = $phar->createDefaultStub($this->defaultStubFilename); + $phar->buildFromDirectory($this->directory); + $phar->setStub('#!/usr/bin/env php' . "\n" . $defaultStub); + $phar->stopBuffering(); + + \chdir($workingDir); + } +} diff --git a/tests/Release/Bundler/PharBundlerTest.php b/tests/Release/Bundler/PharBundlerTest.php new file mode 100644 index 0000000..dceb0f3 --- /dev/null +++ b/tests/Release/Bundler/PharBundlerTest.php @@ -0,0 +1,44 @@ +dir = $dir; + } + + /** + * @throws Exception + */ + public function testPhar() + { + if (ini_get('phar.readonly')) { + $this->markTestSkipped("phar.readonly is set to true"); + } + + $bundler = (new PharBundler()) + ->setWorkDirectory($this->dir . '/lib'); + $bundler->bundle(dirname($this->dir) . '/test.phar'); + $this->assertFileExists(dirname($this->dir) . '/test.phar'); + unlink(dirname($this->dir) . '/test.phar'); + } +}