From 136c4a314c007092bd675eead8f78060b90948ff Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Sun, 26 Sep 2021 13:23:50 -0500 Subject: [PATCH] [DoctrineBridge] Allow AbstractDoctrineExtension implementations to support the newer bundle structure --- UPGRADE-5.4.md | 6 +++++ UPGRADE-6.0.md | 2 ++ src/Symfony/Bridge/Doctrine/CHANGELOG.md | 3 +++ .../AbstractDoctrineExtension.php | 25 ++++++++++++++----- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/UPGRADE-5.4.md b/UPGRADE-5.4.md index f98966dbf1b2d..57d27502d6876 100644 --- a/UPGRADE-5.4.md +++ b/UPGRADE-5.4.md @@ -12,6 +12,12 @@ Console * Deprecate `HelperSet::setCommand()` and `getCommand()` without replacement +DoctrineBridge +-------------- + + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingDriverBundleConfigDefaults()` + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingResourceConfigDirectory()` + Finder ------ diff --git a/UPGRADE-6.0.md b/UPGRADE-6.0.md index afd57a60fafca..7bcba1f6d0ddf 100644 --- a/UPGRADE-6.0.md +++ b/UPGRADE-6.0.md @@ -10,6 +10,8 @@ DoctrineBridge -------------- * Remove `UserLoaderInterface::loadUserByUsername()` in favor of `UserLoaderInterface::loadUserByIdentifier()` + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingDriverBundleConfigDefaults()` + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingResourceConfigDirectory()` Cache ----- diff --git a/src/Symfony/Bridge/Doctrine/CHANGELOG.md b/src/Symfony/Bridge/Doctrine/CHANGELOG.md index d2e0a7761eeef..4e6abc3b78b1f 100644 --- a/src/Symfony/Bridge/Doctrine/CHANGELOG.md +++ b/src/Symfony/Bridge/Doctrine/CHANGELOG.md @@ -7,6 +7,9 @@ CHANGELOG * Add `DoctrineOpenTransactionLoggerMiddleware` to log when a transaction has been left open * Deprecate `PdoCacheAdapterDoctrineSchemaSubscriber` and add `DoctrineDbalCacheAdapterSchemaSubscriber` instead * `UniqueEntity` constraint retrieves a maximum of two entities if the default repository method is used. + * Add support for the newer bundle structure to `AbstractDoctrineExtension::loadMappingInformation()` + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingDriverBundleConfigDefaults()` + * Add argument `$bundleDir` to `AbstractDoctrineExtension::getMappingResourceConfigDirectory()` 5.3 --- diff --git a/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php b/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php index 2714e27d754a3..236789a721a66 100644 --- a/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php +++ b/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php @@ -73,9 +73,11 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder if ($mappingConfig['is_bundle']) { $bundle = null; + $bundleMetadata = null; foreach ($container->getParameter('kernel.bundles') as $name => $class) { if ($mappingName === $name) { $bundle = new \ReflectionClass($class); + $bundleMetadata = $container->getParameter('kernel.bundles_metadata')[$name]; break; } @@ -85,7 +87,7 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled.', $mappingName)); } - $mappingConfig = $this->getMappingDriverBundleConfigDefaults($mappingConfig, $bundle, $container); + $mappingConfig = $this->getMappingDriverBundleConfigDefaults($mappingConfig, $bundle, $container, $bundleMetadata['path']); if (!$mappingConfig) { continue; } @@ -133,11 +135,20 @@ protected function setMappingDriverConfig(array $mappingConfig, string $mappingN * * Returns false when autodetection failed, an array of the completed information otherwise. * + * @param string|null $bundleDir The bundle directory path + * * @return array|false */ - protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \ReflectionClass $bundle, ContainerBuilder $container) + protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \ReflectionClass $bundle, ContainerBuilder $container/*, string $bundleDir = null*/) { - $bundleDir = \dirname($bundle->getFileName()); + if (\func_num_args() < 4 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) { + trigger_deprecation('symfony/doctrine-bridge', '5.4', 'The "%s()" method will have a new "string $bundleDir = null" argument in version 6.0, not defining it is deprecated.', __METHOD__); + $bundleDir = null; + } else { + $bundleDir = func_get_arg(3); + } + + $bundleDir ?? $bundleDir = \dirname($bundle->getFileName()); if (!$bundleConfig['type']) { $bundleConfig['type'] = $this->detectMetadataDriver($bundleDir, $container); @@ -152,7 +163,7 @@ protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \Re if (\in_array($bundleConfig['type'], ['annotation', 'staticphp', 'attribute'])) { $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingObjectDefaultName(); } else { - $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory(); + $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory($bundleDir); } } else { $bundleConfig['dir'] = $bundleDir.'/'.$bundleConfig['dir']; @@ -246,7 +257,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string */ protected function detectMetadataDriver(string $dir, ContainerBuilder $container) { - $configPath = $this->getMappingResourceConfigDirectory(); + $configPath = $this->getMappingResourceConfigDirectory($dir); $extension = $this->getMappingResourceExtension(); if (glob($dir.'/'.$configPath.'/*.'.$extension.'.xml', \GLOB_NOSORT)) { @@ -440,9 +451,11 @@ abstract protected function getMappingObjectDefaultName(); /** * Relative path from the bundle root to the directory where mapping files reside. * + * @param string|null $bundleDir The bundle directory path + * * @return string */ - abstract protected function getMappingResourceConfigDirectory(); + abstract protected function getMappingResourceConfigDirectory(/*string $bundleDir = null*/); /** * Extension used by the mapping files.