src/Filesystem.php
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
    {
        $targetDir = rtrim($targetDir, '/\\');
        $originDir = rtrim($originDir, '/\\');

        // Iterate in destination folder to remove obsolete entries
        if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
            $deleteIterator = $iterator;
            if (null === $deleteIterator) {
                $flags = \FilesystemIterator::SKIP_DOTS;
                $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
            }
            foreach ($deleteIterator as $file) {
                $origin = str_replace($targetDir, $originDir, $file->getPathname());
                if (!$this->exists($origin)) {
                    $this->remove($file);
                }
            }
        }

        $copyOnWindows = false;
        if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
            $copyOnWindows = $options['copy_on_windows'];
        }

        if (null === $iterator) {
            $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
        }

        foreach ($iterator as $file) {
            $target = str_replace($originDir, $targetDir, $file->getPathname());

            if ($copyOnWindows) {
                if (is_link($file) || is_file($file)) {
                    $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
                } elseif (is_dir($file)) {
                    $this->mkdir($target);
                } else {
                    throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file), 0, null);
                }
            } else {
                if (is_link($file)) {
                    $this->symlink($file->getLinkTarget(), $target);
                } elseif (is_dir($file)) {
                    $this->mkdir($target);
                } elseif (is_file($file)) {
                    $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
                } else {
                    throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file), 0, null);
                }
            }
        }
    }
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
    {
        $targetDir = rtrim($targetDir, '/\\');
        $originDir = rtrim($originDir, '/\\');

        // Iterate in destination folder to remove obsolete entries
        if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
            $deleteIterator = $iterator;
            if (null === $deleteIterator) {
                $flags = \FilesystemIterator::SKIP_DOTS;
                $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
            }
            foreach ($deleteIterator as $file) {
                $origin = str_replace($targetDir, $originDir, $file->getPathname());
                if (!$this->exists($origin)) {
                    $this->remove($file);
                }
            }
        }

        $copyOnWindows = false;
        if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
            $copyOnWindows = $options['copy_on_windows'];
        }

        if (null === $iterator) {
            $flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
        }

        foreach ($iterator as $file) {
            $target = str_replace($originDir, $targetDir, $file->getPathname());

            if ($copyOnWindows) {
                if (is_link($file) || is_file($file)) {
                    $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
                } elseif (is_dir($file)) {
                    $this->mkdir($target);
                } else {
                    throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file), 0, null);
                }
            } else {
                if (is_link($file)) {
                    $this->symlink($file->getLinkTarget(), $target);
                } elseif (is_dir($file)) {
                    $this->mkdir($target);
                } elseif (is_file($file)) {
                    $this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
                } else {
                    throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file), 0, null);
                }
            }
        }
    }
public function mirror ( $ originDir , $ targetDir , \ Traversable $ iterator = null , $ options = array ( ) ) { $ targetDir = rtrim ( $ targetDir , '/\\' ) ; $ originDir = rtrim ( $ originDir , '/\\' ) ; // Iterate in destination folder to remove obsolete entries if ( $ this -> exists ( $ targetDir ) && isset ( $ options [ 'delete' ] ) && $ options [ 'delete' ] ) { $ deleteIterator = $ iterator ; if ( null === $ deleteIterator ) { $ flags = \ FilesystemIterator :: SKIP_DOTS ; $ deleteIterator = new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ targetDir , $ flags ) , \ RecursiveIteratorIterator :: CHILD_FIRST ) ; } foreach ( $ deleteIterator as $ file ) { $ origin = str_replace ( $ targetDir , $ originDir , $ file -> getPathname ( ) ) ; if ( ! $ this -> exists ( $ origin ) ) { $ this -> remove ( $ file ) ; } } } $ copyOnWindows = false ; if ( isset ( $ options [ 'copy_on_windows' ] ) && ! function_exists ( 'symlink' ) ) { $ copyOnWindows = $ options [ 'copy_on_windows' ] ; } if ( null === $ iterator ) { $ flags = $ copyOnWindows ? \ FilesystemIterator :: SKIP_DOTS | \ FilesystemIterator :: FOLLOW_SYMLINKS : \ FilesystemIterator :: SKIP_DOTS ; $ iterator = new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ originDir , $ flags ) , \ RecursiveIteratorIterator :: SELF_FIRST ) ; } foreach ( $ iterator as $ file ) { $ target = str_replace ( $ originDir , $ targetDir , $ file -> getPathname ( ) ) ; if ( $ copyOnWindows ) { if ( is_link ( $ file ) || is_file ( $ file ) ) { $ this -> copy ( $ file , $ target , isset ( $ options [ 'override' ] ) ? $ options [ 'override' ] : false ) ; } elseif ( is_dir ( $ file ) ) { $ this -> mkdir ( $ target ) ; } else { throw new \ RuntimeException ( sprintf ( 'Unable to guess "%s" file type.' , $ file ) , 0 , null ) ; } } else { if ( is_link ( $ file ) ) { $ this -> symlink ( $ file -> getLinkTarget ( ) , $ target ) ; } elseif ( is_dir ( $ file ) ) { $ this -> mkdir ( $ target ) ; } elseif ( is_file ( $ file ) ) { $ this -> copy ( $ file , $ target , isset ( $ options [ 'override' ] ) ? $ options [ 'override' ] : false ) ; } else { throw new \ RuntimeException ( sprintf ( 'Unable to guess "%s" file type.' , $ file ) , 0 , null ) ; } } } }
Mirrors a directory to another.

@param string       $originDir The origin directory
@param string       $targetDir The target directory
@param \Traversable $iterator  A Traversable instance
@param array        $options   An array of boolean options
Valid options are:
- $options['override'] Whether to override an existing file on copy or not (see copy())
- $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
- $options['delete'] Whether to delete files that are not in the source directory (defaults to false)

@throws \RuntimeException When file type is unknown
Mirrors a directory to another .