|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Antidot\DevTools\Application\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
14
|
|
|
|
|
15
|
|
|
class SetDevelopmentMode extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
public const NAME = 'config:development-mode'; |
|
18
|
|
|
private const TRUE_OPTIONS = [ |
|
19
|
|
|
1, |
|
20
|
|
|
'1', |
|
21
|
|
|
true, |
|
22
|
|
|
'true' |
|
23
|
|
|
]; |
|
24
|
|
|
private const FALSE_OPTIONS = [ |
|
25
|
|
|
0, |
|
26
|
|
|
'0', |
|
27
|
|
|
false, |
|
28
|
|
|
'false' |
|
29
|
|
|
]; |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(array $config) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct(); |
|
35
|
|
|
$this->config = $config; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function configure(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->setName(self::NAME) |
|
41
|
|
|
->setDescription('Enables development mode.') |
|
42
|
|
|
->addOption( |
|
43
|
|
|
'disable', |
|
44
|
|
|
'dis', |
|
45
|
|
|
InputArgument::OPTIONAL, |
|
46
|
|
|
'Disables development mode.', |
|
47
|
|
|
false |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): ?int |
|
52
|
|
|
{ |
|
53
|
|
|
$needToDisableDevelopmentMode = $this->disableDevelopmentModeInputFormatter($input->getOption('disable')); |
|
54
|
|
|
if ($needToDisableDevelopmentMode) { |
|
55
|
|
|
$this->disableDevelopmentMode($output); |
|
56
|
|
|
return 0; |
|
57
|
|
|
} |
|
58
|
|
|
$this->enableDevelopmentMode($output); |
|
59
|
|
|
|
|
60
|
|
|
return 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function disableDevelopmentModeInputFormatter($getOption): bool |
|
64
|
|
|
{ |
|
65
|
|
|
if (in_array($getOption, self::TRUE_OPTIONS, true)) { |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (in_array($getOption, self::FALSE_OPTIONS, true)) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
74
|
|
|
'Invalid option %s given, dissable option value should be one of: %s, %s', |
|
75
|
|
|
(string)$getOption, |
|
76
|
|
|
implode(',', self::TRUE_OPTIONS), |
|
77
|
|
|
implode(',', self::FALSE_OPTIONS) |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function enableDevelopmentMode(OutputInterface $output): void |
|
82
|
|
|
{ |
|
83
|
|
|
$finder = new Finder(); |
|
84
|
|
|
$finder->files()->in($this->config['config_dir'])->name('*.dist'); |
|
85
|
|
|
/** @var SplFileInfo $file */ |
|
86
|
|
|
foreach ($finder as $file) { |
|
87
|
|
|
$newFile = sprintf( |
|
88
|
|
|
'%s/%s', |
|
89
|
|
|
$file->getPath(), |
|
90
|
|
|
str_replace('.dist', '', $file->getFilename()) |
|
91
|
|
|
); |
|
92
|
|
|
if (file_exists($newFile)) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
file_put_contents($newFile, $file->getContents()); |
|
97
|
|
|
$output->writeln(sprintf( |
|
98
|
|
|
'<info>Development mode config file %s was successfully created</info>', |
|
99
|
|
|
$newFile |
|
100
|
|
|
)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$command = $this->getApplication()->find(ClearConfigCache::NAME); |
|
104
|
|
|
$arguments = [ |
|
105
|
|
|
'command' => ClearConfigCache::NAME |
|
106
|
|
|
]; |
|
107
|
|
|
$command->run(new ArrayInput($arguments), $output); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function disableDevelopmentMode(OutputInterface $output): void |
|
111
|
|
|
{ |
|
112
|
|
|
$finder = new Finder(); |
|
113
|
|
|
$finder->files()->in($this->config['config_dir'])->name('*.dev.{yaml,xml,php}'); |
|
114
|
|
|
/** @var SplFileInfo $file */ |
|
115
|
|
|
foreach ($finder as $file) { |
|
116
|
|
|
$fileToRemove = sprintf( |
|
117
|
|
|
'%s/%s', |
|
118
|
|
|
$file->getPath(), |
|
119
|
|
|
$file->getFilename() |
|
120
|
|
|
); |
|
121
|
|
|
unlink($fileToRemove); |
|
122
|
|
|
$output->writeln(sprintf( |
|
123
|
|
|
'<info>Development mode config file %s was successfully removed.</info>', |
|
124
|
|
|
$fileToRemove |
|
125
|
|
|
)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|