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

simplify configuration loading #451

Merged
merged 1 commit into from
Jan 14, 2021
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
16 changes: 0 additions & 16 deletions src/Configuration/Exception/BaselineFileCannotBeReadException.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Configuration/Exception/FileCannotBeReadException.php

This file was deleted.

6 changes: 2 additions & 4 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace SensioLabs\Deptrac\Configuration;

use SensioLabs\Deptrac\Configuration\Exception\BaselineFileCannotBeReadException;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeParsedAsYamlException;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;
use SensioLabs\Deptrac\Configuration\Exception\ParsedYamlIsNotAnArrayException;
use SensioLabs\Deptrac\Configuration\Loader\YmlFileLoader;
use SensioLabs\Deptrac\File\CouldNotReadFileException;

class Loader
{
Expand All @@ -21,10 +20,9 @@ public function __construct(YmlFileLoader $fileLoader)
}

/**
* @throws FileDoesNotExistsException
* @throws CouldNotReadFileException
* @throws FileCannotBeParsedAsYamlException
* @throws ParsedYamlIsNotAnArrayException
* @throws BaselineFileCannotBeReadException
*/
public function load(string $file): Configuration
{
Expand Down
52 changes: 27 additions & 25 deletions src/Configuration/Loader/YmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,18 @@

namespace SensioLabs\Deptrac\Configuration\Loader;

use SensioLabs\Deptrac\Configuration\Exception\BaselineFileCannotBeReadException;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeParsedAsYamlException;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;
use SensioLabs\Deptrac\Configuration\Exception\ParsedYamlIsNotAnArrayException;
use SensioLabs\Deptrac\File\FileReader;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class YmlFileLoader
{
public function parseFile(string $file): array
{
if (!is_file($file)) {
throw FileDoesNotExistsException::fromFilename($file);
}

if (false === ($content = file_get_contents($file))) {
throw FileCannotBeReadException::fromFilename($file);
}

try {
$data = Yaml::parse($content);
if (isset($data['baseline'])) {
$pathPrefix = dirname($file);
$importFile = $pathPrefix.DIRECTORY_SEPARATOR.$data['baseline'];
if (false === file_exists($importFile) || false === ($importContent = file_get_contents($importFile))) {
throw BaselineFileCannotBeReadException::fromFilename($importFile);
}
$data = array_merge_recursive(
$data,
Yaml::parse($importContent)
);

unset($data['baseline']);
}
$data = Yaml::parse(FileReader::read($file));
} catch (ParseException $exception) {
throw FileCannotBeParsedAsYamlException::fromFilename($file);
}
Expand All @@ -45,6 +22,31 @@ public function parseFile(string $file): array
throw ParsedYamlIsNotAnArrayException::fromFilename($file);
}

if (isset($data['baseline'])) {
$data = $this->importBaseline($file, $data);
}

return $data;
}

private function importBaseline(string $file, array $data): array
{
$pathPrefix = dirname($file);
$importFile = $pathPrefix.DIRECTORY_SEPARATOR.$data['baseline'];

try {
$baselineData = Yaml::parse(FileReader::read($importFile));
} catch (ParseException $e) {
throw FileCannotBeParsedAsYamlException::fromFilename($importFile);
}

if (!is_array($baselineData)) {
throw ParsedYamlIsNotAnArrayException::fromFilename($importFile);
}

$data = array_merge_recursive($data, $baselineData);
unset($data['baseline']);

return $data;
}
}
35 changes: 0 additions & 35 deletions tests/Configuration/Exception/FileDoesNotExistsExceptionTest.php

This file was deleted.

7 changes: 3 additions & 4 deletions tests/Configuration/Loader/YmlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
namespace Tests\SensioLabs\Deptrac\Configuration\Loader;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Configuration\Exception\BaselineFileCannotBeReadException;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeParsedAsYamlException;
use SensioLabs\Deptrac\Configuration\Exception\FileDoesNotExistsException;
use SensioLabs\Deptrac\Configuration\Exception\ParsedYamlIsNotAnArrayException;
use SensioLabs\Deptrac\Configuration\Loader\YmlFileLoader;
use SensioLabs\Deptrac\File\CouldNotReadFileException;

/**
* @covers \SensioLabs\Deptrac\Configuration\Loader
Expand All @@ -22,7 +21,7 @@ public function testLoadThrowsFileDoesNotExistsExceptionWhenFileDoesNotExist():

$loader = new YmlFileLoader();

$this->expectException(FileDoesNotExistsException::class);
$this->expectException(CouldNotReadFileException::class);

$loader->parseFile($file);
}
Expand Down Expand Up @@ -55,7 +54,7 @@ public function testLoadThrowsImportFileCannotBeReadException(): void

$loader = new YmlFileLoader();

$this->expectException(BaselineFileCannotBeReadException::class);
$this->expectException(CouldNotReadFileException::class);
$this->expectExceptionMessageMatches('~non-existent-file\.yaml~');

$loader->parseFile($file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\Configuration\Exception;
namespace Tests\SensioLabs\Deptrac\File;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException;
use SensioLabs\Deptrac\File\CouldNotReadFileException;

/**
* @covers \SensioLabs\Deptrac\Configuration\Exception\FileCannotBeReadException
* @covers \SensioLabs\Deptrac\File\CouldNotReadFileException
*/
final class FileCannotBeReadExceptionTest extends TestCase
final class CouldNotReadFileExceptionTest extends TestCase
{
public function testIsRuntimeException(): void
{
$exception = new FileCannotBeReadException();
$exception = new CouldNotReadFileException();

self::assertInstanceOf(\RuntimeException::class, $exception);
}
Expand All @@ -23,7 +23,7 @@ public function testFromFilenameReturnsException(): void
{
$filename = __FILE__;

$exception = FileCannotBeReadException::fromFilename($filename);
$exception = CouldNotReadFileException::fromFilename($filename);

$message = sprintf(
'File "%s" cannot be read.',
Expand Down