Variable.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset\NotationResolver;
  7. use Magento\Framework\View\Asset;
  8. /**
  9. * Variable resolver to allow specific placeholders in CSS files
  10. */
  11. class Variable
  12. {
  13. /**
  14. * Regex matching {{placeholders}}
  15. */
  16. const VAR_REGEX = '/{{([_a-z]*)}}/si';
  17. /**
  18. * Provides the combined base url and base path from the asset context
  19. */
  20. const VAR_BASE_URL_PATH = 'base_url_path';
  21. /**
  22. * @var \Magento\Framework\View\Asset\Repository
  23. */
  24. private $assetRepo;
  25. /**
  26. * @param Asset\Repository $assetRepo
  27. */
  28. public function __construct(Asset\Repository $assetRepo)
  29. {
  30. $this->assetRepo = $assetRepo;
  31. }
  32. /**
  33. * Replaces the placeholder variables into the given path
  34. *
  35. * @param string $path
  36. * @return string
  37. */
  38. public function convertVariableNotation($path)
  39. {
  40. $matches = [];
  41. if (preg_match_all(self::VAR_REGEX, $path, $matches, PREG_SET_ORDER)) {
  42. $replacements = [];
  43. foreach ($matches as $match) {
  44. if (!isset($replacements[$match[0]])) {
  45. $replacements[$match[0]] = $this->getPlaceholderValue($match[1]);
  46. }
  47. }
  48. $path = str_replace(array_keys($replacements), $replacements, $path);
  49. }
  50. return $path;
  51. }
  52. /**
  53. * Process placeholder
  54. *
  55. * @param string $placeholder
  56. * @return string
  57. */
  58. public function getPlaceholderValue($placeholder)
  59. {
  60. /** @var \Magento\Framework\View\Asset\File\FallbackContext $context */
  61. $context = $this->assetRepo->getStaticViewFileContext();
  62. switch ($placeholder) {
  63. case self::VAR_BASE_URL_PATH:
  64. return '{{' . self::VAR_BASE_URL_PATH . '}}' . $context->getAreaCode() .
  65. ($context->getThemePath() ? '/' . $context->getThemePath() . '/' : '') .
  66. '{{locale}}';
  67. default:
  68. return '';
  69. }
  70. }
  71. }