Simple.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Design\Fallback\Rule;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Component\ComponentRegistrarInterface;
  9. use Magento\Framework\Module\Dir\Reader;
  10. /**
  11. * Class with simple substitution parameters to values
  12. */
  13. class Simple implements RuleInterface
  14. {
  15. /**
  16. * Optional params for rule
  17. *
  18. * @var array
  19. */
  20. protected $optionalParams;
  21. /**
  22. * Pattern for a simple rule
  23. *
  24. * @var string
  25. */
  26. protected $pattern;
  27. /**
  28. * Constructor
  29. *
  30. * @param string $pattern
  31. * @param array $optionalParams
  32. */
  33. public function __construct(
  34. $pattern,
  35. array $optionalParams = []
  36. ) {
  37. $this->pattern = $pattern;
  38. $this->optionalParams = $optionalParams;
  39. }
  40. /**
  41. * Get ordered list of folders to search for a file
  42. *
  43. * @param array $params array of parameters
  44. * @return array folders to perform a search
  45. * @throws \InvalidArgumentException
  46. */
  47. public function getPatternDirs(array $params)
  48. {
  49. $pattern = $this->pattern;
  50. if (preg_match_all('/<([a-zA-Z\_]+)>/', $pattern, $matches)) {
  51. foreach ($matches[1] as $placeholder) {
  52. if (empty($params[$placeholder])) {
  53. if (in_array($placeholder, $this->optionalParams)) {
  54. return [];
  55. } else {
  56. throw new \InvalidArgumentException("Required parameter '{$placeholder}' was not passed");
  57. }
  58. }
  59. $pattern = str_replace('<' . $placeholder . '>', $params[$placeholder], $pattern);
  60. }
  61. }
  62. return [$pattern];
  63. }
  64. }