Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields
Repository Class Reference

Public Member Functions

 __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)
 
 updateDesignParams (array &$params)
 
 createAsset ($fileId, array $params=[])
 
 getStaticViewFileContext ()
 
 createSimilar ($fileId, LocalInterface $similarTo)
 
 createArbitrary ( $filePath, $dirPath, $baseDirType=DirectoryList::STATIC_VIEW, $baseUrlType=UrlInterface::URL_TYPE_STATIC)
 
 createRelated ($fileId, LocalInterface $relativeTo)
 
 createRemoteAsset ($url, $contentType)
 
 getUrl ($fileId)
 
 getUrlWithParams ($fileId, array $params)
 

Static Public Member Functions

static extractModule ($fileId)
 

Data Fields

const FILE_ID_SEPARATOR = '::'
 

Detailed Description

A repository service for view assets @SuppressWarnings(PHPMD.CouplingBetweenObjects)

@api

Since
100.0.2

Definition at line 21 of file Repository.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)
Parameters
\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 at line 100 of file Repository.php.

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  }
$themeList
Definition: config_data.php:15

Member Function Documentation

◆ createArbitrary()

createArbitrary (   $filePath,
  $dirPath,
  $baseDirType = DirectoryList::STATIC_VIEW,
  $baseUrlType = UrlInterface::URL_TYPE_STATIC 
)

Create a file asset with an arbitrary path

This kind of file is not subject of fallback system Client code is responsible for ensuring that the file is in specified directory

Parameters
string$filePath
string$dirPath
string$baseDirType
string$baseUrlType
Returns
File

Definition at line 323 of file Repository.php.

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  }

◆ createAsset()

createAsset (   $fileId,
array  $params = [] 
)

Create a file asset that's subject of fallback system

Parameters
string$fileId
array$params
Returns
File

Definition at line 201 of file Repository.php.

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  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ createRelated()

createRelated (   $fileId,
LocalInterface  $relativeTo 
)

Create a file asset with path relative to specified local asset

Parameters
string$fileId
LocalInterface$relativeTo
Returns
File

Definition at line 371 of file Repository.php.

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  }
createSimilar($fileId, LocalInterface $similarTo)
Definition: Repository.php:294
static getRelatedPath($relativeTo, $path)
Definition: FileSystem.php:230

◆ createRemoteAsset()

createRemoteAsset (   $url,
  $contentType 
)

Create a remote asset value object

Parameters
string$url
string$contentType
Returns
Remote @codeCoverageIgnore

Definition at line 389 of file Repository.php.

390  {
391  return $this->remoteFactory->create(['url' => $url, 'contentType' => $contentType]);
392  }

◆ createSimilar()

createSimilar (   $fileId,
LocalInterface  $similarTo 
)

Create a file asset similar to an existing local asset by using its context

Parameters
string$fileId
LocalInterface$similarTo
Returns
File

Definition at line 294 of file Repository.php.

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  }

◆ extractModule()

static extractModule (   $fileId)
static

Extract module name from specified file ID

Parameters
string$fileId
Returns
array
Exceptions

Definition at line 429 of file Repository.php.

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  }

◆ getStaticViewFileContext()

getStaticViewFileContext ( )

Get current context for static view files

Returns
\Magento\Framework\View\Asset\File\FallbackContext

Definition at line 241 of file Repository.php.

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  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getUrl()

getUrl (   $fileId)

Getter for static view file URL

Parameters
string$fileId
Returns
string

Definition at line 400 of file Repository.php.

401  {
402  $asset = $this->createAsset($fileId);
403  return $asset->getUrl();
404  }
createAsset($fileId, array $params=[])
Definition: Repository.php:201

◆ getUrlWithParams()

getUrlWithParams (   $fileId,
array  $params 
)

A getter for static view file URL with special parameters

To omit parameters and have them automatically determined from application state, use getUrl()

Parameters
string$fileId
array$params
Returns
string
See also
getUrl()

Definition at line 416 of file Repository.php.

417  {
418  $asset = $this->createAsset($fileId, $params);
419  return $asset->getUrl();
420  }
createAsset($fileId, array $params=[])
Definition: Repository.php:201
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ updateDesignParams()

updateDesignParams ( array &  $params)

Update required parameters with default values if custom not specified

Parameters
array&$params
Exceptions

Definition at line 131 of file Repository.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

Field Documentation

◆ FILE_ID_SEPARATOR

const FILE_ID_SEPARATOR = '::'

Scope separator for module notation of file ID

Definition at line 26 of file Repository.php.


The documentation for this class was generated from the following file: