SetModeCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console\Command;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Framework\App\State;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Command to set application mode
  17. */
  18. class SetModeCommand extends Command
  19. {
  20. /**
  21. * Name of "target application mode" input argument
  22. */
  23. const MODE_ARGUMENT = 'mode';
  24. /**
  25. * Name of "skip compilation" input option
  26. */
  27. const SKIP_COMPILATION_OPTION = 'skip-compilation';
  28. /**
  29. * Object manager factory
  30. *
  31. * @var ObjectManagerInterface
  32. */
  33. private $objectManager;
  34. /**
  35. * Inject dependencies
  36. *
  37. * @param ObjectManagerInterface $objectManager
  38. */
  39. public function __construct(ObjectManagerInterface $objectManager)
  40. {
  41. $this->objectManager = $objectManager;
  42. parent::__construct();
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function configure()
  48. {
  49. $description = 'Set application mode.';
  50. $this->setName('deploy:mode:set')
  51. ->setDescription($description)
  52. ->setDefinition([
  53. new InputArgument(
  54. self::MODE_ARGUMENT,
  55. InputArgument::REQUIRED,
  56. 'The application mode to set. Available options are "developer" or "production"'
  57. ),
  58. new InputOption(
  59. self::SKIP_COMPILATION_OPTION,
  60. 's',
  61. InputOption::VALUE_NONE,
  62. 'Skips the clearing and regeneration of static content (generated code, preprocessed CSS, '
  63. . 'and assets in pub/static/)'
  64. )
  65. ]);
  66. parent::configure();
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. protected function execute(InputInterface $input, OutputInterface $output)
  72. {
  73. try {
  74. /** @var \Magento\Deploy\Model\Mode $modeController */
  75. $modeController = $this->objectManager->create(
  76. \Magento\Deploy\Model\Mode::class,
  77. [
  78. 'input' => $input,
  79. 'output' => $output,
  80. ]
  81. );
  82. $toMode = $input->getArgument(self::MODE_ARGUMENT);
  83. $skipCompilation = $input->getOption(self::SKIP_COMPILATION_OPTION);
  84. switch ($toMode) {
  85. case State::MODE_DEVELOPER:
  86. $modeController->enableDeveloperMode();
  87. break;
  88. case State::MODE_PRODUCTION:
  89. if ($skipCompilation) {
  90. $modeController->enableProductionModeMinimal();
  91. } else {
  92. $modeController->enableProductionMode();
  93. }
  94. break;
  95. case State::MODE_DEFAULT:
  96. $modeController->enableDefaultMode();
  97. break;
  98. default:
  99. throw new LocalizedException(__('The mode can\'t be switched to "%1".', $toMode));
  100. }
  101. $output->writeln('Enabled ' . $toMode . ' mode.');
  102. return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  103. } catch (\Exception $e) {
  104. $output->writeln('<error>' . $e->getMessage() . '</error>');
  105. if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
  106. $output->writeln($e->getTraceAsString());
  107. }
  108. // we must have an exit code higher than zero to indicate something was wrong
  109. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  110. }
  111. }
  112. }