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.

2
  • 1
    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. Commented Aug 2, 2018 at 19:40
  • Good to know, thanks @FranciscoLuz ! Commented Aug 6, 2018 at 14:01

5 Answers 5

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).

1
  • +1 very nice explaination about differences between services (singleton-"stateless") and objects Commented Sep 24, 2014 at 16:13
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]
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.

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? Commented Sep 24, 2014 at 12:32
0

into services.yml

 Symfony\Component\Finder\Finder:
     class: Symfony\Component\Finder\Finder
2
  • 3
    Please add a bit of explanation ib why it solves the problem.
    – vonbrand
    Commented Feb 28, 2020 at 15:59
  • 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. Commented Jul 20, 2020 at 17:12
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.