Skip to content

Commit

Permalink
feature #43181 Allow AbstractDoctrineExtension implementations to sup…
Browse files Browse the repository at this point in the history
…port the newer bundle structure (mbabker)

This PR was merged into the 5.4 branch.

Discussion
----------

Allow AbstractDoctrineExtension implementations to support the newer bundle structure

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | yes
| Tickets       | N/A
| License       | MIT
| Doc PR        | TBD

Container extensions inheriting from `Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension` (i.e. the extensions in all Doctrine bundles) currently cannot smoothly support the newer bundle directory structure because the existing API relies on information being gleaned from Reflection for the bundle class.  This PR is an attempt to allow these extensions to support the newer structure.  The notable changes here are:

- The `getMappingDriverBundleConfigDefaults()` method adds a `string $bundleDir` argument which replaces gleaning the bundle directory by reflecting on the Bundle class
- The abstract `getMappingResourceConfigDirectory()` method adds a `string $bundleDir` argument, this is needed for the concrete implementations to have conditional paths supporting both structures (similar to other checks in the framework like the `is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundleDir.'/templates'` check for the templates path)

Commits
-------

136c4a3 [DoctrineBridge] Allow AbstractDoctrineExtension implementations to support the newer bundle structure
  • Loading branch information
fabpot committed Nov 16, 2021
2 parents 6f8b029 + 136c4a3 commit a0d778e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-5.4.md
Expand Up @@ -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
------

Expand Down
2 changes: 2 additions & 0 deletions UPGRADE-6.0.md
Expand Up @@ -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
-----
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Expand Up @@ -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
---
Expand Down
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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'];
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a0d778e

Please sign in to comment.