Skip to content

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 12, 2025

Alternative approach to #432 to decouple the class name discovery from the read of metadata in the attribute (and annotation) drivers.

Other drivers use a FileLocator to provide the list of files to read.

For the attribute/annotation/static-php drivers, the driver class itself does the directory scanning, while its main purpose is to load the metadata of each class.

In this PR I introduce a ClassLocator interface with 2 implementations:

  • ClassNames is a simple list of classes names
  • FileClassLocator scans a list of files, it can also be constructed from a list of directories to support the current usage. FileClassLocator::createFromDirectories($dirPaths, $fileExtension, $excludedDirs).

My goal is to make it possible in Symfony to tag any class in the project with the #[Entity] attribute and get it identified by the EntityManager. Symfony DI resource discovery feature will be leveraged so that we don't need to scan the directories in Doctrine (which is done at runtime, and may be cached).

 When checking $includedFiles for an included file,
 the previous `in_array()` approach, executed in a loop,
 is very expensive. Basically, it results in performance
 of O(N * M), where N - number of declared classes,
 M - number of included classes. The current approach
 is O(N), since `isset()` check has constant `O(1)` time.
This change makes it easy for the depending libraries to use iterable of
`SplFileInfo`, adapted to the format of `ColocatedMappingDriver` file paths.
For example, one could provide an instance of Symfony Finder, adapted with
`FilePathNameIterator` without having to reinvent it from scratch.
This change makes it easy for the client code to migrate toward the
newer version. The migration would be as easy as passing
`new FilePathIterator(new DirectoryFilesIterator($paths))` into
the constructor instead of the array of `$paths`.
This commit adds support for `ColocatedMappingDriver` to accept
`iterable` of file paths. Before it was only possible to accept
an array of directory paths. Now, one could provide fine-grained
iterator of only necessary files to the Driver. Bundles should
use Symfony Finder to implement it, since it gives much flexibility
to the client code to configure which files should be included
and which not.

Backward compatibility is achieved with the following approach:
If it's an array, then it should be OK to take a look at `$paths[0]`
and determine if it is a file or a directory. If it's not an array,
we can assume that it's `$filePaths` iterable that's given.
@GromNaN
Copy link
Member Author

GromNaN commented Aug 12, 2025

@rela589n This PR goes a step further than what you've proposed, by exposing a list of class names instead of scanning file.

@GromNaN GromNaN changed the title Introduce ClassLocator to find classes names for attribute drivers Introduce ClassLocator to find class names for attribute drivers Aug 12, 2025
@GromNaN GromNaN force-pushed the class-locator branch 5 times, most recently from 8e475b1 to 1ee8060 Compare August 12, 2025 22:02
greg0ire
greg0ire previously approved these changes Aug 13, 2025
@GromNaN GromNaN deleted the class-locator branch August 15, 2025 08:58
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 16, 2025
The changes integrate doctrine/persistence#433
ClassLocator allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 16, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` property, which allows
passing any instance of `ClassLocator` for the mapping driver to use.
This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths behaviour can be adapted into the new by using
`FileClassLocator::createFromDirectories($directoryPaths)` and
passing it as a class locator to `AttributeDriver`.

# Conflicts:
#	tests/Tests/ORM/Persisters/BinaryIdPersisterTest.php
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 16, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 16, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 16, 2025
The changes integrate doctrine/persistence#433
ClassLocator allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 20, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 20, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 21, 2025
The changes integrate doctrine/persistence#433
ClassLocator allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 21, 2025
The changes integrate doctrine/persistence#433
ClassLocator allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 21, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 21, 2025
These changes integrate doctrine/persistence#433
`ClassLocator` allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-phpcr-odm that referenced this pull request Aug 21, 2025
These changes integrate doctrine/persistence#433
`ClassLocator` allows clients to pass any iterable of classes they might want.
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 21, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 21, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/doctrine-orm that referenced this pull request Aug 23, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator` (`ClassLocator`) property,
which allows passing any instance of `ClassLocator` for the mapping
driver to use. This commit integrates those changes into `AttributeDriver`.

Since `doctrine/orm` maintains the support for `doctrine/persistence`
of older versions, tests ensure that `ClassLocator` actually exists.

The old paths' behaviour can be adapted into the new by passing
`FileClassLocator` into `AttributeDriver`
(see `FileClassLocator::createFromDirectories($directoryPaths)`).
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 27, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 29, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
rela589n added a commit to rela589n/symfony that referenced this pull request Aug 29, 2025
In the scope of doctrine/persistence#433
(available from `doctrine/persistence` >= 4.1) there was added
`ColocatedMappingDriver::$classLocator`, which allows passing any
instance of `ClassLocator` for the mapping driver to use.

This commit integrates those changes into `AbstractDoctrineExtension`,
used by respective ORM, MongoDB-ODM, PHPCR-ODM bundles. The solution
registers a "mapping_class_finder" service that can be used by the
client code to customize class finding logic.

The changes come into play starting with doctrine/persistence >= 4.1,
and the actual registration happens only if `AttributeDriver` supports
`ClassLocator`.

Dependent libraries would adhere to the same interface, where
`ClassLocator` is in the first argument.

The changes were introduced for:
- ORM: doctrine/orm#12131;
- ODM: doctrine/mongodb-odm#2802;
- PHPCR ODM: doctrine/phpcr-odm#875.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants