Completed
Push — 3.x ( d8e1cb...17d627 )
by Samuel
02:18
created

Application::getAdapter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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