welcome back to dyb-tech
This commit is contained in:
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
use Doctrine\Migrations\Configuration\Connection\Exception\FileNotFound;
|
||||
use Doctrine\Migrations\Configuration\Connection\Exception\InvalidConfiguration;
|
||||
use InvalidArgumentException;
|
||||
|
||||
use function file_exists;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* This class will return a Connection instance, loaded from a configuration file provided as argument.
|
||||
*/
|
||||
final class ConfigurationFile implements ConnectionLoader
|
||||
{
|
||||
public function __construct(private readonly string $filename)
|
||||
{
|
||||
}
|
||||
|
||||
public function getConnection(string|null $name = null): Connection
|
||||
{
|
||||
if ($name !== null) {
|
||||
throw new InvalidArgumentException('Only one connection is supported');
|
||||
}
|
||||
|
||||
if (! file_exists($this->filename)) {
|
||||
throw FileNotFound::new($this->filename);
|
||||
}
|
||||
|
||||
$params = include $this->filename;
|
||||
|
||||
if ($params instanceof Connection) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
if ($params instanceof ConnectionLoader) {
|
||||
return $params->getConnection();
|
||||
}
|
||||
|
||||
if (is_array($params)) {
|
||||
return DriverManager::getConnection($params);
|
||||
}
|
||||
|
||||
throw InvalidConfiguration::invalidArrayConfiguration();
|
||||
}
|
||||
}
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\Migrations\Configuration\Connection\Exception\ConnectionNotSpecified;
|
||||
|
||||
/**
|
||||
* The ConnectionLoader defines the interface used to load the Doctrine\DBAL\Connection instance to use
|
||||
* for migrations.
|
||||
*/
|
||||
interface ConnectionLoader
|
||||
{
|
||||
/**
|
||||
* Read the input and return a Connection, returns null if the config
|
||||
* is not supported.
|
||||
*
|
||||
* @throws ConnectionNotSpecified
|
||||
*/
|
||||
public function getConnection(string|null $name = null): Connection;
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\Migrations\Configuration\Connection\Exception\InvalidConfiguration;
|
||||
use Doctrine\Persistence\ConnectionRegistry;
|
||||
|
||||
final class ConnectionRegistryConnection implements ConnectionLoader
|
||||
{
|
||||
private ConnectionRegistry $registry;
|
||||
|
||||
private string|null $defaultConnectionName = null;
|
||||
|
||||
public static function withSimpleDefault(ConnectionRegistry $registry, string|null $connectionName = null): self
|
||||
{
|
||||
$that = new self();
|
||||
$that->registry = $registry;
|
||||
$that->defaultConnectionName = $connectionName;
|
||||
|
||||
return $that;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function getConnection(string|null $name = null): Connection
|
||||
{
|
||||
$connection = $this->registry->getConnection($name ?? $this->defaultConnectionName);
|
||||
if (! $connection instanceof Connection) {
|
||||
throw InvalidConfiguration::invalidConnectionType($connection);
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection\Exception;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class ConnectionNotSpecified extends InvalidArgumentException implements LoaderException
|
||||
{
|
||||
public static function new(): self
|
||||
{
|
||||
return new self(
|
||||
'You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.',
|
||||
);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection\Exception;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class FileNotFound extends InvalidArgumentException implements LoaderException
|
||||
{
|
||||
public static function new(string $file): self
|
||||
{
|
||||
return new self(sprintf('Database configuration file "%s" does not exist.', $file));
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection\Exception;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use InvalidArgumentException;
|
||||
|
||||
use function get_debug_type;
|
||||
use function sprintf;
|
||||
|
||||
final class InvalidConfiguration extends InvalidArgumentException implements LoaderException
|
||||
{
|
||||
public static function invalidArrayConfiguration(): self
|
||||
{
|
||||
return new self('The connection file has to return an array with database configuration parameters.');
|
||||
}
|
||||
|
||||
public static function invalidConnectionType(object $connection): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The returned connection must be a %s instance, %s returned.',
|
||||
Connection::class,
|
||||
get_debug_type($connection),
|
||||
));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection\Exception;
|
||||
|
||||
use Doctrine\Migrations\Exception\MigrationException;
|
||||
|
||||
interface LoaderException extends MigrationException
|
||||
{
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\Connection;
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\Migrations\Configuration\Exception\InvalidLoader;
|
||||
|
||||
final class ExistingConnection implements ConnectionLoader
|
||||
{
|
||||
public function __construct(private readonly Connection $connection)
|
||||
{
|
||||
}
|
||||
|
||||
public function getConnection(string|null $name = null): Connection
|
||||
{
|
||||
if ($name !== null) {
|
||||
throw InvalidLoader::noMultipleConnections($this);
|
||||
}
|
||||
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user