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\Web; |
18
|
|
|
use CacheTool\CacheTool; |
19
|
|
|
use CacheTool\Command as CacheToolCommand; |
20
|
|
|
use CacheTool\Monolog\ConsoleHandler; |
21
|
|
|
use Monolog\Logger; |
22
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
23
|
|
|
use Symfony\Component\Console\Command\Command; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
28
|
|
|
use Symfony\Component\DependencyInjection\Container; |
29
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
30
|
|
|
use Symfony\Component\Yaml\Parser; |
31
|
|
|
|
32
|
|
|
class Application extends BaseApplication |
33
|
|
|
{ |
34
|
|
|
const VERSION = '@package_version@'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Config |
38
|
|
|
*/ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Logger |
43
|
|
|
*/ |
44
|
|
|
protected $logger; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Config $config |
48
|
|
|
*/ |
49
|
21 |
|
public function __construct(Config $config) |
50
|
|
|
{ |
51
|
21 |
|
parent::__construct('CacheTool', self::VERSION); |
52
|
|
|
|
53
|
21 |
|
$this->config = $config; |
54
|
21 |
|
$this->logger = new Logger('cachetool'); |
55
|
21 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
21 |
|
protected function getDefaultCommands() |
61
|
|
|
{ |
62
|
21 |
|
$commands = parent::getDefaultCommands(); |
63
|
21 |
|
$commands[] = new CacheToolCommand\SelfUpdateCommand(); |
64
|
|
|
|
65
|
21 |
|
if (in_array('apcu', $this->config['extensions'], true)) { |
66
|
20 |
|
$commands[] = new CacheToolCommand\ApcuCacheClearCommand(); |
67
|
20 |
|
$commands[] = new CacheToolCommand\ApcuCacheInfoCommand(); |
68
|
20 |
|
$commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand(); |
69
|
20 |
|
$commands[] = new CacheToolCommand\ApcuKeyDeleteCommand(); |
70
|
20 |
|
$commands[] = new CacheToolCommand\ApcuKeyExistsCommand(); |
71
|
20 |
|
$commands[] = new CacheToolCommand\ApcuKeyFetchCommand(); |
72
|
20 |
|
$commands[] = new CacheToolCommand\ApcuKeyStoreCommand(); |
73
|
20 |
|
$commands[] = new CacheToolCommand\ApcuSmaInfoCommand(); |
74
|
20 |
|
$commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand(); |
75
|
|
|
} |
76
|
|
|
|
77
|
21 |
|
if (in_array('opcache', $this->config['extensions'], true)) { |
78
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheConfigurationCommand(); |
79
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheResetCommand(); |
80
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand(); |
81
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheStatusCommand(); |
82
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand(); |
83
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand(); |
84
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand(); |
85
|
20 |
|
$commands[] = new CacheToolCommand\OpcacheCompileScriptCommand(); |
86
|
|
|
} |
87
|
|
|
|
88
|
21 |
|
$commands[] = new CacheToolCommand\PhpEvalCommand(); |
89
|
21 |
|
$commands[] = new CacheToolCommand\StatCacheClearCommand(); |
90
|
21 |
|
$commands[] = new CacheToolCommand\StatRealpathGetCommand(); |
91
|
21 |
|
$commands[] = new CacheToolCommand\StatRealpathSizeCommand(); |
92
|
|
|
|
93
|
21 |
|
return $commands; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
21 |
|
protected function getDefaultInputDefinition() |
100
|
|
|
{ |
101
|
21 |
|
$definition = parent::getDefaultInputDefinition(); |
102
|
21 |
|
$definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.')); |
103
|
21 |
|
$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.')); |
104
|
21 |
|
$definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli')); |
105
|
21 |
|
$definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web')); |
106
|
21 |
|
$definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter')); |
107
|
21 |
|
$definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter')); |
108
|
21 |
|
$definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to')); |
109
|
21 |
|
$definition->addOption(new InputOption('--config', '-c', InputOption::VALUE_REQUIRED, 'If specified use this yaml configuration file')); |
110
|
|
|
|
111
|
21 |
|
return $definition; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
21 |
|
public function doRun(InputInterface $input, OutputInterface $output) |
118
|
|
|
{ |
119
|
21 |
|
$handler = new ConsoleHandler(); |
120
|
21 |
|
$handler->setOutput($output); |
121
|
21 |
|
$this->logger->pushHandler($handler); |
122
|
|
|
|
123
|
21 |
|
$exitCode = parent::doRun($input, $output); |
124
|
|
|
|
125
|
18 |
|
$handler->close(); |
126
|
|
|
|
127
|
18 |
|
return $exitCode; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
21 |
|
public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
134
|
|
|
{ |
135
|
21 |
|
if ($command instanceof ContainerAwareInterface) { |
136
|
19 |
|
$container = $this->buildContainer($input); |
137
|
18 |
|
$command->setContainer($container); |
138
|
|
|
} |
139
|
|
|
|
140
|
20 |
|
return parent::doRunCommand($command, $input, $output); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param InputInterface $input |
145
|
|
|
* @return ContainerInterface |
146
|
|
|
*/ |
147
|
19 |
|
public function buildContainer(InputInterface $input) |
148
|
|
|
{ |
149
|
19 |
|
$this->parseConfiguration($input); |
150
|
19 |
|
$adapter = $this->getAdapter(); |
151
|
|
|
|
152
|
18 |
|
$cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger); |
153
|
18 |
|
$container = new Container(); |
154
|
18 |
|
$container->set('cachetool', $cacheTool); |
155
|
18 |
|
$container->set('logger', $this->logger); |
156
|
|
|
|
157
|
18 |
|
return $container; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param InputInterface $input |
162
|
|
|
*/ |
163
|
19 |
|
private function parseConfiguration(InputInterface $input) |
164
|
|
|
{ |
165
|
19 |
|
if ($input->hasParameterOption('--config')) { |
166
|
1 |
|
$path = $input->getParameterOption('--config'); |
167
|
|
|
|
168
|
1 |
|
if (!is_file($path)) { |
169
|
|
|
throw new \RuntimeException("Could not read configuration file: {$path}"); |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
$this->config = Config::fromFile($path); |
173
|
|
|
} |
174
|
|
|
|
175
|
19 |
|
if ($input->hasParameterOption('--cli')) { |
176
|
1 |
|
$this->config['adapter'] = 'cli'; |
177
|
18 |
|
} elseif ($input->hasParameterOption('--fcgi')) { |
178
|
1 |
|
$this->config['adapter'] = 'fastcgi'; |
179
|
1 |
|
$this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot'); |
180
|
|
|
|
181
|
1 |
|
if (!is_null($input->getParameterOption('--fcgi'))) { |
182
|
1 |
|
$this->config['fastcgi'] = $input->getParameterOption('--fcgi'); |
183
|
|
|
} |
184
|
17 |
|
} elseif ($input->hasParameterOption('--web')) { |
185
|
|
|
$this->config['adapter'] = 'web'; |
186
|
|
|
$this->config['webPath'] = $input->getParameterOption('--web-path'); |
187
|
|
|
$this->config['webUrl'] = $input->getParameterOption('--web-url'); |
188
|
|
|
} |
189
|
|
|
|
190
|
19 |
|
if ($this->config['adapter'] === 'web') { |
191
|
|
|
$this->config['http'] = new FileGetContents($this->config['webUrl']); |
192
|
|
|
} |
193
|
|
|
|
194
|
19 |
|
if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) { |
195
|
1 |
|
$this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t'); |
196
|
|
|
} |
197
|
19 |
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return null|\CacheTool\Adapter\AbstractAdapter |
202
|
|
|
*/ |
203
|
19 |
|
private function getAdapter() |
204
|
|
|
{ |
205
|
19 |
|
switch ($this->config['adapter']) { |
206
|
19 |
|
case 'cli': |
207
|
15 |
|
return new Cli(); |
208
|
4 |
|
case 'fastcgi': |
209
|
3 |
|
return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']); |
210
|
1 |
|
case 'web': |
211
|
|
|
return new Web($this->config['webPath'], $this->config['http']); |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web"); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|