Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
StaticResource Class Reference
Inheritance diagram for StaticResource:
AppInterface

Public Member Functions

 __construct (State $state, Response\FileInterface $response, Request\Http $request, View\Asset\Publisher $publisher, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Module\ModuleList $moduleList, \Magento\Framework\ObjectManagerInterface $objectManager, ConfigLoaderInterface $configLoader, DeploymentConfig $deploymentConfig=null)
 
 launch ()
 
 catchException (Bootstrap $bootstrap, \Exception $exception)
 
- Public Member Functions inherited from AppInterface
 catchException (App\Bootstrap $bootstrap, \Exception $exception)
 

Protected Member Functions

 parsePath ($path)
 

Additional Inherited Members

- Data Fields inherited from AppInterface
const DISTRO_LOCALE_CODE = 'en_US'
 

Detailed Description

Entry point for retrieving static resources like JS, CSS, images by requested public path

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 19 of file StaticResource.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( State  $state,
Response\FileInterface  $response,
Request\Http  $request,
View\Asset\Publisher  $publisher,
\Magento\Framework\View\Asset\Repository  $assetRepo,
\Magento\Framework\Module\ModuleList  $moduleList,
\Magento\Framework\ObjectManagerInterface  $objectManager,
ConfigLoaderInterface  $configLoader,
DeploymentConfig  $deploymentConfig = null 
)
Parameters
State$state
Response\FileInterface$response
Request\Http$request
View\Asset\Publisher$publisher
\Magento\Framework\View\Asset\Repository$assetRepo
\Magento\Framework\Module\ModuleList$moduleList
\Magento\Framework\ObjectManagerInterface$objectManager
ConfigLoaderInterface$configLoader
DeploymentConfig | null$deploymentConfig

Definition at line 87 of file StaticResource.php.

97  {
98  $this->state = $state;
99  $this->response = $response;
100  $this->request = $request;
101  $this->publisher = $publisher;
102  $this->assetRepo = $assetRepo;
103  $this->moduleList = $moduleList;
104  $this->objectManager = $objectManager;
105  $this->configLoader = $configLoader;
106  $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
107  }

Member Function Documentation

◆ catchException()

catchException ( Bootstrap  $bootstrap,
\Exception  $exception 
)

{}

Definition at line 143 of file StaticResource.php.

144  {
145  $this->getLogger()->critical($exception->getMessage());
146  if ($bootstrap->isDeveloperMode()) {
147  $this->response->setHttpResponseCode(404);
148  $this->response->setHeader('Content-Type', 'text/plain');
149  $this->response->setBody($exception->getMessage() . "\n" . $exception->getTraceAsString());
150  $this->response->sendResponse();
151  } else {
152  require $this->getFilesystem()->getDirectoryRead(DirectoryList::PUB)->getAbsolutePath('errors/404.php');
153  }
154  return true;
155  }
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73

◆ launch()

launch ( )

Finds requested resource and provides it to the client

Returns
\Magento\Framework\App\ResponseInterface
Exceptions

Implements AppInterface.

Definition at line 115 of file StaticResource.php.

116  {
117  // disabling profiling when retrieving static resource
119  $appMode = $this->state->getMode();
120  if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION
121  && !$this->deploymentConfig->getConfigData(
123  )
124  ) {
125  $this->response->setHttpResponseCode(404);
126  } else {
127  $path = $this->request->get('resource');
128  $params = $this->parsePath($path);
129  $this->state->setAreaCode($params['area']);
130  $this->objectManager->configure($this->configLoader->load($params['area']));
131  $file = $params['file'];
132  unset($params['file']);
133  $asset = $this->assetRepo->createAsset($file, $params);
134  $this->response->setFilePath($asset->getSourceFile());
135  $this->publisher->publish($asset);
136  }
137  return $this->response;
138  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ parsePath()

parsePath (   $path)
protected

Parse path to identify parts needed for searching original file

Parameters
string$path
Exceptions

Definition at line 164 of file StaticResource.php.

165  {
166  $path = ltrim($path, '/');
167  $parts = explode('/', $path, 6);
168  if (count($parts) < 5 || preg_match('/\.\.(\\\|\/)/', $path)) {
169  //Checking that path contains all required parts and is not above static folder.
170  throw new \InvalidArgumentException("Requested path '$path' is wrong.");
171  }
172 
173  $result = [];
174  $result['area'] = $parts[0];
175  $result['theme'] = $parts[1] . '/' . $parts[2];
176  $result['locale'] = $parts[3];
177  if (count($parts) >= 6 && $this->moduleList->has($parts[4])) {
178  $result['module'] = $parts[4];
179  } else {
180  $result['module'] = '';
181  if (isset($parts[5])) {
182  $parts[5] = $parts[4] . '/' . $parts[5];
183  } else {
184  $parts[5] = $parts[4];
185  }
186  }
187  $result['file'] = $parts[5];
188  return $result;
189  }

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