123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Deploy\Service;
- use Magento\Deploy\Package\Package;
- use Magento\Deploy\Package\PackageFile;
- use Magento\Framework\App\State as AppState;
- use Magento\Framework\Locale\ResolverInterface as LocaleResolver;
- use Magento\Framework\View\Asset\ContentProcessorException;
- use Magento\Deploy\Console\InputValidator;
- use Psr\Log\LoggerInterface;
- /**
- * Deploy package service
- */
- class DeployPackage
- {
- /**
- * Application state object
- *
- * Allows to switch between different application areas
- *
- * @var AppState
- */
- private $appState;
- /**
- * Locale resolver interface
- *
- * Check if given locale code is a valid one
- *
- * @var LocaleResolver
- */
- private $localeResolver;
- /**
- * Service for deploying static files
- *
- * @var DeployStaticFile
- */
- private $deployStaticFile;
- /**
- * Logger interface
- *
- * @var LoggerInterface
- */
- private $logger;
- /**
- * Total count of processed files
- *
- * @var int
- */
- private $count = 0;
- /**
- * Total count of the errors
- *
- * @var int
- */
- private $errorsCount = 0;
- /**
- * DeployPackage constructor
- *
- * @param AppState $appState
- * @param LocaleResolver $localeResolver
- * @param DeployStaticFile $deployStaticFile
- * @param LoggerInterface $logger
- */
- public function __construct(
- AppState $appState,
- LocaleResolver $localeResolver,
- DeployStaticFile $deployStaticFile,
- LoggerInterface $logger
- ) {
- $this->appState = $appState;
- $this->localeResolver = $localeResolver;
- $this->deployStaticFile = $deployStaticFile;
- $this->logger = $logger;
- }
- /**
- * Execute package deploy procedure
- *
- * @param Package $package
- * @param array $options
- * @param bool $skipLogging
- * @return bool true on success
- */
- public function deploy(Package $package, array $options, $skipLogging = false)
- {
- $result = $this->appState->emulateAreaCode(
- $package->getArea() === Package::BASE_AREA ? 'global' : $package->getArea(),
- function () use ($package, $options, $skipLogging) {
- // emulate application locale needed for correct file path resolving
- $this->localeResolver->setLocale($package->getLocale());
- $this->deployEmulated($package, $options, $skipLogging);
- }
- );
- $package->setState(Package::STATE_COMPLETED);
- return $result;
- }
- /**
- * @param Package $package
- * @param array $options
- * @param bool $skipLogging
- * @return bool
- */
- public function deployEmulated(Package $package, array $options, $skipLogging = false)
- {
- $this->count = 0;
- $this->errorsCount = 0;
- $this->register($package, null, $skipLogging);
- /** @var PackageFile $file */
- foreach ($package->getFiles() as $file) {
- $fileId = $file->getDeployedFileId();
- ++$this->count;
- $this->register($package, $file, $skipLogging);
- if ($this->checkFileSkip($fileId, $options)) {
- continue;
- }
- try {
- $this->processFile($file, $package);
- } catch (ContentProcessorException $exception) {
- $errorMessage = __('Compilation from source: ')
- . $file->getSourcePath()
- . PHP_EOL . $exception->getMessage();
- $this->errorsCount++;
- $this->logger->critical($errorMessage);
- } catch (\Exception $exception) {
- $this->logger->critical($exception->getTraceAsString());
- $this->errorsCount++;
- }
- }
- // execute package post-processors (may adjust content of deployed files, or produce derivative files)
- foreach ($package->getPostProcessors() as $processor) {
- $processor->process($package, $options);
- }
- return true;
- }
- /**
- * Apply proper deployment action
- *
- * File can be created if content is already provided, or copied from parent package or published
- *
- * @param PackageFile $file
- * @param Package $package
- * @return void
- */
- private function processFile(PackageFile $file, Package $package)
- {
- if ($file->getContent()) {
- $this->deployStaticFile->writeFile(
- $file->getDeployedFileName(),
- $package->getPath(),
- $file->getContent()
- );
- } else {
- $parentPackage = $package->getParent();
- if ($this->checkIfCanCopy($file, $package, $parentPackage)) {
- $this->deployStaticFile->copyFile(
- $file->getDeployedFileId(),
- $parentPackage->getPath(),
- $package->getPath()
- );
- } else {
- $this->deployStaticFile->deployFile(
- $file->getFileName(),
- [
- 'area' => $package->getArea(),
- 'theme' => $package->getTheme(),
- 'locale' => $package->getLocale(),
- 'module' => $file->getModule(),
- ]
- );
- }
- }
- }
- /**
- * Check if file can be copied from parent package
- *
- * @param PackageFile $file
- * @param Package $package
- * @param Package $parentPackage
- * @return bool
- */
- private function checkIfCanCopy(PackageFile $file, Package $package, Package $parentPackage = null)
- {
- return $parentPackage
- && $file->getOrigPackage() !== $package
- && (
- $file->getArea() !== $package->getArea()
- || $file->getTheme() !== $package->getTheme()
- || $file->getLocale() !== $package->getLocale()
- )
- && $file->getOrigPackage() === $parentPackage
- && $this->deployStaticFile->readFile($file->getDeployedFileId(), $parentPackage->getPath());
- }
- /**
- * Check if file can be deployed
- *
- * @param string $filePath
- * @param array $options
- * @return boolean
- */
- private function checkFileSkip($filePath, array $options)
- {
- if ($filePath !== '.') {
- $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
- $basename = pathinfo($filePath, PATHINFO_BASENAME);
- if ($ext === 'less' && strpos($basename, '_') === 0) {
- return true;
- }
- $option = isset(InputValidator::$fileExtensionOptionMap[$ext])
- ? InputValidator::$fileExtensionOptionMap[$ext]
- : null;
- return $option ? (isset($options[$option]) ? $options[$option] : false) : false;
- }
- return false;
- }
- /**
- * Add operation to log and package info files
- *
- * @param Package $package
- * @param PackageFile|null $file
- * @param bool $skipLogging
- * @return void
- */
- private function register(Package $package, PackageFile $file = null, $skipLogging = false)
- {
- $logMessage = '.';
- if ($file) {
- $logMessage = "Processing file '{$file->getSourcePath()}'";
- if ($file->getArea()) {
- $logMessage .= " for area '{$file->getArea()}'";
- }
- if ($file->getTheme()) {
- $logMessage .= ", theme '{$file->getTheme()}'";
- }
- if ($file->getLocale()) {
- $logMessage .= ", locale '{$file->getLocale()}'";
- }
- if ($file->getModule()) {
- $logMessage .= "module '{$file->getModule()}'";
- }
- }
- $info = [
- 'count' => $this->count,
- 'last' => $file ? $file->getSourcePath() : ''
- ];
- $this->deployStaticFile->writeTmpFile('info.json', $package->getPath(), json_encode($info));
- if (!$skipLogging) {
- $this->logger->info($logMessage);
- }
- }
- }
|