Skip to content

Commit 002e105

Browse files
committed
minor symfony#21375 [Finder] Show how to clone the finder to reuse its configuration (javiereguiluz)
This PR was merged into the 6.4 branch. Discussion ---------- [Finder] Show how to clone the finder to reuse its configuration Fixes symfony#21343. Commits ------- a3fa482 [Finder] Show how to clone the finder to reuse its configuration
2 parents 468131f + a3fa482 commit 002e105

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

components/finder.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,23 @@ The ``$file`` variable is an instance of
4343

4444
.. warning::
4545

46-
The ``Finder`` object doesn't reset its internal state automatically.
47-
This means that you need to create a new instance if you do not want
48-
to get mixed results.
46+
The ``Finder`` object is stateful. Any method call will change all its
47+
instances. If you need to perform several searches, create multiple instances
48+
of ``Finder`` or clone it after setting the common configuration::
49+
50+
$finder = new Finder();
51+
// first, configure the common options for the following searches
52+
$finder->files()->in('./templates');
53+
54+
// then, clone the finder to create a new instance before searching for files
55+
foreach ((clone $finder)->name('partial_*') as $file) {
56+
echo('Processing partials config: ' . $file->getRelativePathname());
57+
}
58+
59+
// without cloning, the previous ->name() call would affect this search
60+
foreach ((clone $finder)->name('partial_*')->name('plugin_*') as $file) {
61+
echo('Processing plugin config: ' . $file->getRelativePathname());
62+
}
4963

5064
Searching for Files and Directories
5165
-----------------------------------

0 commit comments

Comments
 (0)