/app/code/Magento/Developer/Console/Command/SourceThemeDeployCommand.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 190 lines · 122 code · 21 blank · 47 comment · 2 complexity · 3c17fcc23ffbd62ff5dfbdcf0e0be753 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Magento\Framework\App\State;
  8. use Magento\Framework\Validator\Locale;
  9. use Magento\Framework\View\Asset\Repository;
  10. use Symfony\Component\Console\Command\Command;
  11. use Magento\Framework\App\View\Asset\Publisher;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * Class SourceThemeDeployCommand
  18. *
  19. * Collects and publishes source files for theme
  20. */
  21. class SourceThemeDeployCommand extends Command
  22. {
  23. /**
  24. * Locale option key
  25. */
  26. const LOCALE_OPTION = 'locale';
  27. /**
  28. * Area option key
  29. */
  30. const AREA_OPTION = 'area';
  31. /**
  32. * Theme option key
  33. */
  34. const THEME_OPTION = 'theme';
  35. /**
  36. * Type argument key
  37. */
  38. const TYPE_ARGUMENT = 'type';
  39. /**
  40. * Files argument key
  41. */
  42. const FILE_ARGUMENT = 'file';
  43. /**
  44. * @var Locale
  45. */
  46. private $validator;
  47. /**
  48. * @var Publisher
  49. */
  50. private $assetPublisher;
  51. /**
  52. * @var Repository
  53. */
  54. private $assetRepository;
  55. /**
  56. * Constructor
  57. *
  58. * @param Locale $validator
  59. * @param Publisher $assetPublisher
  60. * @param Repository $assetRepository
  61. */
  62. public function __construct(
  63. Locale $validator,
  64. Publisher $assetPublisher,
  65. Repository $assetRepository
  66. ) {
  67. parent::__construct('dev:source-theme:deploy');
  68. $this->validator = $validator;
  69. $this->assetPublisher = $assetPublisher;
  70. $this->assetRepository = $assetRepository;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function configure()
  76. {
  77. parent::configure();
  78. $this->setDescription('Collects and publishes source files for theme.')
  79. ->setDefinition(
  80. [
  81. new InputArgument(
  82. self::FILE_ARGUMENT,
  83. InputArgument::IS_ARRAY,
  84. 'Files to pre-process (file should be specified without extension)',
  85. ['css/styles-m', 'css/styles-l']
  86. ),
  87. new InputOption(
  88. self::TYPE_ARGUMENT,
  89. null,
  90. InputOption::VALUE_REQUIRED,
  91. 'Type of source files: [less]',
  92. 'less'
  93. ),
  94. new InputOption(
  95. self::LOCALE_OPTION,
  96. null,
  97. InputOption::VALUE_REQUIRED,
  98. 'Locale: [en_US]',
  99. 'en_US'
  100. ),
  101. new InputOption(
  102. self::AREA_OPTION,
  103. null,
  104. InputOption::VALUE_REQUIRED,
  105. 'Area: [frontend|adminhtml]',
  106. 'frontend'
  107. ),
  108. new InputOption(
  109. self::THEME_OPTION,
  110. null,
  111. InputOption::VALUE_REQUIRED,
  112. 'Theme: [Vendor/theme]',
  113. 'Magento/luma'
  114. ),
  115. ]
  116. );
  117. }
  118. /**
  119. * {@inheritdoc}
  120. * @throws \InvalidArgumentException
  121. */
  122. protected function execute(InputInterface $input, OutputInterface $output)
  123. {
  124. $area = $input->getOption(self::AREA_OPTION);
  125. $locale = $input->getOption(self::LOCALE_OPTION);
  126. $theme = $input->getOption(self::THEME_OPTION);
  127. $type = $input->getOption(self::TYPE_ARGUMENT);
  128. $files = $input->getArgument(self::FILE_ARGUMENT);
  129. if (!$this->validator->isValid($locale)) {
  130. throw new \InvalidArgumentException(
  131. $locale . ' argument has invalid value, please run info:language:list for list of available locales'
  132. );
  133. }
  134. if (!preg_match('#^[\w\-]+\/[\w\-]+$#', $theme)) {
  135. throw new \InvalidArgumentException(
  136. 'Value "' . $theme . '" of the option "' . self::THEME_OPTION .
  137. '" has invalid format. The format should be "Vendor/theme".'
  138. );
  139. }
  140. $message = sprintf(
  141. '<info>Processed Area: %s, Locale: %s, Theme: %s, File type: %s.</info>',
  142. $area,
  143. $locale,
  144. $theme,
  145. $type
  146. );
  147. $output->writeln($message);
  148. foreach ($files as $file) {
  149. $fileInfo = pathinfo($file);
  150. $asset = $this->assetRepository->createAsset(
  151. $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['basename'] . '.' . $type,
  152. [
  153. 'area' => $area,
  154. 'theme' => $theme,
  155. 'locale' => $locale,
  156. ]
  157. );
  158. try {
  159. $this->assetPublisher->publish($asset);
  160. } catch (\Magento\Framework\View\Asset\File\NotFoundException $e) {
  161. throw new \InvalidArgumentException(
  162. 'Verify entered values of the argument and options. ' . $e->getMessage()
  163. );
  164. }
  165. $output->writeln('<comment>-> ' . $asset->getFilePath() . '</comment>');
  166. }
  167. $output->writeln('<info>Successfully processed.</info>');
  168. }
  169. }