From 820bcb41f2bf352d3ce302bdbdad858bbc950bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 14 Jun 2018 11:22:35 +0100 Subject: [PATCH] Minor refactoring (#259) --- src/Console/Command/Compile.php | 110 +++++++++--------- .../{BuildLogger.php => CompileLogger.php} | 5 +- src/Console/Logger/UpdateConsoleLogger.php | 104 ----------------- 3 files changed, 59 insertions(+), 160 deletions(-) rename src/Console/Logger/{BuildLogger.php => CompileLogger.php} (97%) delete mode 100644 src/Console/Logger/UpdateConsoleLogger.php diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 9314c6637..cc8573c88 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -22,7 +22,7 @@ use KevinGH\Box\Compactor; use KevinGH\Box\Composer\ComposerConfiguration; use KevinGH\Box\Configuration; -use KevinGH\Box\Console\Logger\BuildLogger; +use KevinGH\Box\Console\Logger\CompileLogger; use KevinGH\Box\MapFile; use KevinGH\Box\PhpSettingsHandler; use KevinGH\Box\RequirementChecker\RequirementsDumper; @@ -176,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void ; $path = $config->getOutputPath(); - $logger = new BuildLogger($io); + $logger = new CompileLogger($io); $startTime = microtime(true); @@ -195,7 +195,7 @@ private function createPhar( Configuration $config, InputInterface $input, OutputInterface $output, - BuildLogger $logger, + CompileLogger $logger, SymfonyStyle $io, bool $debug ): Box { @@ -238,7 +238,7 @@ private function createPhar( return $box; } - private function removeExistingArtifacts(Configuration $config, BuildLogger $logger, bool $debug): void + private function removeExistingArtifacts(Configuration $config, CompileLogger $logger, bool $debug): void { $path = $config->getOutputPath(); @@ -272,7 +272,7 @@ private function removeExistingArtifacts(Configuration $config, BuildLogger $log } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, sprintf( 'Removing the existing PHAR "%s"', $path @@ -282,7 +282,7 @@ private function removeExistingArtifacts(Configuration $config, BuildLogger $log remove($path); } - private function registerReplacementValues(Configuration $config, Box $box, BuildLogger $logger): void + private function registerReplacementValues(Configuration $config, Box $box, CompileLogger $logger): void { $values = $config->getReplacements(); @@ -291,13 +291,13 @@ private function registerReplacementValues(Configuration $config, Box $box, Buil } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Setting replacement values' ); foreach ($values as $key => $value) { $logger->log( - BuildLogger::PLUS_PREFIX, + CompileLogger::PLUS_PREFIX, sprintf( '%s: %s', $key, @@ -309,13 +309,13 @@ private function registerReplacementValues(Configuration $config, Box $box, Buil $box->registerPlaceholders($values); } - private function registerCompactors(Configuration $config, Box $box, BuildLogger $logger): void + private function registerCompactors(Configuration $config, Box $box, CompileLogger $logger): void { $compactors = $config->getCompactors(); if ([] === $compactors) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'No compactor to register' ); @@ -323,7 +323,7 @@ private function registerCompactors(Configuration $config, Box $box, BuildLogger } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Registering compactors' ); @@ -336,7 +336,7 @@ private function registerCompactors(Configuration $config, Box $box, BuildLogger } $logger->log( - BuildLogger::PLUS_PREFIX, + CompileLogger::PLUS_PREFIX, implode('\\', $compactorClassParts) ); }; @@ -346,7 +346,7 @@ private function registerCompactors(Configuration $config, Box $box, BuildLogger $box->registerCompactors($compactors); } - private function registerFileMapping(Configuration $config, Box $box, BuildLogger $logger): void + private function registerFileMapping(Configuration $config, Box $box, CompileLogger $logger): void { $fileMapper = $config->getFileMapper(); @@ -355,22 +355,22 @@ private function registerFileMapping(Configuration $config, Box $box, BuildLogge $box->registerFileMapping($fileMapper); } - private function addFiles(Configuration $config, Box $box, BuildLogger $logger, SymfonyStyle $io): void + private function addFiles(Configuration $config, Box $box, CompileLogger $logger, SymfonyStyle $io): void { - $logger->log(BuildLogger::QUESTION_MARK_PREFIX, 'Adding binary files'); + $logger->log(CompileLogger::QUESTION_MARK_PREFIX, 'Adding binary files'); $count = count($config->getBinaryFiles()); $box->addFiles($config->getBinaryFiles(), true); $logger->log( - BuildLogger::CHEVRON_PREFIX, + CompileLogger::CHEVRON_PREFIX, 0 === $count ? 'No file found' : sprintf('%d file(s)', $count) ); - $logger->log(BuildLogger::QUESTION_MARK_PREFIX, 'Adding files'); + $logger->log(CompileLogger::QUESTION_MARK_PREFIX, 'Adding files'); $count = count($config->getFiles()); @@ -386,18 +386,18 @@ private function addFiles(Configuration $config, Box $box, BuildLogger $logger, } $logger->log( - BuildLogger::CHEVRON_PREFIX, + CompileLogger::CHEVRON_PREFIX, 0 === $count ? 'No file found' : sprintf('%d file(s)', $count) ); } - private function registerMainScript(Configuration $config, Box $box, BuildLogger $logger): ?string + private function registerMainScript(Configuration $config, Box $box, CompileLogger $logger): ?string { if (false === $config->hasMainScript()) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'No main script path configured' ); @@ -407,7 +407,7 @@ private function registerMainScript(Configuration $config, Box $box, BuildLogger $main = $config->getMainScriptPath(); $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, sprintf( 'Adding main file: %s', $main @@ -423,7 +423,7 @@ private function registerMainScript(Configuration $config, Box $box, BuildLogger if ($localMain !== $relativeMain) { $logger->log( - BuildLogger::CHEVRON_PREFIX, + CompileLogger::CHEVRON_PREFIX, $localMain ); } @@ -431,11 +431,11 @@ private function registerMainScript(Configuration $config, Box $box, BuildLogger return $localMain; } - private function registerRequirementsChecker(Configuration $config, Box $box, BuildLogger $logger): bool + private function registerRequirementsChecker(Configuration $config, Box $box, CompileLogger $logger): bool { if (false === $config->checkRequirements()) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Skip requirements checker' ); @@ -443,7 +443,7 @@ private function registerRequirementsChecker(Configuration $config, Box $box, Bu } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Adding requirements checker' ); @@ -462,11 +462,11 @@ private function registerRequirementsChecker(Configuration $config, Box $box, Bu return true; } - private function registerStub(Configuration $config, Box $box, ?string $main, bool $checkRequirements, BuildLogger $logger): void + private function registerStub(Configuration $config, Box $box, ?string $main, bool $checkRequirements, CompileLogger $logger): void { if ($config->isStubGenerated()) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Generating new stub' ); @@ -478,7 +478,7 @@ private function registerStub(Configuration $config, Box $box, ?string $main, bo } if (null !== ($stub = $config->getStubPath())) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, sprintf( 'Using stub file: %s', $stub @@ -504,21 +504,21 @@ private function registerStub(Configuration $config, Box $box, ?string $main, bo $box->getPhar()->setDefaultStub($main); $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Using default stub' ); } - private function configureMetadata(Configuration $config, Box $box, BuildLogger $logger): void + private function configureMetadata(Configuration $config, Box $box, CompileLogger $logger): void { if (null !== ($metadata = $config->getMetadata())) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Setting metadata' ); $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, is_string($metadata) ? $metadata : var_export($metadata, true) ); @@ -526,26 +526,26 @@ private function configureMetadata(Configuration $config, Box $box, BuildLogger } } - private function commit(Box $box, Configuration $config, BuildLogger $logger): void + private function commit(Box $box, Configuration $config, CompileLogger $logger): void { $message = $config->dumpAutoload() ? 'Dumping the Composer autoloader' : 'Skipping dumping the Composer autoloader' ; - $logger->log(BuildLogger::QUESTION_MARK_PREFIX, $message); + $logger->log(CompileLogger::QUESTION_MARK_PREFIX, $message); $box->endBuffering($config->dumpAutoload()); } - private function checkComposerFiles(Box $box, Configuration $config, BuildLogger $logger): void + private function checkComposerFiles(Box $box, Configuration $config, CompileLogger $logger): void { $message = $config->excludeComposerFiles() ? 'Removing the Composer dump artefacts' : 'Keep the Composer dump artefacts' ; - $logger->log(BuildLogger::QUESTION_MARK_PREFIX, $message); + $logger->log(CompileLogger::QUESTION_MARK_PREFIX, $message); if ($config->excludeComposerFiles()) { $box->removeComposerArtefacts( @@ -556,11 +556,11 @@ private function checkComposerFiles(Box $box, Configuration $config, BuildLogger } } - private function configureCompressionAlgorithm(Configuration $config, Box $box, bool $dev, SymfonyStyle $io, BuildLogger $logger): void + private function configureCompressionAlgorithm(Configuration $config, Box $box, bool $dev, SymfonyStyle $io, CompileLogger $logger): void { if (null === ($algorithm = $config->getCompressionAlgorithm())) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, $dev ? 'No compression' : 'No compression' @@ -570,7 +570,7 @@ private function configureCompressionAlgorithm(Configuration $config, Box $box, } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, sprintf( 'Compressing with the algorithm "%s"', array_search($algorithm, get_phar_compression_algorithms(), true) @@ -584,7 +584,7 @@ private function configureCompressionAlgorithm(Configuration $config, Box $box, if (null !== $extension) { $logger->log( - BuildLogger::CHEVRON_PREFIX, + CompileLogger::CHEVRON_PREFIX, sprintf( 'Warning: the extension "%s" will now be required to execute the PHAR', $extension @@ -662,7 +662,7 @@ private function signPhar( string $path, InputInterface $input, OutputInterface $output, - BuildLogger $logger + CompileLogger $logger ): void { // sign using private key, if applicable //TODO: check that out @@ -679,7 +679,7 @@ private function signPhar( } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Signing using a private key' ); @@ -711,11 +711,11 @@ private function signPhar( $box->signUsingFile($key, $passphrase); } - private function correctPermissions(string $path, Configuration $config, BuildLogger $logger): void + private function correctPermissions(string $path, Configuration $config, CompileLogger $logger): void { if (null !== ($chmod = $config->getFileMode())) { $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, sprintf( 'Setting file permissions to %s', '0'.decoct($chmod) @@ -726,7 +726,7 @@ private function correctPermissions(string $path, Configuration $config, BuildLo } } - private function createStub(Configuration $config, ?string $main, bool $checkRequirements, BuildLogger $logger): string + private function createStub(Configuration $config, ?string $main, bool $checkRequirements, CompileLogger $logger): string { $stub = StubGenerator::create() ->alias($config->getAlias()) @@ -737,7 +737,7 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq if (null !== ($shebang = $config->getShebang())) { $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, sprintf( 'Using shebang line: %s', $shebang @@ -747,14 +747,14 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq $stub->shebang($shebang); } else { $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, 'No shebang line' ); } if (null !== ($bannerPath = $config->getStubBannerPath())) { $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, sprintf( 'Using custom banner from file: %s', $bannerPath @@ -764,7 +764,7 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq $stub->banner($config->getStubBannerContents()); } elseif (null !== ($banner = $config->getStubBannerContents())) { $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, 'Using banner:' ); @@ -772,7 +772,7 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq foreach ($bannerLines as $bannerLine) { $logger->log( - BuildLogger::CHEVRON_PREFIX, + CompileLogger::CHEVRON_PREFIX, $bannerLine ); } @@ -783,7 +783,7 @@ private function createStub(Configuration $config, ?string $main, bool $checkReq return $stub->generate(); } - private function logMap(MapFile $fileMapper, BuildLogger $logger): void + private function logMap(MapFile $fileMapper, CompileLogger $logger): void { $map = $fileMapper->getMap(); @@ -792,7 +792,7 @@ private function logMap(MapFile $fileMapper, BuildLogger $logger): void } $logger->log( - BuildLogger::QUESTION_MARK_PREFIX, + CompileLogger::QUESTION_MARK_PREFIX, 'Mapping paths' ); @@ -804,7 +804,7 @@ private function logMap(MapFile $fileMapper, BuildLogger $logger): void } $logger->log( - BuildLogger::MINUS_PREFIX, + CompileLogger::MINUS_PREFIX, sprintf( '%s > %s', $match, @@ -815,10 +815,10 @@ private function logMap(MapFile $fileMapper, BuildLogger $logger): void } } - private function logEndBuilding(BuildLogger $logger, SymfonyStyle $io, Box $box, string $path, float $startTime): void + private function logEndBuilding(CompileLogger $logger, SymfonyStyle $io, Box $box, string $path, float $startTime): void { $logger->log( - BuildLogger::STAR_PREFIX, + CompileLogger::STAR_PREFIX, 'Done.' ); diff --git a/src/Console/Logger/BuildLogger.php b/src/Console/Logger/CompileLogger.php similarity index 97% rename from src/Console/Logger/BuildLogger.php rename to src/Console/Logger/CompileLogger.php index 68919030f..9b8e3d868 100644 --- a/src/Console/Logger/BuildLogger.php +++ b/src/Console/Logger/CompileLogger.php @@ -18,7 +18,10 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -final class BuildLogger +/** + * @internal + */ +final class CompileLogger { public const QUESTION_MARK_PREFIX = '?'; public const STAR_PREFIX = '*'; diff --git a/src/Console/Logger/UpdateConsoleLogger.php b/src/Console/Logger/UpdateConsoleLogger.php deleted file mode 100644 index aab8a7163..000000000 --- a/src/Console/Logger/UpdateConsoleLogger.php +++ /dev/null @@ -1,104 +0,0 @@ - - * Théo Fidry - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace KevinGH\Box\Console\Logger; - -use Symfony\Component\Console\Style\SymfonyStyle; -use Throwable; - -/** - * @private - * @final - */ -class UpdateConsoleLogger -{ - /** - * @var SymfonyStyle - */ - private $io; - - public function __construct(SymfonyStyle $io) - { - $this->io = $io; - } - - public function startUpdating(): void - { - $this->io->writeln('Updating...'); - } - - public function updateSuccess(string $newVersion, string $oldVersion): void - { - $this->io->success('PHP-Scoper has been updated.'); - $this->io->writeln(sprintf( - 'Current version is: %s.', - $newVersion - )); - $this->io->writeln(sprintf( - 'Previous version was: %s.', - $oldVersion - )); - } - - public function updateNotNeeded(string $oldVersion): void - { - $this->io->writeln('PHP-Scoper is currently up to date.'); - $this->io->writeln(sprintf( - 'Current version is: %s.', - $oldVersion - )); - } - - public function error(Throwable $e): void - { - $this->io->error('Unexpected error. If updating, your original phar is untouched.'); - } - - public function rollbackSuccess(): void - { - $this->io->success('PHP-Scoper has been rolled back to prior version.'); - } - - public function rollbackFail(): void - { - $this->io->error('Rollback failed for reasons unknown.'); - } - - public function printLocalVersion(string $version): void - { - $this->io->writeln(sprintf( - 'Your current local version is: %s', - $version - )); - } - - public function printRemoteVersion(string $stability, string $version): void - { - $this->io->writeln(sprintf( - 'The current %s build available remotely is: %s', - $stability, - $version - )); - } - - public function noNewRemoteVersions(string $stability): void - { - $this->io->writeln(sprintf('There are no new %s builds available.', $stability)); - } - - public function currentVersionInstalled(string $stability): void - { - $this->io->writeln(sprintf('You have the current %s build installed.', $stability)); - } -}