Processor.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Model\Template\Css;
  7. use Magento\Framework\View\Asset\NotationResolver\Variable;
  8. use Magento\Framework\View\Asset\Repository;
  9. /**
  10. * Class for processing css placeholders
  11. */
  12. class Processor
  13. {
  14. /**
  15. * @var Repository
  16. */
  17. private $assetRepository;
  18. /**
  19. * @param Repository $assetRepository
  20. */
  21. public function __construct(Repository $assetRepository)
  22. {
  23. $this->assetRepository = $assetRepository;
  24. }
  25. /**
  26. * Process css placeholders
  27. *
  28. * @param string $css
  29. * @return string
  30. */
  31. public function process($css)
  32. {
  33. $matches = [];
  34. if (preg_match_all(Variable::VAR_REGEX, $css, $matches, PREG_SET_ORDER)) {
  35. $replacements = [];
  36. foreach ($matches as $match) {
  37. if (!isset($replacements[$match[0]])) {
  38. $replacements[$match[0]] = $this->getPlaceholderValue($match[1]);
  39. }
  40. }
  41. $css = str_replace(array_keys($replacements), $replacements, $css);
  42. }
  43. return $css;
  44. }
  45. /**
  46. * Retrieve placeholder value
  47. *
  48. * @param string $placeholder
  49. * @return string
  50. */
  51. private function getPlaceholderValue($placeholder)
  52. {
  53. /** @var \Magento\Framework\View\Asset\File\FallbackContext $context */
  54. $context = $this->assetRepository->getStaticViewFileContext();
  55. switch ($placeholder) {
  56. case 'base_url_path':
  57. return $context->getBaseUrl();
  58. case 'locale':
  59. return $context->getLocale();
  60. default:
  61. return '';
  62. }
  63. }
  64. }