Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make RequirmentAppFactory non-static #1274

Merged
merged 4 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use KevinGH\Box\Console\Command\Process;
use KevinGH\Box\Console\Command\Validate;
use KevinGH\Box\Console\Command\Verify;
use KevinGH\Box\RequirementChecker\AppRequirementsFactory;
use KevinGH\Box\RequirementChecker\RequirementsDumper;
use function KevinGH\Box\get_box_version;
use function sprintf;
use function trim;
Expand Down Expand Up @@ -93,7 +95,12 @@ public function getCommands(): array
return [
new ComposerCheckVersion(),
new ComposerVendorDir(),
new Compile($this->getHeader()),
new Compile(
$this->getHeader(),
new RequirementsDumper(
new AppRequirementsFactory(),
),
),
new Diff(),
new Info(),
new Info('info:general'),
Expand Down
23 changes: 17 additions & 6 deletions src/Console/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ final class Compile implements CommandAware

private const DEBUG_DIR = '.box_dump';

public function __construct(private readonly string $header)
{
public function __construct(
private readonly string $header,
private readonly RequirementsDumper $requirementsDumper,
) {
}

public function getConfiguration(): CommandConfiguration
Expand Down Expand Up @@ -268,7 +270,12 @@ private function createPhar(
// file used for debugging purposes and the Composer dump autoloading will not work correctly otherwise.
$main = self::registerMainScript($config, $box, $logger);

$check = self::registerRequirementsChecker($config, $box, $logger);
$check = self::registerRequirementsChecker(
$config,
$box,
$this->requirementsDumper,
$logger,
);

self::addFiles($config, $box, $logger, $io);

Expand Down Expand Up @@ -548,8 +555,12 @@ private static function registerMainScript(Configuration $config, Box $box, Comp
return $localMain;
}

private static function registerRequirementsChecker(Configuration $config, Box $box, CompilerLogger $logger): bool
{
private static function registerRequirementsChecker(
Configuration $config,
Box $box,
RequirementsDumper $requirementsDumper,
CompilerLogger $logger,
): bool {
if (false === $config->checkRequirements()) {
$logger->log(
CompilerLogger::QUESTION_MARK_PREFIX,
Expand All @@ -564,7 +575,7 @@ private static function registerRequirementsChecker(Configuration $config, Box $
'Adding requirements checker',
);

$checkFiles = RequirementsDumper::dump(
$checkFiles = $requirementsDumper->dump(
$config->getComposerJson(),
$config->getComposerLock(),
$config->getCompressionAlgorithm(),
Expand Down
2 changes: 1 addition & 1 deletion src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class AppRequirementsFactory
{
private const SELF_PACKAGE = null;

public static function create(
public function create(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
Expand Down
47 changes: 47 additions & 0 deletions src/RequirementChecker/RequirementCheckerFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\RequirementChecker;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\Assert\Assert;

final class RequirementCheckerFinder
{
private const REQUIREMENT_CHECKER_PATH = __DIR__.'/../../res/requirement-checker';

/**
* @return iterable<array{string, string}>
*/
public function find(): iterable
{
Assert::directory(
self::REQUIREMENT_CHECKER_PATH,
'Expected the requirement checker to have been dumped',
);

/** @var SplFileInfo[] $requirementCheckerFiles */
$requirementCheckerFiles = Finder::create()
->files()
->in(self::REQUIREMENT_CHECKER_PATH);

foreach ($requirementCheckerFiles as $file) {
yield [
$file->getRelativePathname(),
$file->getContents(),
];
}
}
}
23 changes: 15 additions & 8 deletions src/RequirementChecker/RequirementsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace KevinGH\Box\RequirementChecker;

use Fidry\FileSystem\FS;
use KevinGH\Box\Composer\Artifact\ComposerJson;
use KevinGH\Box\Composer\Artifact\ComposerLock;
use KevinGH\Box\Phar\CompressionAlgorithm;
Expand All @@ -27,7 +26,7 @@
/**
* @private
*/
final class RequirementsDumper
final readonly class RequirementsDumper
{
private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP'
<?php
Expand All @@ -37,18 +36,26 @@ final class RequirementsDumper

private const REQUIREMENT_CHECKER_PATH = __DIR__.'/../../res/requirement-checker';

public function __construct(
private AppRequirementsFactory $requirementsFactory,
) {
}

/**
* @return list<array{string, string}>
*/
public static function dump(
public function dump(
?ComposerJson $composerJson,
?ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): array {
Assert::directory(self::REQUIREMENT_CHECKER_PATH, 'Expected the requirement checker to have been dumped');
Assert::directory(
self::REQUIREMENT_CHECKER_PATH,
'Expected the requirement checker to have been dumped',
);

$filesWithContents = [
self::dumpRequirementsConfig(
$this->dumpRequirementsConfig(
$composerJson ?? new ComposerJson('', []),
$composerLock ?? new ComposerLock('', []),
$compressionAlgorithm,
Expand All @@ -63,19 +70,19 @@ public static function dump(
foreach ($requirementCheckerFiles as $file) {
$filesWithContents[] = [
$file->getRelativePathname(),
FS::getFileContents($file->getPathname()),
$file->getContents(),
];
}

return $filesWithContents;
}

private static function dumpRequirementsConfig(
private function dumpRequirementsConfig(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): array {
$requirements = AppRequirementsFactory::create(
$requirements = $this->requirementsFactory->create(
$composerJson,
$composerLock,
$compressionAlgorithm,
Expand Down
4 changes: 3 additions & 1 deletion tests/Benchmark/AppRequirementFactoryBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
{
private const FIXTURES = __DIR__.'/../../fixtures/bench/requirement-checker';

private AppRequirementsFactory $factory;
private ComposerJson $composerJson;
private ComposerLock $composerLock;

public function setUp(): void
{
self::assertVendorsAreInstalled();

$this->factory = new AppRequirementsFactory();
$this->composerJson = new ComposerJson(
'',
json_decode(
Expand All @@ -55,7 +57,7 @@ public function setUp(): void
#[BeforeMethods('setUp')]
public function bench(): void
{
AppRequirementsFactory::create(
$this->factory->create(
$this->composerJson,
$this->composerLock,
CompressionAlgorithm::BZ2,
Expand Down
5 changes: 5 additions & 0 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use KevinGH\Box\Console\Application;
use KevinGH\Box\Console\DisplayNormalizer as BoxDisplayNormalizer;
use KevinGH\Box\Console\MessageRenderer;
use KevinGH\Box\RequirementChecker\AppRequirementsFactory;
use KevinGH\Box\RequirementChecker\RequirementsDumper;
use KevinGH\Box\Test\FileSystemTestCase;
use KevinGH\Box\Test\RequiresPharReadonlyOff;
use KevinGH\Box\VarDumperNormalizer;
Expand Down Expand Up @@ -181,6 +183,9 @@ protected function getCommand(): Command
{
return new Compile(
(new Application())->getHeader(),
new RequirementsDumper(
new AppRequirementsFactory(),
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/RequirementChecker/AppRequirementsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function test_it_can_generate_and_serialized_requirements_from_a_composer
: json_decode($composerLockContents, true, flags: JSON_THROW_ON_ERROR),
);

$actual = AppRequirementsFactory::create(
$actual = (new AppRequirementsFactory())->create(
$composerJson,
$composerLock,
$compressionAlgorithm,
Expand Down
11 changes: 10 additions & 1 deletion tests/RequirementChecker/RequirementsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@
#[CoversClass(RequirementsDumper::class)]
class RequirementsDumperTest extends TestCase
{
private RequirementsDumper $dumper;

protected function setUp(): void
{
$this->dumper = new RequirementsDumper(
new AppRequirementsFactory(),
);
}

#[DataProvider('jsonAndLockContentsProvider')]
public function test_it_dumps_the_requirement_checker_files(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
string $expectedRequirement,
): void {
$checkFiles = RequirementsDumper::dump(
$checkFiles = $this->dumper->dump(
$composerJson,
$composerLock,
$compressionAlgorithm,
Expand Down
Loading