-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change configuration loaders interface
- Loading branch information
Showing
42 changed files
with
537 additions
and
583 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
35 changes: 35 additions & 0 deletions
35
lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFile.php
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,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; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationFileWithFallback.php
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,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(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/Doctrine/Migrations/Configuration/Configuration/ConfigurationLoader.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Migrations\Configuration\Configuration; | ||
|
||
use Doctrine\Migrations\Configuration\Configuration; | ||
|
||
interface ConfigurationLoader | ||
{ | ||
public function getConfiguration() : Configuration; | ||
} |
16 changes: 16 additions & 0 deletions
16
lib/Doctrine/Migrations/Configuration/Configuration/Exception/InvalidConfigurationFormat.php
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,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.'); | ||
} | ||
} |
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 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
16 changes: 16 additions & 0 deletions
16
lib/Doctrine/Migrations/Configuration/Configuration/Exception/MissingConfigurationFile.php
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,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.'); | ||
} | ||
} |
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 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 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
23 changes: 23 additions & 0 deletions
23
lib/Doctrine/Migrations/Configuration/Configuration/ExistingConfiguration.php
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,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; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
lib/Doctrine/Migrations/Configuration/Configuration/FormattedFile.php
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,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
44
lib/Doctrine/Migrations/Configuration/Configuration/JsonFile.php
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,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(); | ||
} | ||
} |
Oops, something went wrong.