welcome back to dyb-tech
This commit is contained in:
vendor/doctrine/migrations/lib/Doctrine/Migrations/Configuration/EntityManager/ConfigurationFile.php
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager;
|
||||
|
||||
use Doctrine\Migrations\Configuration\EntityManager\Exception\FileNotFound;
|
||||
use Doctrine\Migrations\Configuration\EntityManager\Exception\InvalidConfiguration;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
use function file_exists;
|
||||
|
||||
/**
|
||||
* This class will return an EntityManager instance, loaded from a configuration file provided as argument.
|
||||
*/
|
||||
final class ConfigurationFile implements EntityManagerLoader
|
||||
{
|
||||
public function __construct(private readonly string $filename)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the input and return a Configuration, returns null if the config
|
||||
* is not supported.
|
||||
*
|
||||
* @throws InvalidConfiguration
|
||||
*/
|
||||
public function getEntityManager(string|null $name = null): EntityManagerInterface
|
||||
{
|
||||
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 EntityManagerInterface) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
if ($params instanceof EntityManagerLoader) {
|
||||
return $params->getEntityManager();
|
||||
}
|
||||
|
||||
throw InvalidConfiguration::invalidArrayConfiguration();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* The EntityManagerLoader defines the interface used to load the Doctrine\DBAL\EntityManager instance to use
|
||||
* for migrations.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
interface EntityManagerLoader
|
||||
{
|
||||
public function getEntityManager(string|null $name = null): EntityManagerInterface;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager\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\EntityManager\Exception;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
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 EntityManager file has to return an array with database configuration parameters.');
|
||||
}
|
||||
|
||||
public static function invalidManagerType(object $em): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'The returned manager must implement %s, %s returned.',
|
||||
EntityManagerInterface::class,
|
||||
get_debug_type($em),
|
||||
));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager\Exception;
|
||||
|
||||
use Doctrine\Migrations\Exception\MigrationException;
|
||||
|
||||
interface LoaderException extends MigrationException
|
||||
{
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager;
|
||||
|
||||
use Doctrine\Migrations\Configuration\Exception\InvalidLoader;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
final class ExistingEntityManager implements EntityManagerLoader
|
||||
{
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager)
|
||||
{
|
||||
}
|
||||
|
||||
public function getEntityManager(string|null $name = null): EntityManagerInterface
|
||||
{
|
||||
if ($name !== null) {
|
||||
throw InvalidLoader::noMultipleEntityManagers($this);
|
||||
}
|
||||
|
||||
return $this->entityManager;
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Configuration\EntityManager;
|
||||
|
||||
use Doctrine\Migrations\Configuration\EntityManager\Exception\InvalidConfiguration;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
final class ManagerRegistryEntityManager implements EntityManagerLoader
|
||||
{
|
||||
private ManagerRegistry $registry;
|
||||
|
||||
private string|null $defaultManagerName = null;
|
||||
|
||||
public static function withSimpleDefault(ManagerRegistry $registry, string|null $managerName = null): self
|
||||
{
|
||||
$that = new self();
|
||||
$that->registry = $registry;
|
||||
$that->defaultManagerName = $managerName;
|
||||
|
||||
return $that;
|
||||
}
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function getEntityManager(string|null $name = null): EntityManagerInterface
|
||||
{
|
||||
$managerName = $name ?? $this->defaultManagerName;
|
||||
|
||||
$em = $this->registry->getManager($managerName);
|
||||
if (! $em instanceof EntityManagerInterface) {
|
||||
throw InvalidConfiguration::invalidManagerType($em);
|
||||
}
|
||||
|
||||
return $em;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user