Completed
Push — master ( 56eafc...829707 )
by Samuel
01:16
created

Application::getAdapter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4.0218
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
151 19
        $this->logger->info(sprintf('CacheTool %s', self::VERSION));
152 19
        $this->logger->debug(sprintf('Config: %s', $this->config->toJSON()));
153
154 19
        $cacheTool = CacheTool::factory(
155 19
            $this->getAdapter(),
156 18
            $this->config['temp_dir'],
157 18
            $this->logger
158
        );
159
160 18
        $container = new Container();
161 18
        $container->set('cachetool', $cacheTool);
162 18
        $container->set('logger', $this->logger);
163
164 18
        return $container;
165
    }
166
167
    /**
168
     * @param  InputInterface $input
169
     */
170 19
    private function parseConfiguration(InputInterface $input)
171
    {
172 19
        if ($input->hasParameterOption('--config')) {
173 1
            $path = $input->getParameterOption('--config');
174
175 1
            if (!is_file($path)) {
176
                throw new \RuntimeException("Could not read configuration file: {$path}");
177
            }
178
179 1
            $this->config = Config::fromFile($path);
180
        }
181
182 19
        if ($input->hasParameterOption('--cli')) {
183 1
            $this->config['adapter'] = 'cli';
184 18
        } elseif ($input->hasParameterOption('--fcgi')) {
185 1
            $this->config['adapter'] = 'fastcgi';
186 1
            $this->config['fastcgiChroot'] = $input->getParameterOption('--fcgi-chroot');
187
188 1
            if (!is_null($input->getParameterOption('--fcgi'))) {
189 1
                $this->config['fastcgi'] = $input->getParameterOption('--fcgi');
190
            }
191 17
        } elseif ($input->hasParameterOption('--web')) {
192
            $this->config['adapter'] = 'web';
193
            $this->config['webPath'] = $input->getParameterOption('--web-path');
194
            $this->config['webUrl'] = $input->getParameterOption('--web-url');
195
        }
196
197 19
        if ($this->config['adapter'] === 'web') {
198
            $this->config['http'] = new FileGetContents($this->config['webUrl']);
199
        }
200
201 19
        if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) {
202 1
            $this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t');
203
        }
204 19
    }
205
206
207
    /**
208
     * @return null|\CacheTool\Adapter\AbstractAdapter
209
     */
210 19
    private function getAdapter()
211
    {
212 19
        switch ($this->config['adapter']) {
213 19
            case 'cli':
214 15
                return new Cli();
215 4
            case 'fastcgi':
216 3
                return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']);
217 1
            case 'web':
218
                return new Web($this->config['webPath'], $this->config['http']);
219
        }
220
221 1
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
222
    }
223
}
224