-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make configuration parsing more flexible
This moves responsibilities around and introduces abstractions to support different file formats with less effort. Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
- Loading branch information
Showing
7 changed files
with
124 additions
and
132 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
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\Command; | ||
|
||
use Psl\Str; | ||
use Psl\Type; | ||
use Roave\BackwardCompatibility\Configuration\Configuration; | ||
use Roave\BackwardCompatibility\Configuration\ParseConfigurationFile; | ||
use Roave\BackwardCompatibility\Configuration\ParseJsonConfigurationFile; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class DetermineConfigurationFromFilesystem | ||
{ | ||
public function __construct( | ||
private readonly ParseConfigurationFile $parser = new ParseJsonConfigurationFile(), | ||
) { | ||
} | ||
|
||
public function __invoke( | ||
string $currentDirectory, | ||
OutputInterface $stdErr, | ||
): Configuration { | ||
$configuration = $this->parser->parse($currentDirectory); | ||
|
||
if ($configuration->filename !== null) { | ||
$stdErr->writeln(Str\format( | ||
'Using "%s" as configuration file', | ||
Type\string()->coerce($configuration->filename), | ||
)); | ||
} | ||
|
||
return $configuration; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\Configuration; | ||
|
||
use Roave\BackwardCompatibility\Baseline; | ||
|
||
/** @psalm-immutable */ | ||
final class Configuration | ||
{ | ||
private function __construct( | ||
public readonly Baseline $baseline, | ||
public readonly string|null $filename, | ||
) { | ||
} | ||
|
||
public static function default(): self | ||
{ | ||
return new self(Baseline::empty(), null); | ||
} | ||
|
||
public static function fromFile(Baseline $baseline, string $filename): self | ||
{ | ||
return new self($baseline, $filename); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\Configuration; | ||
|
||
interface ParseConfigurationFile | ||
{ | ||
public function parse(string $currentDirectory): Configuration; | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Roave\BackwardCompatibility\Configuration; | ||
|
||
use Psl\File; | ||
use Psl\Json; | ||
use Psl\Type; | ||
use Roave\BackwardCompatibility\Baseline; | ||
use RuntimeException; | ||
|
||
final class ParseJsonConfigurationFile implements ParseConfigurationFile | ||
{ | ||
private const CONFIGURATION_FILENAME = '.roave-backward-compatibility-check.json'; | ||
|
||
public function parse(string $currentDirectory): Configuration | ||
{ | ||
$filename = $currentDirectory . '/' . self::CONFIGURATION_FILENAME; | ||
|
||
try { | ||
$jsonContents = File\read($filename); | ||
} catch (File\Exception\InvalidArgumentException) { | ||
return Configuration::default(); | ||
} | ||
|
||
try { | ||
$configuration = Json\typed( | ||
$jsonContents, | ||
Type\shape( | ||
['baseline' => Type\optional(Type\vec(Type\string()))], | ||
), | ||
); | ||
} catch (Json\Exception\DecodeException $exception) { | ||
throw new RuntimeException( | ||
'It was not possible to parse the configuration', | ||
previous: $exception, | ||
); | ||
} | ||
|
||
$baseline = $configuration['baseline'] ?? []; | ||
|
||
return Configuration::fromFile( | ||
Baseline::fromList(...$baseline), | ||
$filename, | ||
); | ||
} | ||
} |
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