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());
}
}