Why I can't get Symfony Finder like a service?
Asked Answered
H

5

7

I use Symfony Standard Edition and try to get Symfony Finder component like a service, but not found it. To use the Finder, I need to create it manually like:

$finder = new Symfony\Component\Finder\Finder();

Why I can't get it from service container? Is it was wrong?

P.S. The Symfony Filesystem component exists in service container and available by name filesystem.

Honkytonk answered 24/9, 2014 at 12:23 Comment(2)
There is a warning at symfony.com/doc/current/components/finder.html . The warning reads "The Finder object doesn't reset its internal state automatically. This means that you need to create a new instance if you do not want get mixed results." Maybe it is not a good idea having it as a service and you might be better off instantiating it right on the spot.Fieldwork
Good to know, thanks @FranciscoLuz !Honkytonk
F
14

The Symfony's Finder component is a standalone component, it is not a part of the FileSystem component:

There is no "finder" service because a Finder instance is an object that needs to be manipulated to work. And as objects are always passed by reference, if someone modifies the service once, everyone will see those changes. This is not what you want for this component.

But you can create your own service as a Finder instance and use this service only in another service (as a dependency).

Fiendish answered 24/9, 2014 at 13:12 Comment(1)
+1 very nice explaination about differences between services (singleton-"stateless") and objectsLoux
A
6

To complement Yann Eugone's answer with some code. This is how you could create your own FinderService from the ServiceComponent and inject into other services.

services.yml

    std.symfony_finder:
        class: Symfony\Component\Finder\Finder
        public: false

    std.your_service:
        class: Std\AppBundle\Services\YourService
        arguments: [@std.symfony_finder]
Arva answered 23/10, 2015 at 9:53 Comment(0)
P
0

Are you sure that its the filesystem component?

http://symfony.com/doc/current/components/finder.html

use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach ($finder as $file) {
    // Print the absolute path
    print $file->getRealpath()."\n";

    // Print the relative path to the file, omitting the filename
    print $file->getRelativePath()."\n";

    // Print the relative path to the file
    print $file->getRelativePathname()."\n";
}

Here is the example. You can install it over composer and its the

{
    "require": {
        "symfony/finder": "2.3.*"
    }
}

After that you can work with it.

Pillow answered 24/9, 2014 at 12:28 Comment(1)
No, I don't said that Finder is a Filesystem component. My question is Why Finder component not available from service container like Filesystem component?Honkytonk
V
0

into services.yml

 Symfony\Component\Finder\Finder:
     class: Symfony\Component\Finder\Finder
Vince answered 28/2, 2020 at 15:26 Comment(2)
Please add a bit of explanation ib why it solves the problem.Lowell
This answer suffers from the problem described in @Yann Eugoné's answer: it creates a single, global instance that will be shared between all classes it's injected into such that modifications in one will be (unpredictably) inherited by all the others.Valoniah
V
0

As Yann Eugoné said, Finder has its own package: symfony/finder. And it is ordinarily just instantiated directly, i.e., new Finder(). However, it can be used as a service. Specify it in your services configuration, e.g., config/services.yml:

Symfony\Component\Finder\Finder:
    shared: false

shared: false is important with Finder. It prevents the container from reusing the same Finder object between classes, which would otherwise result in cross-contamination, i.e., changes made an instance in one class would take effect in everywhere else it is used. See https://symfony.com/doc/current/service_container/shared.html.

Valoniah answered 10/8, 2021 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.