‪TYPO3CMS  10.4
RelativeCssPathFixer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
29 {
37  public function ‪fixRelativeUrlPaths(string $contents, string $newDir): string
38  {
39  // Replace "url()" paths
40  if (stripos($contents, 'url') !== false) {
41  $regex = '/url(\\(\\s*["\']?(?!\\/)([^"\']+)["\']?\\s*\\))/iU';
42  $contents = $this->‪findAndReplaceUrlPathsByRegex($contents, $regex, $newDir, '(\'|\')');
43  }
44  // Replace "@import" paths
45  if (stripos($contents, '@import') !== false) {
46  $regex = '/@import\\s*(["\']?(?!\\/)([^"\']+)["\']?)/i';
47  $contents = $this->‪findAndReplaceUrlPathsByRegex($contents, $regex, $newDir, '"|"');
48  }
49  return $contents;
50  }
51 
61  protected function ‪findAndReplaceUrlPathsByRegex(string $contents, string $regex, string $newDir, string $wrap = '|'): string
62  {
63  $matches = [];
64  $replacements = [];
65  $wrapParts = explode('|', $wrap);
66  preg_match_all($regex, $contents, $matches);
67  foreach ($matches[2] as $matchCount => $match) {
68  // remove '," or white-spaces around
69  $match = trim($match, '\'" ');
70  // we must not rewrite paths containing ":" or "url(", e.g. data URIs (see RFC 2397)
71  if (strpos($match, ':') === false && !preg_match('/url\\s*\\(/i', $match)) {
72  $newPath = GeneralUtility::resolveBackPath($newDir . $match);
73  $replacements[$matches[1][$matchCount]] = $wrapParts[0] . $newPath . $wrapParts[1];
74  }
75  }
76  // replace URL paths in content
77  if (!empty($replacements)) {
78  $contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
79  }
80  return $contents;
81  }
82 }
‪TYPO3\CMS\Core\Resource\RelativeCssPathFixer\findAndReplaceUrlPathsByRegex
‪string findAndReplaceUrlPathsByRegex(string $contents, string $regex, string $newDir, string $wrap='|')
Definition: RelativeCssPathFixer.php:61
‪TYPO3\CMS\Core\Resource\RelativeCssPathFixer\fixRelativeUrlPaths
‪string fixRelativeUrlPaths(string $contents, string $newDir)
Definition: RelativeCssPathFixer.php:37
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\RelativeCssPathFixer
Definition: RelativeCssPathFixer.php:29