diff --git a/src/Box.php b/src/Box.php index 559ae2091..8585f8b74 100644 --- a/src/Box.php +++ b/src/Box.php @@ -201,7 +201,7 @@ public function compress(CompressionAlgorithm $compressionAlgorithm): ?string try { if (CompressionAlgorithm::NONE === $compressionAlgorithm) { - $this->getPhar()->decompressFiles(); + $this->phar->decompressFiles(); } else { $this->phar->compressFiles($compressionAlgorithm->value); } @@ -328,11 +328,47 @@ 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 signWithTimestamps( DateTimeImmutable $timestamp, SigningAlgorithm $signingAlgorithm @@ -360,7 +396,7 @@ public function signWithTimestamps( */ public function signUsingFile(string $file, ?string $password = null): void { - $this->signWithPrivateKey(FS::getFileContents($file), $password); + $this->signUsingKey(FS::getFileContents($file), $password); } /** @@ -369,7 +405,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 signWithPrivateKey(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 386aceee7..72e2b0a1e 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -52,7 +52,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; @@ -283,7 +282,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( @@ -602,7 +601,7 @@ private static function registerStub( $stub = self::createStub($config, $main, $checkRequirements, $logger); - $box->getPhar()->setStub($stub); + $box->setStub($stub); return; } @@ -621,17 +620,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, @@ -656,7 +646,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); } } @@ -785,9 +775,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 89460a252..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->signWithPrivateKey($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->signWithPrivateKey($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->signWithPrivateKey($key, $password); + $this->box->signUsingKey($key, $password); self::fail('Expected exception to be thrown.'); } catch (InvalidArgumentException $exception) {