Search code examples
symfony-3.3

SplFileInfo::getRelativePath() ... Relative to what?


This is regarding Finder component in Symfony 3.3: The documentation of getRelativePath says: "Returns the relative path".

Does anybody know relative to what?

  • Relative to current folder?
  • Relative to app's root?
  • Relative to whatever I provided as parameter to in()?

Solution

  • It would appear that "relative" does indeed mean "Relative to the path provided to in()".

    An example:

    projects
    | a_sub_dir
    | | foo.txt
    | bar.txt
    

    If, in the above setup, we execute the following code:

    $finder = (new Finder())
        ->files()
        ->in('/projects');
    
    foreach ($finder as $file) {
        var_dump([
            'path' => $file->getRelativePath(),
            'pathName' => $file->getRelativePathname(),
        ]);
    }
    

    We will receive the following output

    array(2) {
      ["path"]=>
      string(9) "a_sub_dir"
      ["pathName"]=>
      string(17) "a_sub_dir/foo.txt"
    }
    array(2) {
      ["path"]=>
      string(0) ""
      ["pathName"]=>
      string(7) "bar.txt"
    }
    

    Addendum: When using multiple values for in(), and a file would be found in both values, it will be present in the loop twice. Once with relative path / pathname for each in() value.