|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Deploy\Model; |
| 8 | + |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Magento\Framework\App\State; |
| 11 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 12 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 13 | +use Magento\Developer\Console\Command\CssDeployCommand; |
| 14 | + |
| 15 | +/** |
| 16 | + * A class to manage Magento modes |
| 17 | + * |
| 18 | + */ |
| 19 | +class Filesystem |
| 20 | +{ |
| 21 | + /** |
| 22 | + * File access permissions |
| 23 | + */ |
| 24 | + const PERMISSIONS_FILE = 0640; |
| 25 | + |
| 26 | + /** |
| 27 | + * Directory access permissions |
| 28 | + */ |
| 29 | + const PERMISSIONS_DIR = 0750; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default theme when no theme is stored in configuration |
| 33 | + */ |
| 34 | + const DEFAULT_THEME = 'Magento/blank'; |
| 35 | + |
| 36 | + /** @var \Magento\Framework\App\DeploymentConfig\Writer */ |
| 37 | + private $writer; |
| 38 | + |
| 39 | + /** @var \Magento\Framework\App\DeploymentConfig\Reader */ |
| 40 | + private $reader; |
| 41 | + |
| 42 | + /** @var \Magento\Framework\ObjectManagerInterface */ |
| 43 | + private $objectManager; |
| 44 | + |
| 45 | + /** @var \Magento\Framework\Filesystem */ |
| 46 | + private $filesystem; |
| 47 | + |
| 48 | + /** @var \Magento\Framework\App\Filesystem\DirectoryList */ |
| 49 | + private $directoryList; |
| 50 | + |
| 51 | + /** @var \Magento\Framework\Filesystem\Driver\File */ |
| 52 | + private $driverFile; |
| 53 | + |
| 54 | + /** @var \Magento\Store\Model\Config\StoreView */ |
| 55 | + private $storeView; |
| 56 | + |
| 57 | + /** @var \Magento\Framework\Shell */ |
| 58 | + private $shell; |
| 59 | + |
| 60 | + /** @var string */ |
| 61 | + private $functionCallPath; |
| 62 | + |
| 63 | + /** |
| 64 | + * @param \Magento\Framework\App\DeploymentConfig\Writer $writer |
| 65 | + * @param \Magento\Framework\App\DeploymentConfig\Reader $reader |
| 66 | + * @param \Magento\Framework\ObjectManagerInterface $objectManager |
| 67 | + * @param \Magento\Framework\Filesystem $filesystem |
| 68 | + * @param \Magento\Framework\App\Filesystem\DirectoryList $directoryList |
| 69 | + * @param \Magento\Framework\Filesystem\Driver\File $driverFile |
| 70 | + * @param \Magento\Store\Model\Config\StoreView $storeView |
| 71 | + * @param \Magento\Framework\Shell $shell |
| 72 | + */ |
| 73 | + public function __construct( |
| 74 | + \Magento\Framework\App\DeploymentConfig\Writer $writer, |
| 75 | + \Magento\Framework\App\DeploymentConfig\Reader $reader, |
| 76 | + \Magento\Framework\ObjectManagerInterface $objectManager, |
| 77 | + \Magento\Framework\Filesystem $filesystem, |
| 78 | + \Magento\Framework\App\Filesystem\DirectoryList $directoryList, |
| 79 | + \Magento\Framework\Filesystem\Driver\File $driverFile, |
| 80 | + \Magento\Store\Model\Config\StoreView $storeView, |
| 81 | + \Magento\Framework\Shell $shell |
| 82 | + ) { |
| 83 | + $this->writer = $writer; |
| 84 | + $this->reader = $reader; |
| 85 | + $this->objectManager = $objectManager; |
| 86 | + $this->filesystem = $filesystem; |
| 87 | + $this->directoryList = $directoryList; |
| 88 | + $this->driverFile = $driverFile; |
| 89 | + $this->storeView = $storeView; |
| 90 | + $this->shell = $shell; |
| 91 | + $this->functionCallPath = 'php -f ' . BP . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'magento '; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Regenerate static |
| 96 | + * |
| 97 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + public function regenerateStatic( |
| 101 | + \Symfony\Component\Console\Output\OutputInterface $output |
| 102 | + ) { |
| 103 | + // Сlean up /var/generation, /var/di/, /var/view_preprocessed and /pub/static directories |
| 104 | + $this->cleanupFilesystem( |
| 105 | + [ |
| 106 | + DirectoryList::CACHE, |
| 107 | + DirectoryList::GENERATION, |
| 108 | + DirectoryList::DI, |
| 109 | + DirectoryList::TMP_MATERIALIZATION_DIR |
| 110 | + ] |
| 111 | + ); |
| 112 | + $this->changePermissions( |
| 113 | + [ |
| 114 | + DirectoryList::STATIC_VIEW |
| 115 | + ], |
| 116 | + self::PERMISSIONS_DIR, |
| 117 | + self::PERMISSIONS_DIR |
| 118 | + ); |
| 119 | + |
| 120 | + // Trigger static assets compilation and deployment |
| 121 | + $this->deployStaticContent($output); |
| 122 | + $this->deployCss($output); |
| 123 | + // Trigger code generation |
| 124 | + $this->compile($output); |
| 125 | + $this->lockStaticResources(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Deploy CSS |
| 130 | + * |
| 131 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 132 | + * @return void |
| 133 | + */ |
| 134 | + protected function deployCss( |
| 135 | + \Symfony\Component\Console\Output\OutputInterface $output |
| 136 | + ) { |
| 137 | + $themeLocalePairs = $this->storeView->retrieveThemeLocalePairs(); |
| 138 | + foreach ($themeLocalePairs as $themeLocalePair) { |
| 139 | + $theme = $themeLocalePair['theme'] ?: self::DEFAULT_THEME; |
| 140 | + $cmd = $this->functionCallPath . 'dev:css:deploy less' |
| 141 | + . ' --' . CssDeployCommand::THEME_OPTION . '="' . $theme . '"' |
| 142 | + . ' --' . CssDeployCommand::LOCALE_OPTION . '="' . $themeLocalePair['locale'] . '"'; |
| 143 | + |
| 144 | + /** |
| 145 | + * @todo build a solution that does not depend on exec |
| 146 | + */ |
| 147 | + $execOutput = $this->shell->execute($cmd); |
| 148 | + $output->writeln($execOutput); |
| 149 | + } |
| 150 | + $output->writeln('CSS deployment complete'); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Deploy static content |
| 155 | + * |
| 156 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 157 | + * @return void |
| 158 | + * @throws \Exception |
| 159 | + */ |
| 160 | + protected function deployStaticContent( |
| 161 | + \Symfony\Component\Console\Output\OutputInterface $output |
| 162 | + ) { |
| 163 | + $output->writeln('Static content deployment start'); |
| 164 | + $cmd = $this->functionCallPath . 'setup:static-content:deploy ' |
| 165 | + . implode(' ', $this->storeView->retrieveLocales()); |
| 166 | + |
| 167 | + /** |
| 168 | + * @todo build a solution that does not depend on exec |
| 169 | + */ |
| 170 | + $execOutput = $this->shell->execute($cmd); |
| 171 | + $output->writeln($execOutput); |
| 172 | + $output->writeln('Static content deployment complete'); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Runs code multi-tenant compiler to generate code and DI information |
| 177 | + * |
| 178 | + * @param \Symfony\Component\Console\Output\OutputInterface $output |
| 179 | + * @return void |
| 180 | + */ |
| 181 | + protected function compile( |
| 182 | + \Symfony\Component\Console\Output\OutputInterface $output |
| 183 | + ) { |
| 184 | + $output->writeln('Start compilation'); |
| 185 | + $this->cleanupFilesystem( |
| 186 | + [ |
| 187 | + DirectoryList::CACHE, |
| 188 | + DirectoryList::GENERATION, |
| 189 | + DirectoryList::DI, |
| 190 | + ] |
| 191 | + ); |
| 192 | + $cmd = $this->functionCallPath . 'setup:di:compile-multi-tenant'; |
| 193 | + |
| 194 | + /** |
| 195 | + * exec command is necessary for now to isolate the autoloaders in the compiler from the memory state |
| 196 | + * of this process, which would prevent some classes from being generated |
| 197 | + * |
| 198 | + * @todo build a solution that does not depend on exec |
| 199 | + */ |
| 200 | + $execOutput = $this->shell->execute($cmd); |
| 201 | + $output->writeln($execOutput); |
| 202 | + $output->writeln('Compilation complete'); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Deletes specified directories by code |
| 207 | + * |
| 208 | + * @param array $directoryCodeList |
| 209 | + * @return void |
| 210 | + */ |
| 211 | + public function cleanupFilesystem($directoryCodeList) |
| 212 | + { |
| 213 | + $excludePatterns = ['#.htaccess#', '#deployed_version.txt#']; |
| 214 | + foreach ($directoryCodeList as $code) { |
| 215 | + if ($code == DirectoryList::STATIC_VIEW) { |
| 216 | + $directoryPath = $this->directoryList->getPath(DirectoryList::STATIC_VIEW); |
| 217 | + if ($this->driverFile->isExists($directoryPath)) { |
| 218 | + $files = $this->driverFile->readDirectory($directoryPath); |
| 219 | + foreach ($files as $file) { |
| 220 | + foreach ($excludePatterns as $pattern) { |
| 221 | + if (preg_match($pattern, $file)) { |
| 222 | + continue 2; |
| 223 | + } |
| 224 | + } |
| 225 | + if ($this->driverFile->isFile($file)) { |
| 226 | + $this->driverFile->deleteFile($file); |
| 227 | + } else { |
| 228 | + $this->driverFile->deleteDirectory($file); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } else { |
| 233 | + $this->filesystem->getDirectoryWrite($code) |
| 234 | + ->delete(); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Change permissions for directories by their code |
| 241 | + * |
| 242 | + * @param array $directoryCodeList |
| 243 | + * @param int $dirPermissions |
| 244 | + * @param int $filePermissions |
| 245 | + * @return void |
| 246 | + */ |
| 247 | + protected function changePermissions($directoryCodeList, $dirPermissions, $filePermissions) |
| 248 | + { |
| 249 | + foreach ($directoryCodeList as $code) { |
| 250 | + $directoryPath = $this->directoryList->getPath($code); |
| 251 | + if ($this->driverFile->isExists($directoryPath)) { |
| 252 | + $this->filesystem->getDirectoryWrite($code) |
| 253 | + ->changePermissionsRecursively('', $dirPermissions, $filePermissions); |
| 254 | + } else { |
| 255 | + $this->driverFile->createDirectory($directoryPath, $dirPermissions); |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Chenge permissions on static resources |
| 262 | + * |
| 263 | + * @return void |
| 264 | + */ |
| 265 | + public function lockStaticResources() |
| 266 | + { |
| 267 | + // Lock /var/generation, /var/di/ and /var/view_preprocessed directories |
| 268 | + $this->changePermissions( |
| 269 | + [ |
| 270 | + DirectoryList::GENERATION, |
| 271 | + DirectoryList::DI, |
| 272 | + DirectoryList::TMP_MATERIALIZATION_DIR, |
| 273 | + ], |
| 274 | + self::PERMISSIONS_DIR, |
| 275 | + self::PERMISSIONS_FILE |
| 276 | + ); |
| 277 | + } |
| 278 | +} |
0 commit comments