protected function copyReferencedFilesAndReplacePaths($contents, $regex, $originalDirectory, $wrap = '|')
{
$matches = [];
$replacements = [];
$wrap = explode('|', $wrap);
preg_match_all($regex, $contents, $matches);
foreach ($matches[2] as $matchCount => $match) {
$match = trim($match, '\'" ');
if (false === strpos($match, ':') && !preg_match('/url\\s*\\(/i', $match)) {
$checksum = md5($originalDirectory . $match);
if (0 < preg_match('/([^\\?#]+)(.+)?/', $match, $items)) {
list(, $path, $suffix) = $items;
} else {
$path = $match;
$suffix = '';
}
$newPath = basename($path);
$extension = pathinfo($newPath, PATHINFO_EXTENSION);
$temporaryFileName = 'vhs-assets-css-' . $checksum . '.' . $extension;
$temporaryFile = constant('PATH_site') . 'typo3temp/' . $temporaryFileName;
$rawPath = GeneralUtility::getFileAbsFileName($originalDirectory . (empty($originalDirectory) ? '' : '/')) . $path;
$realPath = realpath($rawPath);
if (false === $realPath) {
GeneralUtility::sysLog('Asset at path "' . $rawPath . '" not found. Processing skipped.', 'vhs', GeneralUtility::SYSLOG_SEVERITY_WARNING);
} else {
if (false === file_exists($temporaryFile)) {
copy($realPath, $temporaryFile);
}
$replacements[$matches[1][$matchCount]] = $wrap[0] . $temporaryFileName . $suffix . $wrap[1];
}
}
}
if (false === empty($replacements)) {
$contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
}
return $contents;
}