Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Filesystem.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy\Model;
7 
8 use Symfony\Component\Console\Output\OutputInterface;
15 
24 {
35  const PERMISSIONS_FILE = 0640;
36 
47  const PERMISSIONS_DIR = 0750;
48 
52  const DEFAULT_THEME = 'Magento/blank';
53 
57  private $writer;
58 
62  private $reader;
63 
67  private $objectManager;
68 
72  private $filesystem;
73 
77  private $directoryList;
78 
82  private $driverFile;
83 
87  private $storeView;
88 
92  private $shell;
93 
97  private $functionCallPath;
98 
102  private $userCollection;
103 
107  private $locale;
108 
122  public function __construct(
123  \Magento\Framework\App\DeploymentConfig\Writer $writer,
124  \Magento\Framework\App\DeploymentConfig\Reader $reader,
125  \Magento\Framework\ObjectManagerInterface $objectManager,
126  \Magento\Framework\Filesystem $filesystem,
128  \Magento\Framework\Filesystem\Driver\File $driverFile,
129  \Magento\Store\Model\Config\StoreView $storeView,
130  \Magento\Framework\ShellInterface $shell,
131  UserCollection $userCollection = null,
132  Locale $locale = null
133  ) {
134  $this->writer = $writer;
135  $this->reader = $reader;
136  $this->objectManager = $objectManager;
137  $this->filesystem = $filesystem;
138  $this->directoryList = $directoryList;
139  $this->driverFile = $driverFile;
140  $this->storeView = $storeView;
141  $this->shell = $shell;
142  $this->userCollection = $userCollection ?: $this->objectManager->get(UserCollection::class);
143  $this->locale = $locale ?: $this->objectManager->get(Locale::class);
144  $this->functionCallPath =
145  PHP_BINARY . ' -f ' . BP . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'magento ';
146  }
147 
154  public function regenerateStatic(
155  OutputInterface $output
156  ) {
157  // Сlear generated/code, generated/metadata/, var/view_preprocessed and pub/static directories
158  $this->cleanupFilesystem(
159  [
165  ]
166  );
167 
168  // Trigger code generation
169  $this->compile($output);
170  // Trigger static assets compilation and deployment
172  }
173 
181  protected function deployStaticContent(
182  OutputInterface $output
183  ) {
184  $output->writeln('Starting deployment of static content');
185  $cmd = $this->functionCallPath . 'setup:static-content:deploy -f '
186  . implode(' ', $this->getUsedLocales());
187 
191  try {
192  $execOutput = $this->shell->execute($cmd);
193  } catch (LocalizedException $e) {
194  $output->writeln('Something went wrong while deploying static content. See the error log for details.');
195  throw $e;
196  }
197  $output->writeln($execOutput);
198  $output->writeln('Deployment of static content complete');
199  }
200 
206  private function getAdminUserInterfaceLocales()
207  {
208  $locales = [];
209  foreach ($this->userCollection as $user) {
210  $locales[] = $user->getInterfaceLocale();
211  }
212  return $locales;
213  }
214 
221  private function getUsedLocales()
222  {
223  $usedLocales = array_merge(
224  $this->storeView->retrieveLocales(),
225  $this->getAdminUserInterfaceLocales()
226  );
227 
228  return array_map(
229  function ($locale) {
230  if (!$this->locale->isValid($locale)) {
231  throw new \InvalidArgumentException(
232  $locale . ' argument has invalid value, run info:language:list for list of available locales'
233  );
234  }
235 
236  return $locale;
237  },
238  array_unique($usedLocales)
239  );
240  }
241 
249  protected function compile(OutputInterface $output)
250  {
251  $output->writeln('Starting compilation');
252  $this->cleanupFilesystem(
253  [
257  ]
258  );
259  $cmd = $this->functionCallPath . 'setup:di:compile';
260 
267  try {
268  $execOutput = $this->shell->execute($cmd);
269  } catch (LocalizedException $e) {
270  $output->writeln('Something went wrong while compiling generated code. See the error log for details.');
271  throw $e;
272  }
273  $output->writeln($execOutput);
274  $output->writeln('Compilation complete');
275  }
276 
283  public function cleanupFilesystem($directoryCodeList)
284  {
285  $excludePatterns = ['#.htaccess#', '#deployed_version.txt#'];
286  foreach ($directoryCodeList as $code) {
288  $directoryPath = $this->directoryList->getPath(DirectoryList::STATIC_VIEW);
289  if ($this->driverFile->isExists($directoryPath)) {
290  $files = $this->driverFile->readDirectory($directoryPath);
291  foreach ($files as $file) {
292  foreach ($excludePatterns as $pattern) {
293  if (preg_match($pattern, $file)) {
294  continue 2;
295  }
296  }
297  if ($this->driverFile->isFile($file)) {
298  $this->driverFile->deleteFile($file);
299  } else {
300  $this->driverFile->deleteDirectory($file);
301  }
302  }
303  }
304  } else {
305  $this->filesystem->getDirectoryWrite($code)
306  ->delete();
307  }
308  }
309  }
310 
326  protected function changePermissions($directoryCodeList, $dirPermissions, $filePermissions)
327  {
328  foreach ($directoryCodeList as $code) {
329  $directoryPath = $this->directoryList->getPath($code);
330  if ($this->driverFile->isExists($directoryPath)) {
331  $this->filesystem->getDirectoryWrite($code)
332  ->changePermissionsRecursively('', $dirPermissions, $filePermissions);
333  } else {
334  $this->driverFile->createDirectory($directoryPath, $dirPermissions);
335  }
336  }
337  }
338 
351  public function lockStaticResources()
352  {
353  // Lock /generated/code, /generated/metadata/ and /var/view_preprocessed directories
354  $this->changePermissions(
355  [
359  ],
360  self::PERMISSIONS_DIR,
361  self::PERMISSIONS_FILE
362  );
363  }
364 }
$objectManager
Definition: bootstrap.php:17
$pattern
Definition: website.php:22
compile(OutputInterface $output)
Definition: Filesystem.php:249
cleanupFilesystem($directoryCodeList)
Definition: Filesystem.php:283
if(!file_exists($installConfigFile)) if(!defined('TESTS_INSTALLATION_DB_CONFIG_FILE')) $shell
Definition: bootstrap.php:46
__construct(\Magento\Framework\App\DeploymentConfig\Writer $writer, \Magento\Framework\App\DeploymentConfig\Reader $reader, \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, \Magento\Framework\Filesystem\Driver\File $driverFile, \Magento\Store\Model\Config\StoreView $storeView, \Magento\Framework\ShellInterface $shell, UserCollection $userCollection=null, Locale $locale=null)
Definition: Filesystem.php:122
$locales
Definition: locales.php:14
$user
Definition: dummy_user.php:13
deployStaticContent(OutputInterface $output)
Definition: Filesystem.php:181
regenerateStatic(OutputInterface $output)
Definition: Filesystem.php:154
const BP
Definition: autoload.php:14
$filesystem
changePermissions($directoryCodeList, $dirPermissions, $filePermissions)
Definition: Filesystem.php:326
foreach($appDirs as $dir) $files
$code
Definition: info.phtml:12