Skip to content

Commit

Permalink
change configuration loaders interface
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas authored and greg0ire committed Jan 14, 2020
1 parent ab7ebf3 commit 7425e17
Show file tree
Hide file tree
Showing 42 changed files with 537 additions and 583 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Loader;
namespace Doctrine\Migrations\Configuration\Configuration;

use Closure;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey;
use Doctrine\Migrations\Configuration\Exception\UnableToLoadResource;
use Doctrine\Migrations\Configuration\Configuration\Exception\InvalidConfigurationKey;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Tools\BooleanStringFormatter;
use function assert;
Expand All @@ -16,20 +15,21 @@
use function is_bool;
use function is_callable;

/**
* @internal
*/
final class ArrayLoader implements Loader
final class ConfigurationArray implements ConfigurationLoader
{
/** @var array<string,mixed> */
private $configurations;

/**
* @param mixed $array
* @param array<string,mixed> $configurations
*/
public function load($array) : Configuration
public function __construct(array $configurations)
{
if (! is_array($array)) {
throw UnableToLoadResource::with(static::class);
}
$this->configurations = $configurations;
}

public function getConfiguration() : Configuration
{
$configMap = [
'migrations_paths' => static function ($paths, Configuration $configuration) : void {
foreach ($paths as $namespace => $path) {
Expand Down Expand Up @@ -63,7 +63,7 @@ public function load($array) : Configuration
];

$object = new Configuration();
self::applyConfigs($configMap, $object, $array);
self::applyConfigs($configMap, $object, $this->configurations);

if ($object->getMetadataStorageConfiguration() === null) {
$object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use function dirname;
use function realpath;

abstract class ConfigurationFile implements ConfigurationLoader
{
/** @var string */
protected $file;

public function __construct(string $file)
{
$this->file = $file;
}

/**
* @param array<string,string> $directories
*
* @return array<string,string>
*/
final protected function getDirectoriesRelativeToFile(array $directories, string $file) : array
{
foreach ($directories as $ns => $dir) {
$path = realpath(dirname($file) . '/' . $dir);

$directories[$ns] = $path !== false ? $path : $dir;
}

return $directories;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Configuration\Exception\MissingConfigurationFile;
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported;
use function file_exists;

/**
* The ConfigurationLoader class is responsible for getting the Configuration instance from one of the supported methods
* for defining the configuration for your migrations.
*
* @internal
*/
final class ConfigurationFileWithFallback implements ConfigurationLoader
{
/** @var string|null */
private $file;

public function __construct(?string $file = null)
{
$this->file = $file;
}

public function getConfiguration() : Configuration
{
if ($this->file !== null) {
return $this->loadConfiguration($this->file);
}

/**
* If no config has been provided, look for default config file in the path.
*/
$defaultFiles = [
'migrations.xml',
'migrations.yml',
'migrations.yaml',
'migrations.json',
'migrations.php',
];

foreach ($defaultFiles as $file) {
if ($this->configurationFileExists($file)) {
return $this->loadConfiguration($file);
}
}

throw MissingConfigurationFile::new();
}

private function configurationFileExists(string $config) : bool
{
return file_exists($config);
}

/**
* @throws FileTypeNotSupported
*/
private function loadConfiguration(string $file) : Configuration
{
return (new FormattedFile($file))->getConfiguration();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use Doctrine\Migrations\Configuration\Configuration;

interface ConfigurationLoader
{
public function getConfiguration() : Configuration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class InvalidConfigurationFormat extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('The provided configuration file can not be parsed.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;
namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;
use function sprintf;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;
namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class JsonNotValid extends LogicException implements ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class MissingConfigurationFile extends LogicException implements ConfigurationException
{
public static function new() : self
{
return new self('It was not possible to locate any configuration file.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;
namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class XmlNotValid extends LogicException implements ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;
namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class YamlNotAvailable extends LogicException implements ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Exception;
namespace Doctrine\Migrations\Configuration\Configuration\Exception;

use Doctrine\Migrations\Configuration\Exception\ConfigurationException;
use LogicException;

final class YamlNotValid extends LogicException implements ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use Doctrine\Migrations\Configuration\Configuration;

final class ExistingConfiguration implements ConfigurationLoader
{
/** @var Configuration */
private $configurations;

public function __construct(Configuration $configurations)
{
$this->configurations = $configurations;
}

public function getConfiguration() : Configuration
{
return $this->configurations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use Doctrine\Migrations\Configuration\Configuration;
use const PATHINFO_EXTENSION;
use function count;
use function pathinfo;

/**
* @internal
*/
final class FormattedFile extends ConfigurationFile
{
/** @var callable[] */
private $loaders = [];

private function setDefaultLoaders() : void
{
$this->loaders = [
'json' => static function ($file) : ConfigurationLoader {
return new JsonFile($file);
},
'php' => static function ($file) : ConfigurationLoader {
return new PhpFile($file);
},
'xml' => static function ($file) : ConfigurationLoader {
return new XmlFile($file);
},
'yaml' => static function ($file) : ConfigurationLoader {
return new YamlFile($file);
},
'yml' => static function ($file) : ConfigurationLoader {
return new YamlFile($file);
},
];
}

public function getConfiguration() : Configuration
{
if (count($this->loaders) === 0) {
$this->setDefaultLoaders();
}

$extension = pathinfo($this->file, PATHINFO_EXTENSION);
if (! isset($this->loaders[$extension])) {
throw Configuration\Exception\InvalidConfigurationFormat::new();
}

return $this->loaders[$extension]($this->file)->getConfiguration();
}
}
44 changes: 44 additions & 0 deletions lib/Doctrine/Migrations/Configuration/Configuration/JsonFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Configuration\Exception\JsonNotValid;
use Doctrine\Migrations\Configuration\Exception\FileNotFound;
use const JSON_ERROR_NONE;
use function assert;
use function file_exists;
use function file_get_contents;
use function json_decode;
use function json_last_error;

final class JsonFile extends ConfigurationFile
{
public function getConfiguration() : Configuration
{
if (! file_exists($this->file)) {
throw FileNotFound::new();
}

$contents = file_get_contents($this->file);

assert($contents !== false);

$config = json_decode($contents, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw JsonNotValid::new();
}

if (isset($config['migrations_paths'])) {
$config['migrations_paths'] = $this->getDirectoriesRelativeToFile(
$config['migrations_paths'],
$this->file
);
}

return (new ConfigurationArray($config))->getConfiguration();
}
}
Loading

0 comments on commit 7425e17

Please sign in to comment.