The following code returns different results in two environments (which however were supposed to be as close to one another as possible: the same PHP version, etc.)
The /var/www/html/project/vendor/package/path/to/some/folder
directory contains exactly the same set of files in both environments. The files themselves were put there by composer
.
use Symfony\Component\Finder\Finder;
// Don't mind this. The real app uses an autoloading mechanism.
require __DIR__ . '/../vendor/symfony/finder/Finder.php';
require __DIR__ . '/../vendor/symfony/finder/SplFileInfo.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/RecursiveDirectoryIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/ExcludeDirectoryFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/PathFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FilenameFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FileTypeFilterIterator.php';
$path = '/var/www/html/project/vendor/package/path/to/some/folder';
$path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
$iterator = $path->getIterator();
$iterator->rewind();
$current = $iterator->current();
echo $current->getRelativePathname();
It isn't random. The results are consistent in a single environment. But a different path name is diplayed in either environment. I was able to trace it down to PHP's native: FilterIterator::rewind
, but I'm not sure if rewinding is the problem here.
There is no defined "default" order for the Finder component.
Finder's iterator (Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator
) extends on the core \RecursiveDirectoryIterator
, which does not define a default order (nor a way to "set" the order). The ordering is thus not guaranteed defined or guaranteed. It is highly system-dependant and file-system dependant, and should not be be relied upon.
If you need a dependable, consistent sort order, just use the appropriate sort*()
methods on the Finder instance. This will cause the result to be wrapped on a SortableIterator when you call getIterator()
, and get you results ordered in a predictable manner.