Reputation: 12306
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
.
Upvotes: 7
Views: 5509
Reputation: 387
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.
Upvotes: 1
Reputation: 9
into services.yml
Symfony\Component\Finder\Finder:
class: Symfony\Component\Finder\Finder
Upvotes: 0
Reputation: 1121
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]
Upvotes: 6
Reputation: 1361
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).
Upvotes: 15
Reputation: 27295
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.
Upvotes: 0