Skip to content
Closed
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
23 changes: 2 additions & 21 deletions bin/doctrine-migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Doctrine\Migrations\Tools\Console\ConsoleRunner;
use Phar;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
use const PHP_EOL;
Expand Down Expand Up @@ -62,31 +60,14 @@
break;
}

$helperSet = null;
if ($configurationFile !== null) {
if (! is_readable($configurationFile)) {
trigger_error('Configuration file [' . $configurationFile . '] does not have read permission.', E_USER_ERROR);
exit(1);
}

$helperSet = require $configurationFile;

if (! $helperSet instanceof HelperSet) {
foreach ($GLOBALS as $helperSetCandidate) {
if (! $helperSetCandidate instanceof HelperSet) {
continue;
}

$helperSet = $helperSetCandidate;
break;
}
}
require $configurationFile;
}

$helperSet = $helperSet ?? new HelperSet();
$helperSet->set(new QuestionHelper(), 'question');

$commands = [];

ConsoleRunner::run($helperSet, $commands);
ConsoleRunner::run();
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\Configuration\Exception\UnknownLoader;
use Doctrine\Migrations\Configuration\Loader\ArrayLoader;
use Doctrine\Migrations\Configuration\Loader\JsonFileLoader;
use Doctrine\Migrations\Configuration\Loader\Loader;
use Doctrine\Migrations\Configuration\Loader\PhpFileLoader;
use Doctrine\Migrations\Configuration\Loader\XmlFileLoader;
use Doctrine\Migrations\Configuration\Loader\YamlFileLoader;
use function count;

/**
* @internal
*/
class ConfigurationFormatLoader
{
/** @var Loader[] */
private $loaders = [];

public function addLoader(string $type, Loader $loader) : void
{
$this->loaders[$type] = $loader;
}

private function setDefaultLoaders() : void
{
$this->loaders = [
'array' => new ArrayLoader(),
'json' => new JsonFileLoader(),
'php' => new PhpFileLoader(),
'xml' => new XmlFileLoader(),
'yaml' => new YamlFileLoader(),
'yml' => new YamlFileLoader(),
];
}

public function getLoader(string $type) : Loader
{
if (count($this->loaders) === 0) {
$this->setDefaultLoaders();
}

if (! isset($this->loaders[$type])) {
throw UnknownLoader::new($type);
}

return $this->loaders[$type];
}
}
76 changes: 47 additions & 29 deletions lib/Doctrine/Migrations/Configuration/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,67 @@
namespace Doctrine\Migrations\Configuration;

use Doctrine\Migrations\Configuration\Exception\UnknownLoader;
use Doctrine\Migrations\Configuration\Loader\ArrayLoader;
use Doctrine\Migrations\Configuration\Loader\JsonFileLoader;
use Doctrine\Migrations\Configuration\Loader\Loader;
use Doctrine\Migrations\Configuration\Loader\PhpFileLoader;
use Doctrine\Migrations\Configuration\Loader\XmlFileLoader;
use Doctrine\Migrations\Configuration\Loader\YamlFileLoader;
use function count;
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported;
use const PATHINFO_EXTENSION;
use function file_exists;
use function pathinfo;

/**
* @internal
* The ConfigurationLoader class is responsible for getting the Configuration instance from one of the supported methods
* for defining the configuration for your migrations.
*/
class ConfigurationLoader
final class ConfigurationLoader
{
/** @var Loader[] */
private $loaders = [];
/** @var ConfigurationFormatLoader */
private $loader;

public function addLoader(string $type, Loader $loader) : void
public function __construct(?ConfigurationFormatLoader $loader = null)
{
$this->loaders[$type] = $loader;
$this->loader = $loader ?: new ConfigurationFormatLoader();
}

private function setDefaultLoaders() : void
public function getConfiguration(?string $configurationFile) : Configuration
{
$this->loaders = [
'array' => new ArrayLoader(),
'json' => new JsonFileLoader(),
'php' => new PhpFileLoader(),
'xml' => new XmlFileLoader(),
'yaml' => new YamlFileLoader(),
'yml' => new YamlFileLoader(),
if ($configurationFile !== null) {
return $this->loadConfig($configurationFile);
}

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

foreach ($defaultConfig as $config) {
if ($this->configExists($config)) {
return $this->loadConfig($config);
}
}

return $this->loader->getLoader('array')->load([]);
}

public function getLoader(string $type) : Loader
private function configExists(string $config) : bool
{
if (count($this->loaders) === 0) {
$this->setDefaultLoaders();
}
return file_exists($config);
}

if (! isset($this->loaders[$type])) {
throw UnknownLoader::new($type);
}
/**
* @throws FileTypeNotSupported
*/
private function loadConfig(string $configFile) : Configuration
{
$extension = pathinfo($configFile, PATHINFO_EXTENSION);

return $this->loaders[$type];
try {
return $this->loader->getLoader($extension)->load($configFile);
} catch (UnknownLoader $e) {
throw FileTypeNotSupported::new();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Connection;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader;
use Doctrine\Migrations\Configuration\Connection\Loader\NoConnectionLoader;

class ConfiguredConnectionLoader implements ConnectionLoader
{
/** @var string|null */
private $dbConfig;

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

public function getConnection() : Connection
{
$loader = new ArrayConnectionConfigurationLoader(
$this->dbConfig ?: 'migrations-db.php',
new NoConnectionLoader()
);

return $loader->getConnection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Doctrine\Migrations\Configuration\Connection;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified;
use Doctrine\Migrations\Configuration\Connection\Loader\Exception\ConnectionNotSpecified;

/**
* The ConnectionLoader defines the interface used to load the Doctrine\DBAL\Connection instance to use
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Tools\Console\Exception;
namespace Doctrine\Migrations\Configuration\Connection\Loader\Exception;

use InvalidArgumentException;

final class ConnectionNotSpecified extends InvalidArgumentException implements ConsoleException
final class ConnectionNotSpecified extends InvalidArgumentException implements LoaderException
{
public static function new() : self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\Configuration\Connection\ConnectionLoader;
use Doctrine\Migrations\Tools\Console\Exception\ConnectionNotSpecified;
use Doctrine\Migrations\Configuration\Connection\Loader\Exception\ConnectionNotSpecified;

/**
* @internal
Expand Down
Loading