welcome back to dyb-tech
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
class ClassMetadataCollection
|
||||
{
|
||||
private ?string $path = null;
|
||||
private ?string $namespace = null;
|
||||
|
||||
/** @var ClassMetadata[] */
|
||||
private array $metadata;
|
||||
|
||||
/** @param ClassMetadata[] $metadata */
|
||||
public function __construct(array $metadata)
|
||||
{
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
/** @return ClassMetadata[] */
|
||||
public function getMetadata()
|
||||
{
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
/** @param string $path */
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/** @return string|null */
|
||||
public function getPath()
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/** @param string $namespace */
|
||||
public function setNamespace($namespace)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
}
|
||||
|
||||
/** @return string|null */
|
||||
public function getNamespace()
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use Doctrine\ORM\Id\AbstractIdGenerator;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseClassMetadataFactory;
|
||||
|
||||
use function assert;
|
||||
|
||||
class ClassMetadataFactory extends BaseClassMetadataFactory
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents): void
|
||||
{
|
||||
parent::doLoadMetadata($class, $parent, $rootEntityFound, $nonSuperclassParents);
|
||||
|
||||
$customGeneratorDefinition = $class->customGeneratorDefinition;
|
||||
|
||||
if (! isset($customGeneratorDefinition['instance'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert($customGeneratorDefinition['instance'] instanceof AbstractIdGenerator);
|
||||
|
||||
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
|
||||
$class->setIdGenerator($customGeneratorDefinition['instance']);
|
||||
unset($customGeneratorDefinition['instance']);
|
||||
$class->setCustomGeneratorDefinition($customGeneratorDefinition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use RuntimeException;
|
||||
|
||||
use function get_class;
|
||||
use function gettype;
|
||||
use function is_object;
|
||||
use function sprintf;
|
||||
use function trim;
|
||||
|
||||
/** @final */
|
||||
class ContainerEntityListenerResolver implements EntityListenerServiceResolver
|
||||
{
|
||||
private ContainerInterface $container;
|
||||
|
||||
/** @var object[] Map to store entity listener instances. */
|
||||
private array $instances = [];
|
||||
|
||||
/** @var string[] Map to store registered service ids */
|
||||
private array $serviceIds = [];
|
||||
|
||||
/** @param ContainerInterface $container a service locator for listeners */
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear($className = null): void
|
||||
{
|
||||
if ($className === null) {
|
||||
$this->instances = [];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$className = $this->normalizeClassName($className);
|
||||
|
||||
unset($this->instances[$className]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function register($object): void
|
||||
{
|
||||
if (! is_object($object)) {
|
||||
throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object)));
|
||||
}
|
||||
|
||||
$className = $this->normalizeClassName(get_class($object));
|
||||
|
||||
$this->instances[$className] = $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function registerService($className, $serviceId)
|
||||
{
|
||||
$this->serviceIds[$this->normalizeClassName($className)] = $serviceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function resolve($className): object
|
||||
{
|
||||
$className = $this->normalizeClassName($className);
|
||||
|
||||
if (! isset($this->instances[$className])) {
|
||||
if (isset($this->serviceIds[$className])) {
|
||||
$this->instances[$className] = $this->resolveService($this->serviceIds[$className]);
|
||||
} else {
|
||||
$this->instances[$className] = new $className();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->instances[$className];
|
||||
}
|
||||
|
||||
private function resolveService(string $serviceId): object
|
||||
{
|
||||
if (! $this->container->has($serviceId)) {
|
||||
throw new RuntimeException(sprintf('There is no service named "%s"', $serviceId));
|
||||
}
|
||||
|
||||
return $this->container->get($serviceId);
|
||||
}
|
||||
|
||||
private function normalizeClassName(string $className): string
|
||||
{
|
||||
return trim($className, '\\');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use ReflectionClass;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
||||
|
||||
use function array_pop;
|
||||
use function class_exists;
|
||||
use function dirname;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
|
||||
/**
|
||||
* This class provides methods to access Doctrine entity class metadata for a
|
||||
* given bundle, namespace or entity class, for generation purposes
|
||||
*/
|
||||
class DisconnectedMetadataFactory
|
||||
{
|
||||
private ManagerRegistry $registry;
|
||||
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
$this->registry = $registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the metadata of all classes of a bundle.
|
||||
*
|
||||
* @param BundleInterface $bundle A BundleInterface instance
|
||||
*
|
||||
* @return ClassMetadataCollection A ClassMetadataCollection instance
|
||||
*
|
||||
* @throws RuntimeException When bundle does not contain mapped entities.
|
||||
*/
|
||||
public function getBundleMetadata(BundleInterface $bundle)
|
||||
{
|
||||
$namespace = $bundle->getNamespace();
|
||||
$metadata = $this->getMetadataForNamespace($namespace);
|
||||
if (! $metadata->getMetadata()) {
|
||||
throw new RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundle->getName()));
|
||||
}
|
||||
|
||||
$path = $this->getBasePathForClass($bundle->getName(), $bundle->getNamespace(), $bundle->getPath());
|
||||
|
||||
$metadata->setPath($path);
|
||||
$metadata->setNamespace($bundle->getNamespace());
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the metadata of a class.
|
||||
*
|
||||
* @param string $class A class name
|
||||
* @param string $path The path where the class is stored (if known)
|
||||
*
|
||||
* @return ClassMetadataCollection A ClassMetadataCollection instance
|
||||
*
|
||||
* @throws MappingException When class is not valid entity or mapped superclass.
|
||||
*/
|
||||
public function getClassMetadata($class, $path = null)
|
||||
{
|
||||
$metadata = $this->getMetadataForClass($class);
|
||||
if (! $metadata->getMetadata()) {
|
||||
throw MappingException::classIsNotAValidEntityOrMappedSuperClass($class);
|
||||
}
|
||||
|
||||
$this->findNamespaceAndPathForMetadata($metadata, $path);
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the metadata of all classes of a namespace.
|
||||
*
|
||||
* @param string $namespace A namespace name
|
||||
* @param string $path The path where the class is stored (if known)
|
||||
*
|
||||
* @return ClassMetadataCollection A ClassMetadataCollection instance
|
||||
*
|
||||
* @throws RuntimeException When namespace not contain mapped entities.
|
||||
*/
|
||||
public function getNamespaceMetadata($namespace, $path = null)
|
||||
{
|
||||
$metadata = $this->getMetadataForNamespace($namespace);
|
||||
if (! $metadata->getMetadata()) {
|
||||
throw new RuntimeException(sprintf('Namespace "%s" does not contain any mapped entities.', $namespace));
|
||||
}
|
||||
|
||||
$this->findNamespaceAndPathForMetadata($metadata, $path);
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and configure path and namespace for the metadata collection.
|
||||
*
|
||||
* @param string|null $path
|
||||
*
|
||||
* @throws RuntimeException When unable to determine the path.
|
||||
*/
|
||||
public function findNamespaceAndPathForMetadata(ClassMetadataCollection $metadata, $path = null)
|
||||
{
|
||||
$all = $metadata->getMetadata();
|
||||
if (class_exists($all[0]->name)) {
|
||||
$r = new ReflectionClass($all[0]->name);
|
||||
$path = $this->getBasePathForClass($r->getName(), $r->getNamespaceName(), dirname($r->getFilename()));
|
||||
$ns = $r->getNamespaceName();
|
||||
} elseif ($path) {
|
||||
// Get namespace by removing the last component of the FQCN
|
||||
$nsParts = explode('\\', $all[0]->name);
|
||||
array_pop($nsParts);
|
||||
$ns = implode('\\', $nsParts);
|
||||
} else {
|
||||
throw new RuntimeException(sprintf('Unable to determine where to save the "%s" class (use the --path option).', $all[0]->name));
|
||||
}
|
||||
|
||||
$metadata->setPath($path);
|
||||
$metadata->setNamespace($ns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a base path for a class
|
||||
*
|
||||
* @throws RuntimeException When base path not found.
|
||||
*/
|
||||
private function getBasePathForClass(string $name, string $namespace, string $path): string
|
||||
{
|
||||
$namespace = str_replace('\\', '/', $namespace);
|
||||
$search = str_replace('\\', '/', $path);
|
||||
$destination = str_replace('/' . $namespace, '', $search, $c);
|
||||
|
||||
if ($c !== 1) {
|
||||
throw new RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
|
||||
}
|
||||
|
||||
return $destination;
|
||||
}
|
||||
|
||||
private function getMetadataForNamespace(string $namespace): ClassMetadataCollection
|
||||
{
|
||||
$metadata = [];
|
||||
foreach ($this->getAllMetadata() as $m) {
|
||||
if (strpos($m->name, $namespace) !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$metadata[] = $m;
|
||||
}
|
||||
|
||||
return new ClassMetadataCollection($metadata);
|
||||
}
|
||||
|
||||
private function getMetadataForClass(string $entity): ClassMetadataCollection
|
||||
{
|
||||
foreach ($this->registry->getManagers() as $em) {
|
||||
$cmf = new DisconnectedClassMetadataFactory();
|
||||
$cmf->setEntityManager($em);
|
||||
|
||||
if (! $cmf->isTransient($entity)) {
|
||||
return new ClassMetadataCollection([$cmf->getMetadataFor($entity)]);
|
||||
}
|
||||
}
|
||||
|
||||
return new ClassMetadataCollection([]);
|
||||
}
|
||||
|
||||
/** @return ClassMetadata[] */
|
||||
private function getAllMetadata(): array
|
||||
{
|
||||
$metadata = [];
|
||||
foreach ($this->registry->getManagers() as $em) {
|
||||
$cmf = new DisconnectedClassMetadataFactory();
|
||||
$cmf->setEntityManager($em);
|
||||
foreach ($cmf->getAllMetadata() as $m) {
|
||||
$metadata[] = $m;
|
||||
}
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use Doctrine\ORM\Mapping\EntityListenerResolver;
|
||||
|
||||
interface EntityListenerServiceResolver extends EntityListenerResolver
|
||||
{
|
||||
/**
|
||||
* @param string $className
|
||||
* @param string $serviceId
|
||||
*/
|
||||
// phpcs:ignore
|
||||
public function registerService($className, $serviceId);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Bundle\DoctrineBundle\Mapping;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
|
||||
use Doctrine\Persistence\Mapping\ClassMetadata;
|
||||
use Doctrine\Persistence\Mapping\Driver\MappingDriver as MappingDriverInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class MappingDriver implements MappingDriverInterface
|
||||
{
|
||||
private MappingDriverInterface $driver;
|
||||
private ContainerInterface $idGeneratorLocator;
|
||||
|
||||
public function __construct(MappingDriverInterface $driver, ContainerInterface $idGeneratorLocator)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->idGeneratorLocator = $idGeneratorLocator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getAllClassNames()
|
||||
{
|
||||
return $this->driver->getAllClassNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function isTransient($className): bool
|
||||
{
|
||||
return $this->driver->isTransient($className);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function loadMetadataForClass($className, ClassMetadata $metadata): void
|
||||
{
|
||||
$this->driver->loadMetadataForClass($className, $metadata);
|
||||
|
||||
if (
|
||||
! $metadata instanceof OrmClassMetadata
|
||||
|| $metadata->generatorType !== OrmClassMetadata::GENERATOR_TYPE_CUSTOM
|
||||
|| ! isset($metadata->customGeneratorDefinition['class'])
|
||||
|| ! $this->idGeneratorLocator->has($metadata->customGeneratorDefinition['class'])
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$idGenerator = $this->idGeneratorLocator->get($metadata->customGeneratorDefinition['class']);
|
||||
$metadata->setCustomGeneratorDefinition(['instance' => $idGenerator] + $metadata->customGeneratorDefinition);
|
||||
$metadata->setIdGeneratorType(OrmClassMetadata::GENERATOR_TYPE_NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the inner driver
|
||||
*/
|
||||
public function getDriver(): MappingDriverInterface
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user