Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeployPackage.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy\Service;
7 
11 use Magento\Framework\Locale\ResolverInterface as LocaleResolver;
14 use Psr\Log\LoggerInterface;
15 
20 {
28  private $appState;
29 
37  private $localeResolver;
38 
44  private $deployStaticFile;
45 
51  private $logger;
52 
58  private $count = 0;
59 
65  private $errorsCount = 0;
66 
75  public function __construct(
76  AppState $appState,
77  LocaleResolver $localeResolver,
78  DeployStaticFile $deployStaticFile,
79  LoggerInterface $logger
80  ) {
81  $this->appState = $appState;
82  $this->localeResolver = $localeResolver;
83  $this->deployStaticFile = $deployStaticFile;
84  $this->logger = $logger;
85  }
86 
95  public function deploy(Package $package, array $options, $skipLogging = false)
96  {
97  $result = $this->appState->emulateAreaCode(
98  $package->getArea() === Package::BASE_AREA ? 'global' : $package->getArea(),
99  function () use ($package, $options, $skipLogging) {
100  // emulate application locale needed for correct file path resolving
101  $this->localeResolver->setLocale($package->getLocale());
102  $this->deployEmulated($package, $options, $skipLogging);
103  }
104  );
106  return $result;
107  }
108 
115  public function deployEmulated(Package $package, array $options, $skipLogging = false)
116  {
117  $this->count = 0;
118  $this->errorsCount = 0;
119  $this->register($package, null, $skipLogging);
120 
122  foreach ($package->getFiles() as $file) {
123  $fileId = $file->getDeployedFileId();
124  ++$this->count;
125  $this->register($package, $file, $skipLogging);
126  if ($this->checkFileSkip($fileId, $options)) {
127  continue;
128  }
129 
130  try {
131  $this->processFile($file, $package);
132  } catch (ContentProcessorException $exception) {
133  $errorMessage = __('Compilation from source: ')
134  . $file->getSourcePath()
135  . PHP_EOL . $exception->getMessage();
136  $this->errorsCount++;
137  $this->logger->critical($errorMessage);
138  } catch (\Exception $exception) {
139  $this->logger->critical($exception->getTraceAsString());
140  $this->errorsCount++;
141  }
142  }
143 
144  // execute package post-processors (may adjust content of deployed files, or produce derivative files)
145  foreach ($package->getPostProcessors() as $processor) {
146  $processor->process($package, $options);
147  }
148 
149  return true;
150  }
151 
161  private function processFile(PackageFile $file, Package $package)
162  {
163  if ($file->getContent()) {
164  $this->deployStaticFile->writeFile(
165  $file->getDeployedFileName(),
166  $package->getPath(),
167  $file->getContent()
168  );
169  } else {
170  $parentPackage = $package->getParent();
171  if ($this->checkIfCanCopy($file, $package, $parentPackage)) {
172  $this->deployStaticFile->copyFile(
173  $file->getDeployedFileId(),
174  $parentPackage->getPath(),
175  $package->getPath()
176  );
177  } else {
178  $this->deployStaticFile->deployFile(
179  $file->getFileName(),
180  [
181  'area' => $package->getArea(),
182  'theme' => $package->getTheme(),
183  'locale' => $package->getLocale(),
184  'module' => $file->getModule(),
185  ]
186  );
187  }
188  }
189  }
190 
199  private function checkIfCanCopy(PackageFile $file, Package $package, Package $parentPackage = null)
200  {
201  return $parentPackage
202  && $file->getOrigPackage() !== $package
203  && (
204  $file->getArea() !== $package->getArea()
205  || $file->getTheme() !== $package->getTheme()
206  || $file->getLocale() !== $package->getLocale()
207  )
208  && $file->getOrigPackage() === $parentPackage
209  && $this->deployStaticFile->readFile($file->getDeployedFileId(), $parentPackage->getPath());
210  }
211 
219  private function checkFileSkip($filePath, array $options)
220  {
221  if ($filePath !== '.') {
222  $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
223  $basename = pathinfo($filePath, PATHINFO_BASENAME);
224  if ($ext === 'less' && strpos($basename, '_') === 0) {
225  return true;
226  }
229  : null;
230  return $option ? (isset($options[$option]) ? $options[$option] : false) : false;
231  }
232  return false;
233  }
234 
243  private function register(Package $package, PackageFile $file = null, $skipLogging = false)
244  {
245  $logMessage = '.';
246  if ($file) {
247  $logMessage = "Processing file '{$file->getSourcePath()}'";
248  if ($file->getArea()) {
249  $logMessage .= " for area '{$file->getArea()}'";
250  }
251  if ($file->getTheme()) {
252  $logMessage .= ", theme '{$file->getTheme()}'";
253  }
254  if ($file->getLocale()) {
255  $logMessage .= ", locale '{$file->getLocale()}'";
256  }
257  if ($file->getModule()) {
258  $logMessage .= "module '{$file->getModule()}'";
259  }
260  }
261 
262  $info = [
263  'count' => $this->count,
264  'last' => $file ? $file->getSourcePath() : ''
265  ];
266  $this->deployStaticFile->writeTmpFile('info.json', $package->getPath(), json_encode($info));
267 
268  if (!$skipLogging) {
269  $this->logger->info($logMessage);
270  }
271  }
272 }
$processor
Definition: 404.php:10
__()
Definition: __.php:13
$logger
deploy(Package $package, array $options, $skipLogging=false)
__construct(AppState $appState, LocaleResolver $localeResolver, DeployStaticFile $deployStaticFile, LoggerInterface $logger)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52