Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SourceThemeDeployCommand.php
Go to the documentation of this file.
1 <?php
7 
11 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
17 
23 class SourceThemeDeployCommand extends Command
24 {
28  const LOCALE_OPTION = 'locale';
29 
33  const AREA_OPTION = 'area';
34 
38  const THEME_OPTION = 'theme';
39 
43  const TYPE_ARGUMENT = 'type';
44 
48  const FILE_ARGUMENT = 'file';
49 
53  private $validator;
54 
58  private $assetPublisher;
59 
63  private $assetRepository;
64 
72  public function __construct(
73  Locale $validator,
74  Publisher $assetPublisher,
75  Repository $assetRepository
76  ) {
77  parent::__construct('dev:source-theme:deploy');
78  $this->validator = $validator;
79  $this->assetPublisher = $assetPublisher;
80  $this->assetRepository = $assetRepository;
81  }
82 
86  protected function configure()
87  {
88  parent::configure();
89  $this->setDescription('Collects and publishes source files for theme.')
90  ->setDefinition(
91  [
92  new InputArgument(
93  self::FILE_ARGUMENT,
94  InputArgument::IS_ARRAY,
95  'Files to pre-process (file should be specified without extension)',
96  ['css/styles-m', 'css/styles-l']
97  ),
98  new InputOption(
99  self::TYPE_ARGUMENT,
100  null,
101  InputOption::VALUE_REQUIRED,
102  'Type of source files: [less]',
103  'less'
104  ),
105  new InputOption(
106  self::LOCALE_OPTION,
107  null,
108  InputOption::VALUE_REQUIRED,
109  'Locale: [en_US]',
110  'en_US'
111  ),
112  new InputOption(
113  self::AREA_OPTION,
114  null,
115  InputOption::VALUE_REQUIRED,
116  'Area: [frontend|adminhtml]',
117  'frontend'
118  ),
119  new InputOption(
120  self::THEME_OPTION,
121  null,
122  InputOption::VALUE_REQUIRED,
123  'Theme: [Vendor/theme]',
124  'Magento/luma'
125  ),
126 
127  ]
128  );
129  }
130 
135  protected function execute(InputInterface $input, OutputInterface $output)
136  {
137  $area = $input->getOption(self::AREA_OPTION);
138  $locale = $input->getOption(self::LOCALE_OPTION);
139  $theme = $input->getOption(self::THEME_OPTION);
140  $type = $input->getOption(self::TYPE_ARGUMENT);
141 
142  $files = $input->getArgument(self::FILE_ARGUMENT);
143 
144  if (!$this->validator->isValid($locale)) {
145  throw new \InvalidArgumentException(
146  $locale . ' argument has invalid value, please run info:language:list for list of available locales'
147  );
148  }
149 
150  if (!preg_match('#^[\w\-]+\/[\w\-]+$#', $theme)) {
151  throw new \InvalidArgumentException(
152  'Value "' . $theme . '" of the option "' . self::THEME_OPTION .
153  '" has invalid format. The format should be "Vendor/theme".'
154  );
155  }
156 
157  $message = sprintf(
158  '<info>Processed Area: %s, Locale: %s, Theme: %s, File type: %s.</info>',
159  $area,
160  $locale,
161  $theme,
162  $type
163  );
164  $output->writeln($message);
165 
166  foreach ($files as $file) {
167  $fileInfo = pathinfo($file);
168  $asset = $this->assetRepository->createAsset(
169  $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileInfo['basename'] . '.' . $type,
170  [
171  'area' => $area,
172  'theme' => $theme,
173  'locale' => $locale,
174  ]
175  );
176 
177  try {
178  $this->assetPublisher->publish($asset);
179  } catch (\Magento\Framework\View\Asset\File\NotFoundException $e) {
180  throw new \InvalidArgumentException(
181  'Verify entered values of the argument and options. ' . $e->getMessage()
182  );
183  }
184 
185  $output->writeln('<comment>-> ' . $asset->getFilePath() . '</comment>');
186  }
187 
188  $output->writeln('<info>Successfully processed.</info>');
189  }
190 }
$message
$type
Definition: item.phtml:13
$theme
__construct(Locale $validator, Publisher $assetPublisher, Repository $assetRepository)
execute(InputInterface $input, OutputInterface $output)
foreach($appDirs as $dir) $files