Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Repository.php
Go to the documentation of this file.
1 <?php
8 
13 
22 {
26  const FILE_ID_SEPARATOR = '::';
27 
31  private $baseUrl;
32 
36  private $design;
37 
42  private $themeList;
43 
47  private $assetSource;
48 
52  private $fallbackContext;
53 
57  private $fileContext;
58 
62  private $defaults = null;
63 
67  private $fileFactory;
68 
72  private $fallbackContextFactory;
73 
77  private $contextFactory;
78 
82  private $remoteFactory;
83 
87  private $themeProvider;
88 
100  public function __construct(
101  \Magento\Framework\UrlInterface $baseUrl,
102  \Magento\Framework\View\DesignInterface $design,
103  \Magento\Framework\View\Design\Theme\ListInterface $themeList,
104  \Magento\Framework\View\Asset\Source $assetSource,
105  \Magento\Framework\App\Request\Http $request,
106  FileFactory $fileFactory,
107  File\FallbackContextFactory $fallbackContextFactory,
108  File\ContextFactory $contextFactory,
109  RemoteFactory $remoteFactory
110  ) {
111  $this->baseUrl = $baseUrl;
112  $this->design = $design;
113  $this->themeList = $themeList;
114  $this->assetSource = $assetSource;
115  $this->request = $request;
116  $this->fileFactory = $fileFactory;
117  $this->fallbackContextFactory = $fallbackContextFactory;
118  $this->contextFactory = $contextFactory;
119  $this->remoteFactory = $remoteFactory;
120  }
121 
131  public function updateDesignParams(array &$params)
132  {
133  // Set area
134  if (empty($params['area'])) {
135  $params['area'] = $this->getDefaultParameter('area');
136  }
137 
138  // Set themeModel
139  $theme = null;
140  $area = $params['area'];
141  if (!empty($params['themeId'])) {
142  $theme = $params['themeId'];
143  } elseif (isset($params['theme'])) {
144  $theme = $params['theme'];
145  } elseif (empty($params['themeModel']) && $area !== $this->getDefaultParameter('area')) {
146  $theme = $this->design->getConfigurationDesignTheme($area);
147  }
148 
149  if ($theme) {
150  $params['themeModel'] = $this->getThemeProvider()->getThemeByFullPath($area . '/' . $theme);
151  if (!$params['themeModel']) {
152  throw new \UnexpectedValueException("Could not find theme '$theme' for area '$area'");
153  }
154  } elseif (empty($params['themeModel'])) {
155  $params['themeModel'] = $this->getDefaultParameter('themeModel');
156  }
157 
158  // Set module
159  if (!array_key_exists('module', $params)) {
160  $params['module'] = false;
161  }
162 
163  // Set locale
164  if (empty($params['locale'])) {
165  $params['locale'] = $this->getDefaultParameter('locale');
166  }
167  return $this;
168  }
169 
173  private function getThemeProvider()
174  {
175  if (null === $this->themeProvider) {
176  $this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
177  }
178 
179  return $this->themeProvider;
180  }
181 
188  private function getDefaultParameter($name)
189  {
190  $this->defaults = $this->design->getDesignParams();
191  return $this->defaults[$name];
192  }
193 
201  public function createAsset($fileId, array $params = [])
202  {
203  $this->updateDesignParams($params);
204  list($module, $filePath) = self::extractModule($fileId);
205  if (!$module && $params['module']) {
206  $module = $params['module'];
207  }
208 
209  if (!isset($params['publish'])) {
210  $map = $this->getRepositoryFilesMap($fileId, $params);
211  if ($map) {
212  $params = array_replace($params, $map);
213  }
214  }
215 
216  $isSecure = isset($params['_secure']) ? (bool) $params['_secure'] : null;
217  $themePath = isset($params['theme']) ? $params['theme'] : $this->design->getThemePath($params['themeModel']);
218  $context = $this->getFallbackContext(
220  $isSecure,
221  $params['area'],
222  $themePath,
223  $params['locale']
224  );
225  return $this->fileFactory->create(
226  [
227  'source' => $this->assetSource,
228  'context' => $context,
229  'filePath' => $filePath,
230  'module' => $module,
231  'contentType' => $this->assetSource->getContentType($filePath)
232  ]
233  );
234  }
235 
241  public function getStaticViewFileContext()
242  {
243  $params = [];
244  $this->updateDesignParams($params);
245  $themePath = $this->design->getThemePath($params['themeModel']);
246  $isSecure = $this->request->isSecure();
247  return $this->getFallbackContext(
249  $isSecure,
250  $params['area'],
251  $themePath,
252  $params['locale']
253  );
254  }
255 
268  private function getFallbackContext($urlType, $isSecure, $area, $themePath, $locale)
269  {
270  $secureKey = null === $isSecure ? 'null' : (int)$isSecure;
271  $baseDirType = DirectoryList::STATIC_VIEW;
272  $id = implode('|', [$baseDirType, $urlType, $secureKey, $area, $themePath, $locale]);
273  if (!isset($this->fallbackContext[$id])) {
274  $url = $this->baseUrl->getBaseUrl(['_type' => $urlType, '_secure' => $isSecure]);
275  $this->fallbackContext[$id] = $this->fallbackContextFactory->create(
276  [
277  'baseUrl' => $url,
278  'areaType' => $area,
279  'themePath' => $themePath,
280  'localeCode' => $locale
281  ]
282  );
283  }
284  return $this->fallbackContext[$id];
285  }
286 
294  public function createSimilar($fileId, LocalInterface $similarTo)
295  {
296  list($module, $filePath) = self::extractModule($fileId);
297  if (!$module) {
298  $module = $similarTo->getModule();
299  }
300  return $this->fileFactory->create(
301  [
302  'source' => $this->assetSource,
303  'context' => $similarTo->getContext(),
304  'filePath' => $filePath,
305  'module' => $module,
306  'contentType' => $this->assetSource->getContentType($filePath)
307  ]
308  );
309  }
310 
323  public function createArbitrary(
324  $filePath,
325  $dirPath,
326  $baseDirType = DirectoryList::STATIC_VIEW,
327  $baseUrlType = UrlInterface::URL_TYPE_STATIC
328  ) {
329  $context = $this->getFileContext($baseDirType, $baseUrlType, $dirPath);
330  $contentType = $this->assetSource->getContentType($filePath);
331  return $this->fileFactory->create(
332  [
333  'source' => $this->assetSource,
334  'context' => $context,
335  'filePath' => $filePath,
336  'module' => '',
337  'contentType' => $contentType
338  ]
339  );
340  }
341 
352  private function getFileContext($baseDirType, $urlType, $dirPath)
353  {
354  $id = implode('|', [$baseDirType, $urlType, $dirPath]);
355  if (!isset($this->fileContext[$id])) {
356  $url = $this->baseUrl->getBaseUrl(['_type' => $urlType]);
357  $this->fileContext[$id] = $this->contextFactory->create(
358  ['baseUrl' => $url, 'baseDirType' => $baseDirType, 'contextPath' => $dirPath]
359  );
360  }
361  return $this->fileContext[$id];
362  }
363 
371  public function createRelated($fileId, LocalInterface $relativeTo)
372  {
373  list($module, $filePath) = self::extractModule($fileId);
374  if ($module) {
375  return $this->createSimilar($fileId, $relativeTo);
376  }
377  $filePath = \Magento\Framework\View\FileSystem::getRelatedPath($relativeTo->getFilePath(), $filePath);
378  return $this->createSimilar($filePath, $relativeTo);
379  }
380 
389  public function createRemoteAsset($url, $contentType)
390  {
391  return $this->remoteFactory->create(['url' => $url, 'contentType' => $contentType]);
392  }
393 
400  public function getUrl($fileId)
401  {
402  $asset = $this->createAsset($fileId);
403  return $asset->getUrl();
404  }
405 
416  public function getUrlWithParams($fileId, array $params)
417  {
418  $asset = $this->createAsset($fileId, $params);
419  return $asset->getUrl();
420  }
421 
429  public static function extractModule($fileId)
430  {
431  if (strpos($fileId, self::FILE_ID_SEPARATOR) === false) {
432  return ['', $fileId];
433  }
434  $result = explode(self::FILE_ID_SEPARATOR, $fileId, 2);
435  if (empty($result[0])) {
436  throw new \Magento\Framework\Exception\LocalizedException(
437  new \Magento\Framework\Phrase('Scope separator "::" cannot be used without scope identifier.')
438  );
439  }
440  return [$result[0], $result[1]];
441  }
442 
448  private function getRepositoryFilesMap($fileId, array $params)
449  {
450  $repositoryMap = ObjectManager::getInstance()->get(RepositoryMap::class);
451  return $repositoryMap->getMap($fileId, $params);
452  }
453 }
createSimilar($fileId, LocalInterface $similarTo)
Definition: Repository.php:294
createAsset($fileId, array $params=[])
Definition: Repository.php:201
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
createArbitrary( $filePath, $dirPath, $baseDirType=DirectoryList::STATIC_VIEW, $baseUrlType=UrlInterface::URL_TYPE_STATIC)
Definition: Repository.php:323
__construct(\Magento\Framework\UrlInterface $baseUrl, \Magento\Framework\View\DesignInterface $design, \Magento\Framework\View\Design\Theme\ListInterface $themeList, \Magento\Framework\View\Asset\Source $assetSource, \Magento\Framework\App\Request\Http $request, FileFactory $fileFactory, File\FallbackContextFactory $fallbackContextFactory, File\ContextFactory $contextFactory, RemoteFactory $remoteFactory)
Definition: Repository.php:100
$themeList
Definition: config_data.php:15
getUrlWithParams($fileId, array $params)
Definition: Repository.php:416
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
createRelated($fileId, LocalInterface $relativeTo)
Definition: Repository.php:371
static getRelatedPath($relativeTo, $path)
Definition: FileSystem.php:230
if(!isset($_GET['name'])) $name
Definition: log.php:14