PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] What is the default order of file elements in Symfony finder?

 January 16, 2022     iterator, php, symfony, symfony-finder     No comments   

Issue

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.


Solution

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.



Answered By - yivi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing