Skip to content

Commit

Permalink
refactor: Make Box::getPhar() internal
Browse files Browse the repository at this point in the history
The `Phar` instance might be unset within `Box` hence it should not be
leaked to any production code.
  • Loading branch information
theofidry committed Nov 5, 2023
1 parent b4a141c commit 8ec3a5e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
48 changes: 45 additions & 3 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use KevinGH\Box\Compactor\PhpScoper;
use KevinGH\Box\Compactor\Placeholder;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\SigningAlgorithm;
use KevinGH\Box\PhpScoper\NullScoper;
use KevinGH\Box\PhpScoper\Scoper;
use Phar;
Expand Down Expand Up @@ -198,7 +199,7 @@ public function compress(CompressionAlgorithm $compressionAlgorithm): ?string

try {
if (CompressionAlgorithm::NONE === $compressionAlgorithm) {
$this->getPhar()->decompressFiles();
$this->phar->decompressFiles();
} else {
$this->phar->compressFiles($compressionAlgorithm->value);
}
Expand Down Expand Up @@ -325,11 +326,52 @@ public function addFile(string $file, ?string $contents = null, bool $binary = f
return $local;
}

/**
* @internal
*/
public function getPhar(): Phar
{
return $this->phar;
}

public function setAlias(string $alias): void
{
$aliasWasAdded = $this->phar->setAlias($alias);

Assert::true(
$aliasWasAdded,
sprintf(
'The alias "%s" is invalid. See Phar::setAlias() documentation for more information.',
$alias,
),
);
}

public function setStub(string $stub): void
{
$this->phar->setStub($stub);
}

public function setDefaultStub(string $main): void
{
$this->phar->setDefaultStub($main);
}

public function setMetadata(mixed $metadata): void
{
$this->phar->setMetadata($metadata);
}

public function extractTo(string $directory, bool $overwrite = false): void
{
$this->phar->extractTo($directory, overwrite: $overwrite);
}

public function sign(SigningAlgorithm $signingAlgorithm): void
{
$this->phar->setSignatureAlgorithm($signingAlgorithm->value);
}

/**
* Signs the PHAR using a private key file.
*
Expand All @@ -338,7 +380,7 @@ public function getPhar(): Phar
*/
public function signUsingFile(string $file, ?string $password = null): void
{
$this->sign(FS::getFileContents($file), $password);
$this->signUsingKey(FS::getFileContents($file), $password);
}

/**
Expand All @@ -347,7 +389,7 @@ public function signUsingFile(string $file, ?string $password = null): void
* @param string $key The private key
* @param null|string $password The private key password
*/
public function sign(string $key, ?string $password): void
public function signUsingKey(string $key, ?string $password): void
{
$pubKey = $this->pharFilePath.'.pubkey';

Expand Down
24 changes: 6 additions & 18 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Path;
use Webmozart\Assert\Assert;
use function array_map;
use function array_shift;
use function count;
Expand Down Expand Up @@ -278,7 +277,7 @@ private function createPhar(
self::checkComposerFiles($box, $config, $logger);

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

self::configureCompressionAlgorithm(
Expand Down Expand Up @@ -594,7 +593,7 @@ private static function registerStub(

$stub = self::createStub($config, $main, $checkRequirements, $logger);

$box->getPhar()->setStub($stub);
$box->setStub($stub);

return;
}
Expand All @@ -613,17 +612,8 @@ private static function registerStub(
return;
}

$aliasWasAdded = $box->getPhar()->setAlias($config->getAlias());

Assert::true(
$aliasWasAdded,
sprintf(
'The alias "%s" is invalid. See Phar::setAlias() documentation for more information.',
$config->getAlias(),
),
);

$box->getPhar()->setDefaultStub($main);
$box->setAlias($config->getAlias());
$box->setDefaultStub($main);

$logger->log(
CompilerLogger::QUESTION_MARK_PREFIX,
Expand All @@ -648,7 +638,7 @@ private static function configureMetadata(Configuration $config, Box $box, Compi
is_string($metadata) ? $metadata : var_export($metadata, true),
);

$box->getPhar()->setMetadata($metadata);
$box->setMetadata($metadata);
}
}

Expand Down Expand Up @@ -763,9 +753,7 @@ private static function signPhar(
$key = $config->getPrivateKeyPath();

if (null === $key) {
$box->getPhar()->setSignatureAlgorithm(
$config->getSigningAlgorithm()->value,
);
$box->sign($config->getSigningAlgorithm());

return;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/BoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ public function test_it_can_sign_the_phar(): void

$this->configureHelloWorldPhar();

$this->box->sign($key, $password);
$this->box->signUsingKey($key, $password);

self::assertNotSame([], $phar->getSignature(), 'Expected the PHAR to be signed.');
self::assertIsString($phar->getSignature()['hash'], 'Expected the PHAR signature hash to be a string.');
Expand All @@ -1462,7 +1462,7 @@ public function test_it_cannot_sign_if_cannot_get_the_private_key(): void
$this->configureHelloWorldPhar();

try {
$this->box->sign($key, $password);
$this->box->signUsingKey($key, $password);

self::fail('Expected exception to be thrown.');
} catch (InvalidArgumentException $exception) {
Expand All @@ -1482,7 +1482,7 @@ public function test_it_cannot_sign_if_cannot_create_the_public_key(): void
$this->configureHelloWorldPhar();

try {
$this->box->sign($key, $password);
$this->box->signUsingKey($key, $password);

self::fail('Expected exception to be thrown.');
} catch (InvalidArgumentException $exception) {
Expand Down

0 comments on commit 8ec3a5e

Please sign in to comment.