FluidTYPO3\Vhs\Service\AssetService::copyReferencedFilesAndReplacePaths PHP Метод

copyReferencedFilesAndReplacePaths() защищенный Метод

Finds and replaces all URLs by using a given regex
protected copyReferencedFilesAndReplacePaths ( string $contents, string $regex, string $originalDirectory, string $wrap = '|' ) : string
$contents string Data to process
$regex string Regex used to find URLs in content
$originalDirectory string Original location to CSS file, if file based.
$wrap string Wrap around replaced values
Результат string Processed data
    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;
    }