Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 271
Victor Bocharsky Avatar asked Sep 24 '14 12:09

Victor Bocharsky


2 Answers

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

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

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

like image 129
Yann Eugoné Avatar answered Sep 28 '22 01:09

Yann Eugoné


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]
like image 27
HKandulla Avatar answered Sep 28 '22 01:09

HKandulla