SourceThemeDeployCommand.php 5.4 KB

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