Completed
Push — master ( bff7dd...088f50 )
by Samuel
14:43 queued 05:36
created

Application::doRunCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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 20
    public function __construct(Config $config)
50
    {
51 20
        parent::__construct('CacheTool', self::VERSION);
52
53 20
        $this->config = $config;
54 20
        $this->logger = new Logger('cachetool');
55 20
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 20
    protected function getDefaultCommands()
61
    {
62 20
        $commands = parent::getDefaultCommands();
63 20
        $commands[] = new CacheToolCommand\SelfUpdateCommand();
64
65 20
        if (in_array('apcu', $this->config['extensions'], true)) {
66 19
            $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
67 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
68 19
            $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
69 19
            $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
70 19
            $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
71 19
            $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
72 19
            $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
73 19
            $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
74 19
            $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
75
        }
76
77 20
        if (in_array('opcache', $this->config['extensions'], true)) {
78 19
            $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
79 19
            $commands[] = new CacheToolCommand\OpcacheResetCommand();
80 19
            $commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand();
81 19
            $commands[] = new CacheToolCommand\OpcacheStatusCommand();
82 19
            $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
83 19
            $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
84 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand();
85 19
            $commands[] = new CacheToolCommand\OpcacheCompileScriptCommand();
86
        }
87
88 20
        $commands[] = new CacheToolCommand\PHPEvalCommand();
89 20
        $commands[] = new CacheToolCommand\PHPFileCommand();
90 20
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
91 20
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
92 20
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
93
94 20
        return $commands;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 20
    protected function getDefaultInputDefinition()
101
    {
102 20
        $definition = parent::getDefaultInputDefinition();
103 20
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
104 20
        $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.'));
105 20
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
106 20
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_NONE, 'If specified, forces adapter to web'));
107 20
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
108 20
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
109 20
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
110 20
        $definition->addOption(new InputOption('--config', '-c', InputOption::VALUE_OPTIONAL, 'If specified use this yaml config'));
111
112 20
        return $definition;
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118 20
    public function doRun(InputInterface $input, OutputInterface $output)
119
    {
120 20
        $handler = new ConsoleHandler();
121 20
        $handler->setOutput($output);
122 20
        $this->logger->pushHandler($handler);
123
124 20
        $exitCode = parent::doRun($input, $output);
125
126 17
        $handler->close();
127
128 17
        return $exitCode;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 20
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
135
    {
136 20
        if ($command instanceof ContainerAwareInterface) {
137 18
            $container = $this->buildContainer($input);
138 17
            $command->setContainer($container);
139
        }
140
141 19
        return parent::doRunCommand($command, $input, $output);
142
    }
143
144
    /**
145
     * @param  InputInterface     $input
146
     * @return ContainerInterface
147
     */
148 18
    public function buildContainer(InputInterface $input)
149
    {
150 18
        $this->parseConfiguration($input);
151 18
        $adapter = $this->getAdapter();
152
153 17
        $cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger);
154 17
        $container = new Container();
155 17
        $container->set('cachetool', $cacheTool);
156 17
        $container->set('logger', $this->logger);
157
158 17
        return $container;
159
    }
160
161
    /**
162
     * @param  InputInterface $input
163
     */
164 18
    private function parseConfiguration(InputInterface $input)
165
    {
166 18
        if ($input->hasParameterOption('--config')) {
167
            $path = $input->getParameterOption('--config');
168
            if (is_file($path)) {
169
                $yaml = new Parser();
170
                $config = $yaml->parse(file_get_contents($path));
171
                $this->config = new Config($config);
172
            }
173
        }
174
175 18
        if ($input->hasParameterOption('--cli')) {
176 1
            $this->config['adapter'] = 'cli';
177 17
        } 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 16
        } 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 18
        if ($this->config['adapter'] === 'web') {
191
            $this->config['http'] = new FileGetContents($this->config['webUrl']);
192
        }
193
194 18
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
195 1
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
196
        }
197 18
    }
198
199
200
    /**
201
     * @return null|\CacheTool\Adapter\AbstractAdapter
202
     */
203 18
    private function getAdapter()
204
    {
205 18
        switch ($this->config['adapter']) {
206 18
            case 'cli':
207 14
                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