CssResolver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Url;
  7. use Magento\Framework\View\FileSystem;
  8. /**
  9. * CSS URLs resolver class.
  10. * This utility class provides a set of methods to work with CSS files.
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class CssResolver
  15. {
  16. /**
  17. * PCRE that matches non-absolute URLs in CSS content
  18. */
  19. const REGEX_CSS_RELATIVE_URLS =
  20. '#url\s*\(\s*(?(?=\'|").)(?!http\://|https\://|/|data\:)(.+?)(?:[\#\?].*?|[\'"])?\s*\)#';
  21. /**
  22. * Adjust relative URLs in CSS content as if the file with this content is to be moved to new location
  23. *
  24. * @param string $cssContent
  25. * @param string $relatedPath
  26. * @param string $filePath
  27. * @return mixed
  28. */
  29. public function relocateRelativeUrls($cssContent, $relatedPath, $filePath)
  30. {
  31. $offset = FileSystem::offsetPath($relatedPath, $filePath);
  32. $callback = function ($path) use ($offset) {
  33. return FileSystem::normalizePath($offset . '/' . $path);
  34. };
  35. return $this->replaceRelativeUrls($cssContent, $callback);
  36. }
  37. /**
  38. * A generic method for applying certain callback to all found relative URLs in CSS content
  39. *
  40. * Traverse through all relative URLs and apply a callback to each path
  41. * The $inlineCallback is a user function that obtains the URL value and must return a replacement
  42. *
  43. * @param string $cssContent
  44. * @param callback $inlineCallback
  45. * @return string
  46. */
  47. public function replaceRelativeUrls($cssContent, $inlineCallback)
  48. {
  49. $patterns = self::extractRelativeUrls($cssContent);
  50. if ($patterns) {
  51. $replace = [];
  52. foreach ($patterns as $pattern => $path) {
  53. if (!isset($replace[$pattern])) {
  54. $newPath = call_user_func($inlineCallback, $path);
  55. $newPattern = str_replace($path, $newPath, $pattern);
  56. $replace[$pattern] = $newPattern;
  57. }
  58. }
  59. if ($replace) {
  60. $cssContent = str_replace(array_keys($replace), array_values($replace), $cssContent);
  61. }
  62. }
  63. return $cssContent;
  64. }
  65. /**
  66. * Extract all "import" directives from CSS-content and put them to the top of document
  67. *
  68. * @param string $cssContent
  69. * @return string
  70. */
  71. public function aggregateImportDirectives($cssContent)
  72. {
  73. $parts = preg_split('/(@import\s.+?;\s*)/', $cssContent, -1, PREG_SPLIT_DELIM_CAPTURE);
  74. $imports = [];
  75. $css = [];
  76. foreach ($parts as $part) {
  77. if (0 === strpos($part, '@import', 0)) {
  78. $imports[] = trim($part);
  79. } else {
  80. $css[] = $part;
  81. }
  82. }
  83. $result = implode($css);
  84. if ($imports) {
  85. $result = implode("\n", $imports)
  86. . "\n/* The above import directives are aggregated from content. */\n"
  87. . $result;
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Subroutine for obtaining url() fragments from the CSS content
  93. *
  94. * @param string $cssContent
  95. * @return array
  96. */
  97. private static function extractRelativeUrls($cssContent)
  98. {
  99. preg_match_all(self::REGEX_CSS_RELATIVE_URLS, $cssContent, $matches);
  100. if (!empty($matches[0]) && !empty($matches[1])) {
  101. return array_combine($matches[0], $matches[1]);
  102. }
  103. return [];
  104. }
  105. }