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
.
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).
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With