Skip to content

Commit

Permalink
Rework configuration helper (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Jun 19, 2022
1 parent 888e092 commit 6bd2f85
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 114 deletions.
3 changes: 0 additions & 3 deletions fixtures/Console/Command/TestConfigurableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

class TestConfigurableCommand extends ConfigurableBaseCommand
{
/**
* {@inheritdoc}
*/
protected function executeCommand(IO $io): int
{
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,25 @@
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Console;
namespace KevinGH\Box\Configuration;

use function file_exists;
use KevinGH\Box\Configuration\Configuration;
use KevinGH\Box\Configuration\NoConfigurationFound;
use KevinGH\Box\Json\Json;
use function realpath;
use stdClass;
use Symfony\Component\Console\Helper\Helper;

/**
* @private
*/
final class ConfigurationHelper extends Helper
final class ConfigurationLoader
{
private const FILE_NAME = 'box.json';
private const SCHEMA_FILE = __DIR__.'/../../res/schema.json';

private $json;

public function __construct()
{
$this->json = new Json();
}

public function getName(): string
{
return 'config';
}

public function findDefaultPath(): string
public function __construct(private readonly Json $json = new Json())
{
if (false === file_exists(self::FILE_NAME)) {
if (false === file_exists(self::FILE_NAME.'.dist')) {
throw new NoConfigurationFound();
}

return realpath(self::FILE_NAME.'.dist');
}

return realpath(self::FILE_NAME);
}

/**
* @param null|non-empty-string $file
*/
public function loadFile(?string $file): Configuration
{
if (null === $file) {
Expand Down
9 changes: 0 additions & 9 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use function KevinGH\Box\get_box_version;
use function sprintf;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Helper\HelperSet;
use function trim;

/**
Expand Down Expand Up @@ -82,12 +81,4 @@ protected function getDefaultCommands(): array

return $commands;
}

protected function getDefaultHelperSet(): HelperSet
{
$helperSet = parent::getDefaultHelperSet();
$helperSet->set(new ConfigurationHelper());

return $helperSet;
}
}
6 changes: 0 additions & 6 deletions src/Console/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace KevinGH\Box\Console\Command;

use KevinGH\Box\Console\Application;
use KevinGH\Box\Console\ConfigurationHelper;
use KevinGH\Box\Console\IO\IO;
use KevinGH\Box\Console\OutputFormatterConfigurator;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
Expand All @@ -38,9 +37,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return $this->executeCommand(new IO($input, $output));
}

final protected function getConfigurationHelper(): ConfigurationHelper
{
return $this->getHelper('config');
}
}
7 changes: 0 additions & 7 deletions src/Console/Command/ConfigurableBaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\IO\IO;
use KevinGH\Box\Json\JsonValidationException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
Expand Down Expand Up @@ -51,14 +50,8 @@ final protected function getConfig(IO $io, bool $allowNoFile = false): Configura
{
return ConfigurationLoader::getConfig(
$io->getInput()->getOption(self::CONFIG_PARAM),
$this->getConfigurationHelper(),
$io,
$allowNoFile,
);
}

final protected function getConfigPath(InputInterface $input): string
{
return $input->getOption(self::CONFIG_PARAM) ?? $this->getConfigurationHelper()->findDefaultPath();
}
}
4 changes: 2 additions & 2 deletions src/Console/Command/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Exception;
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\ConfigurationLocator;
use KevinGH\Box\Console\IO\IO;
use KevinGH\Box\Console\MessageRenderer;
use KevinGH\Box\Json\JsonValidationException;
Expand Down Expand Up @@ -70,8 +71,7 @@ protected function executeCommand(IO $io): int

try {
$config = ConfigurationLoader::getConfig(
$input->getArgument(self::FILE_ARGUMENT) ?? $this->getConfigurationHelper()->findDefaultPath(),
$this->getConfigurationHelper(),
$input->getArgument(self::FILE_ARGUMENT) ?? ConfigurationLocator::findDefaultPath(),
$io,
false,
);
Expand Down
12 changes: 6 additions & 6 deletions src/Console/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use InvalidArgumentException;
use KevinGH\Box\Configuration\Configuration;
use KevinGH\Box\Configuration\ConfigurationLoader as ConfigLoader;
use KevinGH\Box\Configuration\NoConfigurationFound;
use KevinGH\Box\Console\IO\IO;
use KevinGH\Box\Json\JsonValidationException;
Expand All @@ -36,18 +37,18 @@ final class ConfigurationLoader
*
* @param bool $allowNoFile Load the config nonetheless if not file is found when true
*
* @throws JsonValidationException
* @throws JsonValidationException|NoConfigurationFound
*/
public static function getConfig(
?string $configPath,
ConfigurationHelper $helper,
IO $io,
bool $allowNoFile,
): Configuration {
$configPath = self::getConfigPath($configPath, $helper, $io, $allowNoFile);
$configPath = self::getConfigPath($configPath, $io, $allowNoFile);
$configLoader = new ConfigLoader();

try {
return $helper->loadFile($configPath);
return $configLoader->loadFile($configPath);
} catch (InvalidArgumentException $invalidConfig) {
$io->error('The configuration file is invalid.');

Expand All @@ -57,12 +58,11 @@ public static function getConfig(

private static function getConfigPath(
?string $configPath,
ConfigurationHelper $helper,
IO $io,
bool $allowNoFile,
): ?string {
try {
$configPath ??= $helper->findDefaultPath();
$configPath ??= ConfigurationLocator::findDefaultPath();
} catch (NoConfigurationFound $noConfigurationFound) {
if (false === $allowNoFile) {
throw $noConfigurationFound;
Expand Down
53 changes: 53 additions & 0 deletions src/Console/ConfigurationLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Console;

use function file_exists;
use KevinGH\Box\Configuration\NoConfigurationFound;
use KevinGH\Box\NotInstantiable;
use function realpath;

/**
* @private
*/
final class ConfigurationLocator
{
use NotInstantiable;

private const FILE_NAME = 'box.json';

/**
* @var list<non-empty-string>
*/
private static array $candidates;

public static function findDefaultPath(): string
{
if (!isset(self::$candidates)) {
self::$candidates = [
self::FILE_NAME,
self::FILE_NAME.'.dist',
];
}

foreach (self::$candidates as $candidate) {
if (file_exists($candidate)) {
return realpath($candidate);
}
}

throw new NoConfigurationFound();
}
}
8 changes: 4 additions & 4 deletions tests/Composer/ComposerOrchestratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public static function composerAutoloadProvider(): iterable
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
}
require_once __DIR__ . '/composer/autoload_real.php';
\$loader = ${composerAutoloaderName}::getLoader();
Expand Down Expand Up @@ -568,7 +568,7 @@ function foo() {
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
}
require_once __DIR__ . '/composer/autoload_real.php';
\$loader = ${composerAutoloaderName}::getLoader();
Expand Down Expand Up @@ -624,7 +624,7 @@ function foo() {
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
}
require_once __DIR__ . '/composer/autoload_real.php';
\$loader = ${composerAutoloaderName}::getLoader();
Expand Down Expand Up @@ -684,7 +684,7 @@ function foo() {
echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
exit(1);
}
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit80c62b20a4a44fb21e8e102ccb92ff05::getLoader();
Expand Down
55 changes: 55 additions & 0 deletions tests/Configuration/ConfigurationLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\Configuration;

use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\touch;
use KevinGH\Box\Test\FileSystemTestCase;

/**
* @covers \KevinGH\Box\Configuration\ConfigurationLoader
*/
class ConfigurationLoaderTest extends FileSystemTestCase
{
private ConfigurationLoader $loader;

protected function setUp(): void
{
parent::setUp();

$this->loader = new ConfigurationLoader();
}

public function test_it_can_load_a_configuration(): void
{
touch('index.php');
dump_file('box.json.dist', '{}');

$this->assertInstanceOf(
Configuration::class,
$this->loader->loadFile('box.json.dist'),
);
}

public function test_it_can_load_a_configuration_without_a_file(): void
{
touch('index.php');

$this->assertInstanceOf(
Configuration::class,
$this->loader->loadFile(null),
);
}
}
3 changes: 1 addition & 2 deletions tests/Configuration/ConfigurationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use function json_encode;
use const JSON_PRETTY_PRINT;
use KevinGH\Box\Console\ConfigurationHelper;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\make_path_absolute;
use function KevinGH\Box\FileSystem\touch;
Expand Down Expand Up @@ -56,7 +55,7 @@ final protected function setConfig(array $config): void

final protected function reloadConfig(): void
{
$this->config = (new ConfigurationHelper())->loadFile($this->file);
$this->config = (new ConfigurationLoader())->loadFile($this->file);
}

final protected function isWindows(): bool
Expand Down
Loading

0 comments on commit 6bd2f85

Please sign in to comment.