Skip to content

[DOC]: Describe custom metadata files regex with AttributeDriver::setFileRegex() #11904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 3.3.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/en/reference/advanced-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,48 @@ state you can pass a closure as the third argument. It will be called with
the arguments proxydir, namespace and className when the proxy file could not
be found.

Custom Metadata Files Regex
---------------------------

When using the ``AttributeDriver`` in combination with domain-oriented directory structures,
it's common to have domain-related classes like tests, fixtures, commands within the same
directory as entity classes. These should typically be excluded from metadata mapping.

For example, a typical directory structure for User aggregate might look like this:

.. code-block:: text

User/
├── User.php
├── UserFixture.php
├── Email/
│ ├── Email.php
│ └── EmailTest.php
├── Password/
│ ├── Password.php
│ └── PasswordTest.php
└── Actions/
└── Register/
├── RegisterUserCommand.php
└── RegisterUserTest.php

You can define a regular expression to filter which files the driver will consider as mapping sources:

.. code-block:: php

<?php
use Doctrine\ORM\Mapping\Driver\AttributeDriver;

$driver = new AttributeDriver([__DIR__ . '/src/Domain/User']);
$driver->setFileRegex('/^(?!.*(Test|Fixture|Command)\.php$).*\.php$/');

The provided regular expression is going to be matched against each file name during mapping discovery.
In this example, files ending with ``Test.php``, ``Fixture.php``, or ``Command.php`` will be excluded,
while `User.php`, `Email.php`, and `Password.php` will be included.

This ensures that only actual entity classes are taken into account when loading metadata,
preventing unrelated files from being included by the driver.

Multiple Metadata Sources
-------------------------

Expand Down