Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Merged.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Merged implements \Iterator
14 {
18  const CACHE_VIEW_REL = '_cache';
19 
23  protected $logger;
24 
28  protected $mergeStrategy;
29 
33  private $assetRepo;
34 
38  protected $assets;
39 
43  protected $contentType;
44 
48  private $versionStorage;
49 
53  protected $isInitialized = false;
54 
65  public function __construct(
66  \Psr\Log\LoggerInterface $logger,
68  \Magento\Framework\View\Asset\Repository $assetRepo,
69  array $assets,
70  \Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage = null
71  ) {
72  $this->logger = $logger;
73  $this->mergeStrategy = $mergeStrategy;
74  $this->assetRepo = $assetRepo;
75  $this->versionStorage = $versionStorage ?: ObjectManager::getInstance()->get(
76  \Magento\Framework\App\View\Deployment\Version\StorageInterface::class
77  );
78 
79  if (!$assets) {
80  throw new \InvalidArgumentException('At least one asset has to be passed for merging.');
81  }
83  foreach ($assets as $asset) {
84  if (!($asset instanceof MergeableInterface)) {
85  throw new \InvalidArgumentException(
86  'Asset has to implement \Magento\Framework\View\Asset\MergeableInterface.'
87  );
88  }
89  if (!$this->contentType) {
90  $this->contentType = $asset->getContentType();
91  } elseif ($asset->getContentType() != $this->contentType) {
92  throw new \InvalidArgumentException(
93  "Content type '{$asset->getContentType()}' cannot be merged with '{$this->contentType}'."
94  );
95  }
96  }
97  $this->assets = $assets;
98  }
99 
105  protected function initialize()
106  {
107  if (!$this->isInitialized) {
108  $this->isInitialized = true;
109  try {
110  $mergedAsset = $this->createMergedAsset($this->assets);
111  $this->mergeStrategy->merge($this->assets, $mergedAsset);
112  $this->assets = [$mergedAsset];
113  } catch (\Exception $e) {
114  $this->logger->critical($e);
115  }
116  }
117  }
118 
125  private function createMergedAsset(array $assets)
126  {
127  $paths = [];
129  foreach ($assets as $asset) {
130  $paths[] = $asset->getPath();
131  }
132  $paths = array_unique($paths);
133 
134  $version = $this->versionStorage->load();
135  if ($version) {
136  $paths[] = $version;
137  }
138 
139  $filePath = md5(implode('|', $paths)) . '.' . $this->contentType;
140  return $this->assetRepo->createArbitrary($filePath, self::getRelativeDir());
141  }
142 
148  public function current()
149  {
150  $this->initialize();
151  return current($this->assets);
152  }
153 
157  public function key()
158  {
159  $this->initialize();
160  return key($this->assets);
161  }
162 
166  public function next()
167  {
168  $this->initialize();
169  next($this->assets);
170  }
171 
175  public function rewind()
176  {
177  $this->initialize();
178  reset($this->assets);
179  }
180 
184  public function valid()
185  {
186  $this->initialize();
187  return (bool)current($this->assets);
188  }
189 
195  public static function getRelativeDir()
196  {
197  return self::CACHE_VIEW_REL . '/merged';
198  }
199 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct( $fileName, $sourcePath=null, $area=null, $theme=null, $locale=null, $module=null)
Definition: Asset.php:56
$paths
Definition: _bootstrap.php:83