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

symlinkAll() public méthode

Symlink or copy all files and folders between two directories.
public symlinkAll ( string $source, string $destination, boolean $skipExisting = true, boolean $recursive = false, string[] $blacklist = [], boolean $copy = false )
$source string
$destination string
$skipExisting boolean
$recursive boolean
$blacklist string[]
$copy boolean
    public function symlinkAll($source, $destination, $skipExisting = true, $recursive = false, $blacklist = [], $copy = false)
    {
        if (!is_dir($destination)) {
            mkdir($destination);
        }
        // The symlink won't work if $source is a relative path.
        $source = realpath($source);
        // Files to always skip.
        $skip = ['.git', '.DS_Store'];
        $skip = array_merge($skip, $blacklist);
        $sourceDirectory = opendir($source);
        while ($file = readdir($sourceDirectory)) {
            // Skip symlinks, '.' and '..', and files in $skip.
            if ($file === '.' || $file === '..' || $this->inBlacklist($file, $skip) || is_link($source . '/' . $file)) {
                continue;
            }
            $sourceFile = $source . '/' . $file;
            $linkFile = $destination . '/' . $file;
            if ($recursive && !is_link($linkFile) && is_dir($linkFile) && is_dir($sourceFile)) {
                $this->symlinkAll($sourceFile, $linkFile, $skipExisting, $recursive, $blacklist, $copy);
                continue;
            } elseif (file_exists($linkFile)) {
                if ($skipExisting) {
                    continue;
                } else {
                    throw new \Exception('File exists: ' . $linkFile);
                }
            } elseif (is_link($linkFile)) {
                // This is a broken link. Remove it.
                $this->remove($linkFile);
            }
            if ($copy) {
                $this->copyAll($sourceFile, $linkFile, $blacklist);
            } else {
                if ($this->relative) {
                    $sourceFile = $this->makePathRelative($sourceFile, $linkFile);
                    chdir($destination);
                }
                $this->fs->symlink($sourceFile, $linkFile, $this->copyOnWindows);
            }
        }
        closedir($sourceDirectory);
    }

Usage Example

 /**
  * Test FilesystemHelper::symlinkAll().
  */
 public function testSymlinkAll()
 {
     $testSource = $this->tempDir(true);
     $testDestination = $this->tempDir();
     // Test plain symlinking.
     $this->filesystemHelper->symlinkAll($testSource, $testDestination);
     $this->assertFileExists($testDestination . '/test-file');
     $this->assertFileExists($testDestination . '/test-dir/test-file');
     $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file');
     // Test with skipping an existing file.
     $testDestination = $this->tempDir();
     touch($testDestination . '/test-file');
     $this->filesystemHelper->symlinkAll($testSource, $testDestination);
     $this->assertFileExists($testDestination . '/test-file');
     $this->assertFileExists($testDestination . '/test-dir/test-file');
     $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file');
     // Test with relative links. This has no effect on Windows.
     $testDestination = $this->tempDir();
     $this->filesystemHelper->setRelativeLinks(true);
     $this->filesystemHelper->symlinkAll($testSource, $testDestination);
     $this->filesystemHelper->setRelativeLinks(false);
     $this->assertFileExists($testDestination . '/test-file');
     $this->assertFileExists($testDestination . '/test-dir/test-file');
     $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file');
     // Test with a blacklist.
     $testDestination = $this->tempDir();
     touch($testSource . '/test-file2');
     $this->filesystemHelper->symlinkAll($testSource, $testDestination, true, false, ['test-file']);
     $this->assertFileNotExists($testDestination . '/test-file');
     $this->assertFileExists($testDestination . '/test-dir/test-file');
     $this->assertFileExists($testDestination . '/test-nesting/1/2/3/test-file');
 }