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