Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetModeCommand.php
Go to the documentation of this file.
1 <?php
8 
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 
21 class SetModeCommand extends Command
22 {
26  const MODE_ARGUMENT = 'mode';
27 
31  const SKIP_COMPILATION_OPTION = 'skip-compilation';
32 
38  private $objectManager;
39 
45  public function __construct(ObjectManagerInterface $objectManager)
46  {
47  $this->objectManager = $objectManager;
48  parent::__construct();
49  }
50 
54  protected function configure()
55  {
56  $description = 'Set application mode.';
57 
58  $this->setName('deploy:mode:set')
59  ->setDescription($description)
60  ->setDefinition([
61  new InputArgument(
62  self::MODE_ARGUMENT,
63  InputArgument::REQUIRED,
64  'The application mode to set. Available options are "developer" or "production"'
65  ),
66  new InputOption(
67  self::SKIP_COMPILATION_OPTION,
68  's',
69  InputOption::VALUE_NONE,
70  'Skips the clearing and regeneration of static content (generated code, preprocessed CSS, '
71  . 'and assets in pub/static/)'
72  )
73  ]);
74  parent::configure();
75  }
76 
80  protected function execute(InputInterface $input, OutputInterface $output)
81  {
82  try {
84  $modeController = $this->objectManager->create(
85  \Magento\Deploy\Model\Mode::class,
86  [
87  'input' => $input,
88  'output' => $output,
89  ]
90  );
91  $toMode = $input->getArgument(self::MODE_ARGUMENT);
92  $skipCompilation = $input->getOption(self::SKIP_COMPILATION_OPTION);
93  switch ($toMode) {
95  $modeController->enableDeveloperMode();
96  break;
98  if ($skipCompilation) {
99  $modeController->enableProductionModeMinimal();
100  } else {
101  $modeController->enableProductionMode();
102  }
103  break;
104  case State::MODE_DEFAULT:
105  $modeController->enableDefaultMode();
106  break;
107  default:
108  throw new LocalizedException(__('The mode can\'t be switched to "%1".', $toMode));
109  }
110  $output->writeln('Enabled ' . $toMode . ' mode.');
111 
112  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
113  } catch (\Exception $e) {
114  $output->writeln('<error>' . $e->getMessage() . '</error>');
115  if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
116  $output->writeln($e->getTraceAsString());
117  }
118  // we must have an exit code higher than zero to indicate something was wrong
119  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
120  }
121  }
122 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
__construct(ObjectManagerInterface $objectManager)