Banago\PHPloy\PHPloy::copy PHP Method

copy() public method

Copy given directory's contents.
public copy ( $copyDirs )
    public function copy($copyDirs)
    {
        $dirNameTrimFunc = function ($name) {
            return rtrim(str_replace('\\', '/', trim($name)), '/');
        };
        foreach ($copyDirs as $copyRule) {
            list($fromDir, $toDir) = array_map($dirNameTrimFunc, array_pad(explode('->', $copyRule), 2, '.'));
            // Skip to next element if to and from are the same
            if ($fromDir == $toDir) {
                $this->cli->out("<red>Omitting directory <white>{$fromDir}<red>, as it would copy on itself");
                break;
            }
            // Skip to next element if from is not present
            if (!$this->connection->has($fromDir)) {
                $this->cli->out("<red>Omitting directory <white>{$fromDir}<red>, as it does not exist on the server");
                break;
            }
            $this->cli->out("<red>Copying directory <white>{$fromDir}<red> to <white>{$toDir}");
            // File/dir listing
            $contents = $this->connection->listContents($fromDir, false);
            if (count($contents) < 1) {
                $this->cli->out(" - Nothing to copy in {$fromDir}");
                return;
            }
            foreach ($contents as $item) {
                if ($item['type'] === 'file') {
                    $newPath = $toDir . '/' . pathinfo($item['path'], PATHINFO_BASENAME);
                    if ($this->connection->has($newPath)) {
                        $this->connection->delete($newPath);
                    }
                    $this->connection->copy($item['path'], $newPath);
                    $this->cli->out("<red> × {$item['path']} is copied to {$newPath}");
                } elseif ($item['type'] === 'dir') {
                    $dirParts = explode('/', $item['path']);
                    $this->copy([$fromDir . '/' . end($dirParts) . '->' . $toDir . '/' . end($dirParts)]);
                }
            }
            $this->cli->out("<red>Copied <white>{$fromDir} <red>to <white>{$toDir}");
        }
    }