Skip to content

Commit

Permalink
refactor: Make the ComposerOrchestrator into a non-static class
Browse files Browse the repository at this point in the history
The goal is to increase the re-usability of this class.
  • Loading branch information
theofidry committed Oct 16, 2023
1 parent 842901b commit 40098a8
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 438 deletions.
64 changes: 64 additions & 0 deletions src/Composer/AutoloadDumper.php
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,
);
}
}
71 changes: 71 additions & 0 deletions src/Composer/CompilerPsrLogger.php
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;
}
}
Loading

0 comments on commit 40098a8

Please sign in to comment.