Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileManager.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
20  private $config;
21 
25  private $filesystem;
26 
30  private $appState;
31 
35  private $assetRepo;
36 
43  public function __construct(
44  Config $config,
45  \Magento\Framework\Filesystem $appFilesystem,
46  AppState $appState,
47  \Magento\Framework\View\Asset\Repository $assetRepo
48  ) {
49  $this->config = $config;
50  $this->filesystem = $appFilesystem;
51  $this->appState = $appState;
52  $this->assetRepo = $assetRepo;
53  }
54 
60  public function createRequireJsConfigAsset()
61  {
62  $relPath = $this->config->getConfigFileRelativePath();
63  $this->ensureSourceFile($relPath);
64  return $this->assetRepo->createArbitrary($relPath, '');
65  }
66 
72  public function createMinResolverAsset()
73  {
74  $relPath = $this->config->getMinResolverRelativePath();
75  $this->ensureMinResolverFile($relPath);
76  return $this->assetRepo->createArbitrary($relPath, '');
77  }
78 
84  public function createRequireJsMixinsAsset()
85  {
86  return $this->assetRepo->createArbitrary($this->config->getMixinsFileRelativePath(), '');
87  }
88 
94  public function createRequireJsAsset()
95  {
96  return $this->assetRepo->createArbitrary($this->config->getRequireJsFileRelativePath(), '');
97  }
98 
104  public function createUrlResolverAsset()
105  {
106  return $this->assetRepo->createArbitrary($this->config->getUrlResolverFileRelativePath(), '');
107  }
108 
115  {
116  if ($this->checkIfExist($this->config->getMapFileRelativePath())) {
117  return $this->assetRepo->createArbitrary($this->config->getMapFileRelativePath(), '');
118  } else {
119  return null;
120  }
121  }
122 
131  private function ensureSourceFile($relPath)
132  {
133  $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
134  if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
135  $dir->writeFile($relPath, $this->config->getConfig());
136  }
137  }
138 
145  private function ensureMinResolverFile($relPath)
146  {
147  $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
148  if ($this->appState->getMode() == AppState::MODE_DEVELOPER || !$dir->isExist($relPath)) {
149  $dir->writeFile($relPath, $this->config->getMinResolverCode());
150  }
151  }
152 
158  public function createStaticJsAsset()
159  {
160  if ($this->appState->getMode() != AppState::MODE_PRODUCTION) {
161  return false;
162  }
163  return $this->assetRepo->createAsset(Config::STATIC_FILE_NAME);
164  }
165 
171  public function createBundleJsPool()
172  {
173  $bundles = [];
174  if ($this->appState->getMode() == AppState::MODE_PRODUCTION) {
175  $libDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
177  $context = $this->assetRepo->getStaticViewFileContext();
178 
179  $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
180 
181  if (!$libDir->isExist($bundleDir)) {
182  return [];
183  }
184 
185  foreach ($libDir->read($bundleDir) as $bundleFile) {
186  if (pathinfo($bundleFile, PATHINFO_EXTENSION) !== 'js') {
187  continue;
188  }
189  $relPath = $libDir->getRelativePath($bundleFile);
190  $bundles[] = $this->assetRepo->createArbitrary($relPath, '');
191  }
192  }
193 
194  return $bundles;
195  }
196 
203  public function clearBundleJsPool()
204  {
205  $dirWrite = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
207  $context = $this->assetRepo->getStaticViewFileContext();
208  $bundleDir = $context->getPath() . '/' . Config::BUNDLE_JS_DIR;
209  return $dirWrite->delete($bundleDir);
210  }
211 
218  private function checkIfExist($relPath)
219  {
220  $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
221  return $dir->isExist($relPath);
222  }
223 }
$config
Definition: fraud_order.php:17
__construct(Config $config, \Magento\Framework\Filesystem $appFilesystem, AppState $appState, \Magento\Framework\View\Asset\Repository $assetRepo)
Definition: FileManager.php:43