-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Make the ComposerOrchestrator into a non-static class
The goal is to increase the re-usability of this class.
- Loading branch information
Showing
10 changed files
with
555 additions
and
438 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Composer; | ||
|
||
use Humbug\PhpScoper\Autoload\ScoperAutoloadGenerator; | ||
use Humbug\PhpScoper\Symbol\SymbolsRegistry; | ||
use KevinGH\Box\NotInstantiable; | ||
use function preg_replace; | ||
use function str_replace; | ||
use const PHP_EOL; | ||
|
||
final class AutoloadDumper | ||
{ | ||
use NotInstantiable; | ||
|
||
public static function generateAutoloadStatements( | ||
SymbolsRegistry $symbolsRegistry, | ||
string $autoloadContents, | ||
): string { | ||
if (0 === $symbolsRegistry->count()) { | ||
return $autoloadContents; | ||
} | ||
|
||
$autoloadContents = str_replace('<?php', '', $autoloadContents); | ||
|
||
$autoloadContents = preg_replace( | ||
'/return (ComposerAutoloaderInit.+::getLoader\(\));/', | ||
'\$loader = $1;', | ||
$autoloadContents, | ||
); | ||
|
||
$scoperStatements = (new ScoperAutoloadGenerator($symbolsRegistry))->dump(); | ||
|
||
$scoperStatements = preg_replace( | ||
'/scoper\-autoload\.php \@generated by PhpScoper/', | ||
'@generated by Humbug Box', | ||
$scoperStatements, | ||
); | ||
|
||
$scoperStatements = preg_replace( | ||
'/(\s*\\$loader \= .*)/', | ||
$autoloadContents, | ||
$scoperStatements, | ||
); | ||
|
||
return preg_replace( | ||
'/\n{2,}/m', | ||
PHP_EOL.PHP_EOL, | ||
$scoperStatements, | ||
); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the box project. | ||
* | ||
* (c) Kevin Herrera <[email protected]> | ||
* Théo Fidry <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace KevinGH\Box\Composer; | ||
|
||
use KevinGH\Box\Console\Logger\CompilerLogger; | ||
use Psr\Log\AbstractLogger; | ||
use Psr\Log\LogLevel; | ||
use Stringable; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function array_key_exists; | ||
|
||
final class CompilerPsrLogger extends AbstractLogger | ||
{ | ||
public function __construct( | ||
private CompilerLogger $decoratedLogger, | ||
) { | ||
} | ||
|
||
public function log($level, Stringable|string $message, array $context = []): void | ||
{ | ||
$verbosity = self::getVerbosity($level); | ||
$output = self::getOutput($context); | ||
|
||
if (null === $output) { | ||
$this->decoratedLogger->log( | ||
CompilerLogger::CHEVRON_PREFIX, | ||
$message, | ||
$verbosity, | ||
); | ||
} else { | ||
$this->decoratedLogger->getIO()->writeln( | ||
$output, | ||
$verbosity, | ||
); | ||
} | ||
} | ||
|
||
private static function getVerbosity(string $level): int | ||
{ | ||
return match ($level) { | ||
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, | ||
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERY_VERBOSE, | ||
default => OutputInterface::OUTPUT_NORMAL, | ||
}; | ||
} | ||
|
||
private static function getOutput(array $context): ?string | ||
{ | ||
$knownKeys = ['stdout', 'stderr']; | ||
|
||
foreach ($knownKeys as $knownKey) { | ||
if (array_key_exists($knownKey, $context)) { | ||
return $context[$knownKey]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
Oops, something went wrong.