-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ca896d
commit 8ae290b
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Release\Bundler; | ||
|
||
use ParagonIE\Gossamer\GossamerException; | ||
use Phar; | ||
|
||
class PharBundler extends AbstractBundler | ||
{ | ||
/** @var string $defaultStubFilename */ | ||
protected $defaultStubFilename = 'default-stub.php'; | ||
|
||
/** | ||
* @param string $filename | ||
* @return self | ||
*/ | ||
public function setDefaultStubFilename($filename) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
namespace ParagonIE\Gossamer\Tests\Release\Bundler; | ||
|
||
use Exception; | ||
use ParagonIE\Gossamer\Release\Bundler\PharBundler; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers PharBundler | ||
*/ | ||
class PharBundlerTest extends TestCase | ||
{ | ||
/** @var string $dir */ | ||
protected $dir = ''; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function before() | ||
{ | ||
$dir = __DIR__; | ||
do { | ||
$prev = $dir; | ||
$dir = realpath(dirname($dir)); | ||
} while (!empty($dir) && $dir !== $prev && !is_dir($dir . '/.git')); | ||
$this->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'); | ||
} | ||
} |