diff --git a/src/Box.php b/src/Box.php index 710466bf1..5849c078d 100644 --- a/src/Box.php +++ b/src/Box.php @@ -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; @@ -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); } @@ -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. * @@ -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); } /** @@ -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'; diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 7a0f9e9e8..9cc350a01 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -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; @@ -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( @@ -594,7 +593,7 @@ private static function registerStub( $stub = self::createStub($config, $main, $checkRequirements, $logger); - $box->getPhar()->setStub($stub); + $box->setStub($stub); return; } @@ -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, @@ -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); } } @@ -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; } diff --git a/tests/BoxTest.php b/tests/BoxTest.php index 979ba8f46..17fd294b4 100644 --- a/tests/BoxTest.php +++ b/tests/BoxTest.php @@ -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.'); @@ -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) { @@ -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) {