diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionConfigurationChainLoader.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionConfigurationChainLoader.php new file mode 100644 index 0000000000..50857e8d08 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionConfigurationChainLoader.php @@ -0,0 +1,44 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection; + + +final class ConnectionConfigurationChainLoader +{ + /** @var ConnectionLoaderInterface[] */ + private $loaders; + + + public function __construct(array $loaders) + { + $this->loaders = $loaders; + } + + public function load() + { + foreach($this->loaders as $loader) { + if (false !== $confObj = $loader->load()) { + return $confObj; + } + } + + return null; + } +} diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionLoaderInterface.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionLoaderInterface.php new file mode 100644 index 0000000000..c8f99fbe46 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionLoaderInterface.php @@ -0,0 +1,30 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection; + + +interface ConnectionLoaderInterface +{ + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + */ + public function load(); +} diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php new file mode 100644 index 0000000000..6526445bbd --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php @@ -0,0 +1,57 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; + +class ArrayConnectionConfigurationLoader implements ConnectionLoaderInterface +{ + private $filename; + + public function __construct($filename) + { + $this->filename = $filename; + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + */ + public function load() + { + if (empty($this->filename)) { + return false; + } + + if (!file_exists($this->filename)) { + return false; + } + + $params = include $this->filename; + if (!is_array($params)) { + throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.'); + } + + return DriverManager::getConnection($params); + } + +} diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationLoader.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationLoader.php new file mode 100644 index 0000000000..7a240a0d09 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationLoader.php @@ -0,0 +1,49 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; + +class ConnectionConfigurationLoader implements ConnectionLoaderInterface +{ + + private $configuration; + + public function __construct($configuration) + { + $this->configuration = $configuration; + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + */ + public function load() + { + if ($this->configuration) { + + return $this->configuration->getConnection(); + } + + return false; + } +} diff --git a/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php new file mode 100644 index 0000000000..fdf6628219 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php @@ -0,0 +1,63 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; +use Symfony\Component\Console\Helper\HelperSet; + +class ConnectionHelperLoader implements ConnectionLoaderInterface +{ + + + /** + * @var string + */ + private $helperName; + + /** @var HelperSet */ + private $helperSet; + + + /** + * ConnectionHelperLoader constructor. + * @param HelperSet $helperSet + * @param string $helperName + */ + public function __construct(HelperSet $helperSet, $helperName) + { + $this->helperName = $helperName; + $this->helperSet = $helperSet; + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + */ + public function load() + { + if ($this->helperSet->has($this->helperName)) { + + return $this->helperSet->get($this->helperName)->getConnection(); + } + + return false; + } +} diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php index 13477349c9..d1783e79b4 100644 --- a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php +++ b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php @@ -21,6 +21,10 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Migrations\Configuration\Configuration; +use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader; +use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader; +use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionConfigurationChainLoader; use Doctrine\DBAL\Migrations\OutputWriter; use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper; use Symfony\Component\Console\Command\Command; @@ -156,29 +160,23 @@ private function getOutputWriter(OutputInterface $output) private function getConnection(InputInterface $input) { if (!$this->connection) { - if ($input->getOption('db-configuration')) { - if (!file_exists($input->getOption('db-configuration'))) { - throw new \InvalidArgumentException("The specified connection file is not a valid file."); - } - - $params = include $input->getOption('db-configuration'); - if (!is_array($params)) { - throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.'); - } - $this->connection = DriverManager::getConnection($params); - } elseif (file_exists('migrations-db.php')) { - $params = include 'migrations-db.php'; - if (!is_array($params)) { - throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.'); - } - $this->connection = DriverManager::getConnection($params); - } elseif ($this->getHelperSet()->has('connection')) { - $this->connection = $this->getHelper('connection')->getConnection(); - } elseif ($this->configuration) { - $this->connection = $this->configuration->getConnection(); - } else { - throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); + $chainLoader = new ConnectionConfigurationChainLoader( + [ + new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')), + new ArrayConnectionConfigurationLoader('migrations-db.php'), + new ConnectionHelperLoader($this->getHelperSet(), 'connection'), + new ConnectionConfigurationLoader($this->configuration), + ] + ); + $connection = $chainLoader->load(); + + if ($connection) { + $this->connection = $connection; + + return $this->connection; } + + throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); } return $this->connection;