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..6e287902a2 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/ConnectionLoaderInterface.php @@ -0,0 +1,33 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection; + + +use Doctrine\DBAL\Connection; + +interface ConnectionLoaderInterface +{ + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + * @return Connection|null + */ + public function chosen(); +} 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..5e64015254 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ArrayConnectionConfigurationLoader.php @@ -0,0 +1,58 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\Connection; +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 + * @return Connection|null + */ + public function chosen() + { + if (empty($this->filename)) { + return null; + } + + if (!file_exists($this->filename)) { + return null; + } + + $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/ConnectionConfigurationChainLoader.php b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationChainLoader.php new file mode 100644 index 0000000000..f77ca8f525 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationChainLoader.php @@ -0,0 +1,52 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; + +final class ConnectionConfigurationChainLoader implements ConnectionLoaderInterface +{ + /** @var ConnectionLoaderInterface[] */ + private $loaders; + + + public function __construct(array $loaders) + { + $this->loaders = $loaders; + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + * @return Connection|null + */ + public function chosen() + { + foreach($this->loaders as $loader) { + if (null !== $confObj = $loader->chosen()) { + return $confObj; + } + } + + return null; + } +} 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..1e5a3412c2 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionConfigurationLoader.php @@ -0,0 +1,54 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Migrations\Configuration\Configuration; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; + +class ConnectionConfigurationLoader implements ConnectionLoaderInterface +{ + /** @var Configuration */ + private $configuration; + + public function __construct(Configuration $configuration=null) + { + if ($configuration !== null) { + $this->configuration = $configuration; + } + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + * @return Connection|null + */ + public function chosen() + { + if ($this->configuration) { + + return $this->configuration->getConnection(); + } + + return null; + } +} 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..8875786336 --- /dev/null +++ b/lib/Doctrine/DBAL/Migrations/Configuration/Connection/Loader/ConnectionHelperLoader.php @@ -0,0 +1,70 @@ +. + */ + +namespace Doctrine\DBAL\Migrations\Configuration\Connection\Loader; + + +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Migrations\Configuration\Connection\ConnectionLoaderInterface; +use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; +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 = null, $helperName) + { + $this->helperName = $helperName; + if ($helperSet === null) { + $helperSet = new HelperSet(); + } + $this->helperSet = $helperSet; + } + + /** + * read the input and return a Configuration, returns `false` if the config + * is not supported + * @return Connection|null + */ + public function chosen() + { + if ($this->helperSet->has($this->helperName)) { + $connectionHelper = $this->helperSet->get($this->helperName); + if ($connectionHelper instanceof ConnectionHelper) { + + return $connectionHelper->getConnection(); + } + } + + return null; + } +} diff --git a/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php b/lib/Doctrine/DBAL/Migrations/Tools/Console/Command/AbstractCommand.php index 13477349c9..3d44c83b4b 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\Loader\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->chosen(); + + 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;