Platformsh\Cli\Helper\FilesystemHelper::makePathAbsolute PHP Méthode

makePathAbsolute() public méthode

The realpath() function will only work for existing files, and not for symlinks. This is a more flexible solution.
public makePathAbsolute ( string $relativePath ) : string
$relativePath string
Résultat string
    public function makePathAbsolute($relativePath)
    {
        if (file_exists($relativePath) && !is_link($relativePath) && ($realPath = realpath($relativePath))) {
            $absolute = $realPath;
        } else {
            $parent = dirname($relativePath);
            if (!is_dir($parent) || !($parentRealPath = realpath($parent))) {
                throw new \InvalidArgumentException('Directory not found: ' . $parent);
            }
            $basename = basename($relativePath);
            $absolute = $basename == '..' ? dirname($parentRealPath) : rtrim($parentRealPath . '/' . $basename, './');
        }
        return $absolute;
    }

Usage Example

 /**
  * Test FilesystemHelper::makePathAbsolute().
  */
 public function testMakePathAbsolute()
 {
     $testDir = $this->tempDir();
     chdir($testDir);
     $path = $this->filesystemHelper->makePathAbsolute('test.txt');
     $this->assertEquals($testDir . '/' . 'test.txt', $path);
     $childDir = $testDir . '/test';
     mkdir($childDir);
     chdir($childDir);
     $path = $this->filesystemHelper->makePathAbsolute('../test.txt');
     $this->assertEquals($testDir . '/' . 'test.txt', $path);
     $path = $this->filesystemHelper->makePathAbsolute('..');
     $this->assertEquals($testDir, $path);
     $this->setExpectedException('InvalidArgumentException');
     $path = $this->filesystemHelper->makePathAbsolute('nonexistent/test.txt');
 }