This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Finder
Christophe Coevoet 54c553cce7 Merge branch '2.7' into 2.8
* 2.7:
  Set the redraw frequency at least to 1. Setting it to 0 would otherwise produce an error.
  [Process] Unset callback after stop to free memory
  Improve error message for undefined DIC aliases
  [VarDumper] fixed .sf-dump z-index
  [DependencyInjection] Validate class names and factory methods
  ampq → amqp
  Fix typo
  CS: remove unneeded parentheses around control statements
  [TwigBridge] Clean deps now that 2.8.0 is tagged
2015-12-05 12:09:21 +01:00
..
Adapter [Finder] Deprecate adapters and related classes 2015-09-18 11:02:33 +02:00
Comparator CS: Binary operators should be arounded by at least one space 2015-03-27 23:05:05 +01:00
Exception [Finder] Deprecate adapters and related classes 2015-09-18 11:02:33 +02:00
Expression [Finder] simplified code 2015-09-28 12:21:41 +02:00
Iterator Merge branch '2.7' into 2.8 2015-12-05 12:09:21 +01:00
Shell [Finder] Deprecate adapters and related classes 2015-09-18 11:02:33 +02:00
Tests Merge branch '2.7' into 2.8 2015-09-30 11:15:55 +02:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
CHANGELOG.md [Finder] Deprecate adapters and related classes 2015-09-18 11:02:33 +02:00
composer.json Merge branch '2.7' into 2.8 2015-10-30 13:15:42 -07:00
Finder.php Merge branch '2.7' into 2.8 2015-09-29 14:32:55 +02:00
Glob.php Merge branch '2.6' into 2.7 2015-05-15 15:33:16 +02:00
LICENSE Updated copyright to 2015 2015-01-01 13:56:52 +01:00
phpunit.xml.dist Add missing exclusions from phpunit.xml.dist 2015-11-18 09:19:46 +01:00
README.md link to https://symfony.com where possible 2015-05-01 16:06:45 +02:00
SplFileInfo.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00

Finder Component

Finder finds files and directories via an intuitive fluent interface.

use Symfony\Component\Finder\Finder;

$finder = new Finder();

$iterator = $finder
  ->files()
  ->name('*.php')
  ->depth(0)
  ->size('>= 1K')
  ->in(__DIR__);

foreach ($iterator as $file) {
    print $file->getRealpath()."\n";
}

The iterator returns instances of Symfony\Component\Finder\SplFileInfo\SplFileInfo. Besides the build-in methods inherited from \SplFileInfo (getPerms(), getSize(), ...), you can also use getRelativePath() and getRelativePathname(). Read the official documentation for more information.

But you can also use it to find files stored remotely like in this example where we are looking for files on Amazon S3:

$s3 = new \Zend_Service_Amazon_S3($key, $secret);
$s3->registerStreamWrapper("s3");

$finder = new Finder();
$finder->name('photos*')->size('< 100K')->date('since 1 hour ago');
foreach ($finder->in('s3://bucket-name') as $file) {
    print $file->getFilename()."\n";
}

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/Finder/
$ composer install
$ phpunit