Skip to content

Commit 5803e7e

Browse files
committed
Feature: support for $pathsAsFilePaths
This commit adds support for a `$pathsAsFilePaths` parameter, introduced as part of doctrine/orm#12106 It adds a minor BC break (as well as was in the case of `$reportFieldsWhereDeclared` - `8d8579`) - classes, extending `DoctrineOrmMappingsPass` that override `createAttributeMappingDriver()` method will face signature mismatch.
1 parent 820d574 commit 5803e7e

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/DependencyInjection/Compiler/DoctrineOrmMappingsPass.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,25 @@ public static function createAnnotationMappingDriver(array $namespaces, array $d
141141
}
142142

143143
/**
144-
* @param string[] $namespaces List of namespaces that are handled with attribute mapping
145-
* @param string[] $directories List of directories to look for classes with attributes
146-
* @param string[] $managerParameters List of parameters that could which object manager name
147-
* your bundle uses. This compiler pass will automatically
148-
* append the parameter name for the default entity manager
149-
* to this list.
150-
* @param string|false $enabledParameter Service container parameter that must be present to
151-
* enable the mapping. Set to false to not do any check,
152-
* optional.
153-
* @param string[] $aliasMap Map of alias to namespace.
154-
* @param bool $reportFieldsWhereDeclared Will report fields for the classes where they are declared
144+
* @param string[] $namespaces List of namespaces that are handled with attribute mapping
145+
* @param iterable<string> $paths Iterable of source file path names (if {@see $pathsAsFilePaths}),
146+
* or an array of directories where mapping classes can be found.
147+
* @param string[] $managerParameters List of parameters that could which object manager name
148+
* your bundle uses. This compiler pass will automatically
149+
* append the parameter name for the default entity manager
150+
* to this list.
151+
* @param string|false $enabledParameter Service container parameter that must be present to
152+
* enable the mapping. Set to false to not do any check,
153+
* optional.
154+
* @param string[] $aliasMap Map of alias to namespace.
155+
* @param bool $reportFieldsWhereDeclared Will report fields for the classes where they are declared
156+
* @param bool $pathsAsFilePaths whether {@see $paths} are paths to files or directories
155157
*
156158
* @return self
157159
*/
158-
public static function createAttributeMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false)
160+
public static function createAttributeMappingDriver(array $namespaces, iterable $paths, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [], bool $reportFieldsWhereDeclared = false, bool $pathsAsFilePaths = false): self
159161
{
160-
$driver = new Definition(AttributeDriver::class, [$directories, $reportFieldsWhereDeclared]);
162+
$driver = new Definition(AttributeDriver::class, [$paths, $reportFieldsWhereDeclared, $pathsAsFilePaths]);
161163

162164
return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
163165
}

src/DependencyInjection/DoctrineExtension.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
3939
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
4040
use Doctrine\ORM\UnitOfWork;
41+
use Doctrine\Persistence\Mapping\Driver\FilePathNameIterator;
4142
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
4243
use Doctrine\Persistence\Mapping\Driver\PHPDriver;
4344
use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver;
@@ -61,6 +62,7 @@
6162
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
6263
use Symfony\Component\DependencyInjection\Reference;
6364
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
65+
use Symfony\Component\Finder\Finder;
6466
use Symfony\Component\Form\AbstractType;
6567
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\DoctrineTransportFactory;
6668
use Symfony\Component\Messenger\MessageBusInterface;
@@ -881,6 +883,12 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager,
881883
$args[2] = $entityManager['report_fields_where_declared'];
882884
} elseif ($driverType === 'attribute') {
883885
$args[1] = $entityManager['report_fields_where_declared'];
886+
887+
/** @var string[] $directoryPaths */
888+
$directoryPaths = $args[0];
889+
$filesIterator = $this->registerFilesIteratorService($entityManager['name'] . '_' . $driverType, $container, $directoryPaths);
890+
891+
$args[0] = new Reference($filesIterator);
884892
} elseif ($driverType === 'xml') {
885893
$args[1] ??= SimplifiedXmlDriver::DEFAULT_FILE_EXTENSION;
886894
$args[2] = $entityManager['validate_xml_mapping'];
@@ -894,6 +902,29 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager,
894902
$ormConfigDef->addMethodCall('setEntityNamespaces', [$this->aliasMap]);
895903
}
896904

905+
/** @param string[] $dirs */
906+
private function registerFilesIteratorService(string $servicePrefix, ContainerBuilder $container, array $dirs): string
907+
{
908+
$finderService = $this->getObjectManagerElementName($servicePrefix.'_mapping_files_finder');
909+
910+
if ($container->hasDefinition($finderService)) {
911+
$finderDefinition = $container->getDefinition($finderService);
912+
} else {
913+
$finderDefinition = new Definition(Finder::class, []);
914+
}
915+
916+
$finderDefinition->addMethodCall('files');
917+
$finderDefinition->addMethodCall('in', [$dirs]);
918+
$finderDefinition->addMethodCall('name', ['*.php']);
919+
920+
$container->setDefinition($finderService, $finderDefinition);
921+
922+
$filesIterator = $this->getObjectManagerElementName($servicePrefix.'_mapping_files_iterator');
923+
$container->setDefinition($filesIterator, new Definition(FilePathNameIterator::class, [new Reference($finderService)]));
924+
925+
return $filesIterator;
926+
}
927+
897928
/**
898929
* Loads an ORM second level cache bundle mapping information.
899930
*

0 commit comments

Comments
 (0)