Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StaticResource.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\App;
7 
12 use Psr\Log\LoggerInterface;
13 
20 {
24  private $state;
25 
29  private $response;
30 
34  private $request;
35 
39  private $publisher;
40 
44  private $assetRepo;
45 
49  private $moduleList;
50 
54  private $objectManager;
55 
59  private $configLoader;
60 
64  private $filesystem;
65 
69  private $deploymentConfig;
70 
74  private $logger;
75 
87  public function __construct(
88  State $state,
89  Response\FileInterface $response,
90  Request\Http $request,
91  View\Asset\Publisher $publisher,
92  \Magento\Framework\View\Asset\Repository $assetRepo,
93  \Magento\Framework\Module\ModuleList $moduleList,
94  \Magento\Framework\ObjectManagerInterface $objectManager,
95  ConfigLoaderInterface $configLoader,
96  DeploymentConfig $deploymentConfig = null
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  }
108 
115  public function launch()
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  }
139 
143  public function catchException(Bootstrap $bootstrap, \Exception $exception)
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  }
156 
164  protected function parsePath($path)
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  }
190 
197  private function getFilesystem()
198  {
199  if (!$this->filesystem) {
200  $this->filesystem = $this->objectManager->get(Filesystem::class);
201  }
202  return $this->filesystem;
203  }
204 
211  private function getLogger()
212  {
213  if (!$this->logger) {
214  $this->logger = $this->objectManager->get(LoggerInterface::class);
215  }
216 
217  return $this->logger;
218  }
219 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
__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)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
catchException(Bootstrap $bootstrap, \Exception $exception)