Phrozn\Site\DefaultSite::tryToCopyFile PHP Method

tryToCopyFile() private method

It will skip copy if filename matches one of $skip regexes, or if $file is a folder. Introduces a dependency on the SPL extension, but as the doc states : "As of PHP 5.3.0 this extension can no longer be disabled and is therefore always available."
private tryToCopyFile ( SplFileInfo $file, SplFileInfo $inDir, SplFileInfo $outDir, string[] $skip = [] )
$file SplFileInfo
$inDir SplFileInfo
$outDir SplFileInfo
$skip string[] Array of regexes
    private function tryToCopyFile($file, $inDir, $outDir, $skip = array())
    {
        // collect info
        $inputFile = $file->getRealPath();
        $inputDir = $inDir->getRealPath();
        $outputDir = $outDir->getRealPath();
        // skip if not a file
        if (!$file->isFile()) {
            return;
        }
        // skip if file matches any skip regex
        if (count($skip)) {
            foreach ($skip as $skipRegex) {
                // inspect the whole path, not just the basename
                if (preg_match($skipRegex, $inputFile)) {
                    return;
                }
            }
        }
        // sanity check -- REALLY not supposed to happen
        if (strpos($inputFile, $inputDir) !== 0) {
            throw new \RuntimeException(sprintf('File "%s" is not a child of input folder "%s"', $inputFile, $inputDir));
        }
        $relativePath = substr($inputFile, strlen($inputDir) + 1);
        $outputFile = $outputDir . DIRECTORY_SEPARATOR . $relativePath;
        // copy the file
        try {
            $destinationDir = dirname($outputFile);
            if (!is_dir($destinationDir)) {
                mkdir($destinationDir, 0777, true);
            }
            if (!copy($inputFile, $outputFile)) {
                throw new \RuntimeException(sprintf('Failed copy to "%s"', $outputFile));
            }
            $cwd = realpath(getcwd());
            $inputFile = str_replace($cwd, '.', $inputFile);
            $outputFile = str_replace($cwd, '.', $outputFile);
            $this->getOutputter()->stdout('%b' . $outputFile . '%n copied');
        } catch (\Exception $e) {
            $this->getOutputter()->stderr($inputFile . ': ' . $e->getMessage());
        }
    }