Pool.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset\PreProcessor;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\View\Asset\PreProcessorInterface;
  9. /**
  10. * A registry of asset preprocessors (not to confuse with the "Registry" pattern)
  11. */
  12. class Pool
  13. {
  14. /**
  15. * Name of property referenced to pre-processor implementation class
  16. */
  17. const PREPROCESSOR_CLASS = 'class';
  18. /**
  19. * @var array
  20. */
  21. private $preprocessors;
  22. /**
  23. * @var array
  24. */
  25. private $instances;
  26. /**
  27. * @var Helper\SortInterface
  28. */
  29. private $sorter;
  30. /**
  31. * @var string
  32. */
  33. private $defaultPreprocessor;
  34. /**
  35. * @var ObjectManagerInterface
  36. */
  37. private $objectManager;
  38. /**
  39. * Constructor
  40. *
  41. * @param ObjectManagerInterface $objectManager
  42. * @param Helper\SortInterface $sorter
  43. * @param string $defaultPreprocessor
  44. * @param array $preprocessors
  45. */
  46. public function __construct(
  47. ObjectManagerInterface $objectManager,
  48. Helper\SortInterface $sorter,
  49. $defaultPreprocessor,
  50. array $preprocessors = []
  51. ) {
  52. $this->preprocessors = $preprocessors;
  53. $this->sorter = $sorter;
  54. $this->defaultPreprocessor = $defaultPreprocessor;
  55. $this->objectManager = $objectManager;
  56. }
  57. /**
  58. * Execute preprocessors instances suitable to convert source content type into a destination one
  59. *
  60. * @param Chain $chain
  61. * @return void
  62. */
  63. public function process(Chain $chain)
  64. {
  65. $type = $chain->getTargetContentType();
  66. foreach ($this->getPreProcessors($type) as $preProcessor) {
  67. $preProcessor->process($chain);
  68. }
  69. }
  70. /**
  71. * Retrieve preProcessors by types
  72. *
  73. * @param string $type
  74. * @return PreProcessorInterface[]
  75. * @throws \UnexpectedValueException
  76. */
  77. private function getPreProcessors($type)
  78. {
  79. if (isset($this->instances[$type])) {
  80. return $this->instances[$type];
  81. }
  82. if (isset($this->preprocessors[$type])) {
  83. $preprocessors = $this->sorter->sort($this->preprocessors[$type]);
  84. } else {
  85. $preprocessors = [
  86. 'default' => [self::PREPROCESSOR_CLASS => $this->defaultPreprocessor]
  87. ];
  88. }
  89. $this->instances[$type] = [];
  90. foreach ($preprocessors as $preprocessor) {
  91. $instance = $this->objectManager->get($preprocessor[self::PREPROCESSOR_CLASS]);
  92. if (!$instance instanceof PreProcessorInterface) {
  93. throw new \UnexpectedValueException(
  94. '"' . $preprocessor[self::PREPROCESSOR_CLASS] . '" has to implement the PreProcessorInterface.'
  95. );
  96. }
  97. $this->instances[$type][] = $instance;
  98. }
  99. return $this->instances[$type];
  100. }
  101. }