Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeployStaticContentCommand.php
Go to the documentation of this file.
1 <?php
7 
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
21 
27 class DeployStaticContentCommand extends Command
28 {
32  const DEFAULT_LANGUAGE_VALUE = 'en_US';
33 
37  private $inputValidator;
38 
42  private $consoleLoggerFactory;
43 
47  private $options;
48 
55  private $objectManager;
56 
60  private $appState;
61 
70  public function __construct(
71  InputValidator $inputValidator,
72  ConsoleLoggerFactory $consoleLoggerFactory,
73  Options $options,
74  ObjectManagerProvider $objectManagerProvider
75  ) {
76  $this->inputValidator = $inputValidator;
77  $this->consoleLoggerFactory = $consoleLoggerFactory;
78  $this->options = $options;
79  $this->objectManager = $objectManagerProvider->get();
80 
81  parent::__construct();
82  }
83 
89  protected function configure()
90  {
91  $this->setName('setup:static-content:deploy')
92  ->setDescription('Deploys static view files')
93  ->setDefinition($this->options->getOptionsList());
94 
95  parent::configure();
96  }
97 
103  protected function execute(InputInterface $input, OutputInterface $output)
104  {
105  $time = microtime(true);
106 
107  if (!$input->getOption(Options::FORCE_RUN) && $this->getAppState()->getMode() !== State::MODE_PRODUCTION) {
108  throw new LocalizedException(
109  __(
110  'NOTE: Manual static content deployment is not required in "default" and "developer" modes.'
111  . PHP_EOL . 'In "default" and "developer" modes static contents are being deployed '
112  . 'automatically on demand.'
113  . PHP_EOL . 'If you still want to deploy in these modes, use -f option: '
114  . "'bin/magento setup:static-content:deploy -f'"
115  )
116  );
117  }
118 
119  $this->inputValidator->validate($input);
120 
121  $options = $input->getOptions();
122  $options[Options::LANGUAGE] = $input->getArgument(Options::LANGUAGES_ARGUMENT) ?: ['all'];
123  $refreshOnly = isset($options[Options::REFRESH_CONTENT_VERSION_ONLY])
124  && $options[Options::REFRESH_CONTENT_VERSION_ONLY];
125 
126  $verbose = $output->getVerbosity() > 1 ? $output->getVerbosity() : OutputInterface::VERBOSITY_VERBOSE;
127 
128  $logger = $this->consoleLoggerFactory->getLogger($output, $verbose);
129  if (!$refreshOnly) {
130  $logger->notice(PHP_EOL . "Deploy using {$options[Options::STRATEGY]} strategy");
131  }
132 
133  $this->mockCache();
134 
136  $deployService = $this->objectManager->create(DeployStaticContent::class, [
137  'logger' => $logger
138  ]);
139 
140  $deployService->deploy($options);
141 
142  if (!$refreshOnly) {
143  $logger->notice(PHP_EOL . "Execution time: " . (microtime(true) - $time));
144  }
145 
146  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
147  }
148 
154  private function mockCache()
155  {
156  $this->objectManager->configure([
157  'preferences' => [
158  Cache::class => DummyCache::class
159  ]
160  ]);
161  }
162 
166  private function getAppState()
167  {
168  if (null === $this->appState) {
169  $this->appState = $this->objectManager->get(State::class);
170  }
171  return $this->appState;
172  }
173 }
__()
Definition: __.php:13
$logger
__construct(InputValidator $inputValidator, ConsoleLoggerFactory $consoleLoggerFactory, Options $options, ObjectManagerProvider $objectManagerProvider)