Completed
Push — master ( 24c3a4...52b53c )
by Samuel
22:24 queued 20:47
created

Application::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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