|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of CacheTool. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CacheTool\Console; |
|
13
|
|
|
|
|
14
|
|
|
use CacheTool\Adapter\FastCGI; |
|
15
|
|
|
use CacheTool\Adapter\Cli; |
|
16
|
|
|
use CacheTool\Adapter\Http\FileGetContents; |
|
17
|
|
|
use CacheTool\Adapter\Http\SymfonyHttpClient; |
|
18
|
|
|
use CacheTool\Adapter\Web; |
|
19
|
|
|
use CacheTool\CacheTool; |
|
20
|
|
|
use CacheTool\Command as CacheToolCommand; |
|
21
|
|
|
use CacheTool\Monolog\ConsoleHandler; |
|
22
|
|
|
use Monolog\Logger; |
|
23
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
24
|
|
|
use Symfony\Component\Console\Command\Command; |
|
25
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
28
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
29
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
30
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
31
|
|
|
use Symfony\Component\Yaml\Parser; |
|
32
|
|
|
|
|
33
|
|
|
class Application extends BaseApplication |
|
34
|
|
|
{ |
|
35
|
|
|
const VERSION = '@package_version@'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Config |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $config; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Logger |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $logger; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Config $config |
|
49
|
|
|
*/ |
|
50
|
25 |
|
public function __construct(Config $config) |
|
51
|
|
|
{ |
|
52
|
25 |
|
parent::__construct('CacheTool', self::VERSION); |
|
53
|
|
|
|
|
54
|
25 |
|
$this->config = $config; |
|
55
|
25 |
|
$this->logger = new Logger('cachetool'); |
|
56
|
25 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
25 |
|
protected function getDefaultCommands() |
|
62
|
|
|
{ |
|
63
|
25 |
|
$commands = parent::getDefaultCommands(); |
|
64
|
25 |
|
$commands[] = new CacheToolCommand\SelfUpdateCommand( |
|
65
|
25 |
|
'gordalina/cachetool', |
|
66
|
25 |
|
'@package_version@', |
|
67
|
25 |
|
'gordalina/cachetool' |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
25 |
|
if (in_array('apcu', $this->config['extensions'], true)) { |
|
|
|
|
|
|
71
|
24 |
|
$commands[] = new CacheToolCommand\ApcuCacheClearCommand(); |
|
72
|
24 |
|
$commands[] = new CacheToolCommand\ApcuCacheInfoCommand(); |
|
73
|
24 |
|
$commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand(); |
|
74
|
24 |
|
$commands[] = new CacheToolCommand\ApcuKeyDeleteCommand(); |
|
75
|
24 |
|
$commands[] = new CacheToolCommand\ApcuKeyExistsCommand(); |
|
76
|
24 |
|
$commands[] = new CacheToolCommand\ApcuKeyFetchCommand(); |
|
77
|
24 |
|
$commands[] = new CacheToolCommand\ApcuKeyStoreCommand(); |
|
78
|
24 |
|
$commands[] = new CacheToolCommand\ApcuSmaInfoCommand(); |
|
79
|
24 |
|
$commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
25 |
|
if (in_array('opcache', $this->config['extensions'], true)) { |
|
83
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheConfigurationCommand(); |
|
84
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheResetCommand(); |
|
85
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand(); |
|
86
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheStatusCommand(); |
|
87
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand(); |
|
88
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand(); |
|
89
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand(); |
|
90
|
24 |
|
$commands[] = new CacheToolCommand\OpcacheCompileScriptCommand(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
25 |
|
$commands[] = new CacheToolCommand\PhpEvalCommand(); |
|
94
|
25 |
|
$commands[] = new CacheToolCommand\StatCacheClearCommand(); |
|
95
|
25 |
|
$commands[] = new CacheToolCommand\StatRealpathGetCommand(); |
|
96
|
25 |
|
$commands[] = new CacheToolCommand\StatRealpathSizeCommand(); |
|
97
|
|
|
|
|
98
|
25 |
|
return $commands; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritDoc} |
|
103
|
|
|
*/ |
|
104
|
25 |
|
protected function getDefaultInputDefinition() |
|
105
|
|
|
{ |
|
106
|
25 |
|
$definition = parent::getDefaultInputDefinition(); |
|
107
|
25 |
|
$definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.')); |
|
108
|
25 |
|
$definition->addOption(new InputOption('--fcgi-chroot', null, InputOption::VALUE_OPTIONAL, 'If specified, used for mapping script path to chrooted FastCGI server. --tmp-dir need to be chrooted too.')); |
|
109
|
25 |
|
$definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli')); |
|
110
|
25 |
|
$definition->addOption(new InputOption('--web', null, InputOption::VALUE_OPTIONAL, 'If specified, uses web adapter, defaults to FileGetContents. Available adapters are: FileGetContents and SymfonyHttpClient', 'FileGetContents')); |
|
111
|
25 |
|
$definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter')); |
|
112
|
25 |
|
$definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter')); |
|
113
|
25 |
|
$definition->addOption(new InputOption('--web-allow-insecure', null, InputOption::VALUE_OPTIONAL, 'If specified, verify_peer and verify_host are disabled (only for SymfonyHttpClient)')); |
|
114
|
25 |
|
$definition->addOption(new InputOption('--web-basic-auth', null, InputOption::VALUE_OPTIONAL, 'If specified, used for basic authorization (only for SymfonyHttpClient)')); |
|
115
|
25 |
|
$definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to')); |
|
116
|
25 |
|
$definition->addOption(new InputOption('--config', '-c', InputOption::VALUE_REQUIRED, 'If specified use this yaml configuration file')); |
|
117
|
25 |
|
return $definition; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritDoc} |
|
122
|
|
|
*/ |
|
123
|
25 |
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
124
|
|
|
{ |
|
125
|
25 |
|
$handler = new ConsoleHandler(); |
|
126
|
25 |
|
$handler->setOutput($output); |
|
127
|
25 |
|
$this->logger->pushHandler($handler); |
|
128
|
|
|
|
|
129
|
25 |
|
$exitCode = parent::doRun($input, $output); |
|
130
|
|
|
|
|
131
|
22 |
|
$handler->close(); |
|
132
|
|
|
|
|
133
|
22 |
|
return $exitCode; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritDoc} |
|
138
|
|
|
*/ |
|
139
|
25 |
|
public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
|
140
|
|
|
{ |
|
141
|
25 |
|
if ($command instanceof ContainerAwareInterface) { |
|
142
|
23 |
|
$container = $this->buildContainer($input); |
|
143
|
22 |
|
$command->setContainer($container); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
24 |
|
return parent::doRunCommand($command, $input, $output); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param InputInterface $input |
|
151
|
|
|
* @return ContainerInterface |
|
152
|
|
|
*/ |
|
153
|
23 |
|
public function buildContainer(InputInterface $input) |
|
154
|
|
|
{ |
|
155
|
23 |
|
$this->parseConfiguration($input); |
|
156
|
|
|
|
|
157
|
23 |
|
$this->logger->info(sprintf('CacheTool %s', self::VERSION)); |
|
158
|
23 |
|
$this->logger->debug(sprintf('Config: %s', $this->config->toJSON())); |
|
159
|
|
|
|
|
160
|
23 |
|
$cacheTool = CacheTool::factory( |
|
161
|
23 |
|
$this->getAdapter(), |
|
162
|
22 |
|
$this->config['temp_dir'], |
|
163
|
22 |
|
$this->logger |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
22 |
|
$container = new Container(); |
|
167
|
22 |
|
$container->set('cachetool', $cacheTool); |
|
168
|
22 |
|
$container->set('logger', $this->logger); |
|
169
|
|
|
|
|
170
|
22 |
|
return $container; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param InputInterface $input |
|
175
|
|
|
*/ |
|
176
|
23 |
|
private function parseConfiguration(InputInterface $input) |
|
177
|
|
|
{ |
|
178
|
23 |
|
if ($input->hasParameterOption('--config')) { |
|
179
|
2 |
|
$path = $input->getParameterOption('--config'); |
|
180
|
|
|
|
|
181
|
2 |
|
if (!is_file($path)) { |
|
182
|
|
|
throw new \RuntimeException("Could not read configuration file: {$path}"); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
2 |
|
$this->config = Config::fromFile($path); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
23 |
|
if ($input->hasParameterOption('--cli')) { |
|
189
|
1 |
|
$this->config['adapter'] = 'cli'; |
|
190
|
22 |
|
} elseif ($input->hasParameterOption('--fcgi')) { |
|
191
|
1 |
|
$this->config['adapter'] = 'fastcgi'; |
|
192
|
1 |
|
$this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot'); |
|
193
|
|
|
|
|
194
|
1 |
|
if (!is_null($input->getParameterOption('--fcgi'))) { |
|
195
|
1 |
|
$this->config['fastcgi'] = $input->getParameterOption('--fcgi'); |
|
196
|
|
|
} |
|
197
|
21 |
|
} elseif ($input->hasParameterOption('--web')) { |
|
198
|
3 |
|
$this->config['adapter'] = 'web'; |
|
199
|
3 |
|
$this->config['webClient'] = $input->getParameterOption('--web') ?? 'FileGetContents'; |
|
200
|
3 |
|
$this->config['webPath'] = $input->getParameterOption('--web-path'); |
|
201
|
3 |
|
$this->config['webUrl'] = $input->getParameterOption('--web-url'); |
|
202
|
3 |
|
if($this->config['webClient'] === 'SymfonyHttpClient') { |
|
203
|
1 |
|
if ($input->hasParameterOption('--web-allow-insecure')) { |
|
204
|
|
|
$this->config['webAllowInsecure'] = true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
if ($input->hasParameterOption('--web-basic-auth')) { |
|
208
|
|
|
$this->config['webBasicAuth'] = $input->getParameterOption('--web-basic-auth'); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
23 |
|
if ($this->config['adapter'] === 'web') { |
|
214
|
4 |
|
switch ($this->config['webClient']) { |
|
215
|
4 |
|
case 'FileGetContents': |
|
216
|
2 |
|
$this->config['http'] = new FileGetContents($this->config['webUrl']); |
|
217
|
2 |
|
break; |
|
218
|
|
|
|
|
219
|
2 |
|
case 'SymfonyHttpClient': |
|
220
|
|
|
|
|
221
|
2 |
|
$symfonyHttpClientConfig = []; |
|
222
|
|
|
|
|
223
|
2 |
|
if ($this->config['webAllowInsecure']) { |
|
224
|
|
|
$symfonyHttpClientConfig['verify_peer'] = false; |
|
225
|
|
|
$symfonyHttpClientConfig['verify_host'] = false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
2 |
|
if ($this->config['webBasicAuth']) { |
|
229
|
1 |
|
$symfonyHttpClientConfig['auth_basic'] = $this->config['webBasicAuth']; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
2 |
|
$this->config['http'] = new SymfonyHttpClient($this->config['webUrl'], $symfonyHttpClientConfig); |
|
233
|
2 |
|
break; |
|
234
|
|
|
|
|
235
|
|
|
default: |
|
236
|
|
|
throw new \RuntimeException("{$this->config["web"]} is not a valid adapter. Possible adapters: FileGetContents or SymfonyHttpClient"); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
23 |
|
if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) { |
|
241
|
1 |
|
$this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t'); |
|
242
|
|
|
} |
|
243
|
23 |
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @return null|\CacheTool\Adapter\AbstractAdapter |
|
247
|
|
|
*/ |
|
248
|
23 |
|
private function getAdapter() |
|
249
|
|
|
{ |
|
250
|
23 |
|
switch ($this->config['adapter']) { |
|
251
|
23 |
|
case 'cli': |
|
252
|
15 |
|
return new Cli(); |
|
253
|
8 |
|
case 'fastcgi': |
|
254
|
3 |
|
return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']); |
|
255
|
5 |
|
case 'web': |
|
256
|
4 |
|
return new Web($this->config['webPath'], $this->config['http']); |
|
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
1 |
|
throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web"); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|