• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gordalina / cachetool / 21418036912

27 Jan 2026 11:16PM UTC coverage: 91.631% (+1.2%) from 90.476%
21418036912

Pull #263

github

web-flow
Merge 519d4b6a4 into 732a831d5
Pull Request #263: Bump phpunit/phpunit from 9.5.25 to 9.6.33

1062 of 1159 relevant lines covered (91.63%)

124.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.02
/src/Console/Application.php
1
<?php
2

3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <samuel.gordalina@gmail.com>
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\Http\SymfonyHttpClient;
18
use CacheTool\Adapter\Web;
19
use CacheTool\CacheTool;
20
use CacheTool\Command as CacheToolCommand;
21
use CacheTool\Monolog\ConsoleHandler;
22
use Monolog\Logger;
23
use SelfUpdate\SelfUpdateCommand;
24
use Symfony\Component\Console\Application as BaseApplication;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputDefinition;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
31
use Symfony\Component\DependencyInjection\Container;
32
use Symfony\Component\DependencyInjection\ContainerInterface;
33

34
class Application extends BaseApplication
35
{
36
    const VERSION = '@package_version@';
37

38
    /**
39
     * @var Config
40
     */
41
    protected $config;
42

43
    /**
44
     * @var Logger
45
     */
46
    protected $logger;
47

48
    /**
49
     * @param Config $config
50
     */
51
    public function __construct(Config $config)
52
    {
53
        parent::__construct('CacheTool', self::VERSION);
410✔
54

55
        $this->config = $config;
410✔
56
        $this->logger = new Logger('cachetool');
410✔
57
    }
58

59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function getDefaultCommands(): array
63
    {
64
        $commands = parent::getDefaultCommands();
410✔
65
        $commands[] = new SelfUpdateCommand(
410✔
66
            'gordalina/cachetool',
410✔
67
            '@package_version@',
410✔
68
            'gordalina/cachetool'
410✔
69
        );
410✔
70

71
        if (in_array('apcu', $this->config['extensions'], true)) {
410✔
72
            $commands[] = new CacheToolCommand\ApcuCacheClearCommand();
400✔
73
            $commands[] = new CacheToolCommand\ApcuCacheInfoCommand();
400✔
74
            $commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand();
400✔
75
            $commands[] = new CacheToolCommand\ApcuKeyDeleteCommand();
400✔
76
            $commands[] = new CacheToolCommand\ApcuKeyExistsCommand();
400✔
77
            $commands[] = new CacheToolCommand\ApcuKeyFetchCommand();
400✔
78
            $commands[] = new CacheToolCommand\ApcuKeyStoreCommand();
400✔
79
            $commands[] = new CacheToolCommand\ApcuSmaInfoCommand();
400✔
80
            $commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand();
400✔
81
        }
82

83
        if (in_array('opcache', $this->config['extensions'], true)) {
410✔
84
            $commands[] = new CacheToolCommand\OpcacheConfigurationCommand();
400✔
85
            $commands[] = new CacheToolCommand\OpcacheResetCommand();
400✔
86
            $commands[] = new CacheToolCommand\OpcacheResetFileCacheCommand();
400✔
87
            $commands[] = new CacheToolCommand\OpcacheStatusCommand();
400✔
88
            $commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand();
400✔
89
            $commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand();
400✔
90
            $commands[] = new CacheToolCommand\OpcacheCompileScriptsCommand();
400✔
91
            $commands[] = new CacheToolCommand\OpcacheCompileScriptCommand();
400✔
92
        }
93

94
        $commands[] = new CacheToolCommand\PhpEvalCommand();
410✔
95
        $commands[] = new CacheToolCommand\StatCacheClearCommand();
410✔
96
        $commands[] = new CacheToolCommand\StatRealpathGetCommand();
410✔
97
        $commands[] = new CacheToolCommand\StatRealpathSizeCommand();
410✔
98

99
        return $commands;
410✔
100
    }
101

102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function getDefaultInputDefinition(): InputDefinition
106
    {
107
        $definition = parent::getDefaultInputDefinition();
410✔
108
        $definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.'));
410✔
109
        $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.'));
410✔
110
        $definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli'));
410✔
111
        $definition->addOption(new InputOption('--web', null, InputOption::VALUE_OPTIONAL, 'If specified, uses web adapter, defaults to FileGetContents. Available adapters are: FileGetContents and SymfonyHttpClient'));
410✔
112
        $definition->addOption(new InputOption('--web-path', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
410✔
113
        $definition->addOption(new InputOption('--web-url', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a information for web adapter'));
410✔
114
        $definition->addOption(new InputOption('--web-allow-insecure', null, InputOption::VALUE_NONE, 'If specified, verify_peer and verify_host are disabled (only for SymfonyHttpClient)'));
410✔
115
        $definition->addOption(new InputOption('--web-basic-auth', null, InputOption::VALUE_OPTIONAL, 'If specified, used for basic authorization (only for SymfonyHttpClient)'));
410✔
116
        $definition->addOption(new InputOption('--web-host', null, InputOption::VALUE_OPTIONAL, 'If specified, adds a Host header to web adapter request (only for SymfonyHttpClient)'));
410✔
117
        $definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to'));
410✔
118
        $definition->addOption(new InputOption('--config', '-c', InputOption::VALUE_REQUIRED, 'If specified use this yaml configuration file'));
410✔
119
        return $definition;
410✔
120
    }
121

122
    /**
123
     * {@inheritDoc}
124
     */
125
    public function doRun(InputInterface $input, OutputInterface $output)
126
    {
127
        $handler = new ConsoleHandler();
410✔
128
        $handler->setOutput($output);
410✔
129
        $this->logger->pushHandler($handler);
410✔
130

131
        $exitCode = parent::doRun($input, $output);
410✔
132

133
        $handler->close();
340✔
134

135
        return $exitCode;
340✔
136
    }
137

138
    /**
139
     * {@inheritDoc}
140
     */
141
    public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
142
    {
143
        if ($command instanceof ContainerAwareInterface) {
410✔
144
            $container = $this->buildContainer($input);
390✔
145
            $command->setContainer($container);
370✔
146
        }
147

148
        return parent::doRunCommand($command, $input, $output);
390✔
149
    }
150

151
    /**
152
     * @param  InputInterface     $input
153
     * @return ContainerInterface
154
     */
155
    public function buildContainer(InputInterface $input)
156
    {
157
        $this->parseConfiguration($input);
390✔
158

159
        $this->logger->info(sprintf('CacheTool %s', self::VERSION));
380✔
160
        $this->logger->debug(sprintf('Config: %s', $this->config->toJSON()));
380✔
161

162
        $cacheTool = CacheTool::factory(
380✔
163
            $this->getAdapter(),
380✔
164
            $this->config['temp_dir'],
380✔
165
            $this->logger
380✔
166
        );
380✔
167

168
        $container = new Container();
370✔
169
        $container->set('cachetool', $cacheTool);
370✔
170
        $container->set('logger', $this->logger);
370✔
171

172
        return $container;
370✔
173
    }
174

175
    /**
176
     * @param  InputInterface $input
177
     */
178
    private function parseConfiguration(InputInterface $input)
179
    {
180
        if ($input->getOption('config')) {
390✔
181
            $path = $input->getOption('config');
30✔
182

183
            if (!is_file($path)) {
30✔
184
                throw new \RuntimeException("Could not read configuration file: {$path}");
10✔
185
            }
186

187
            $this->config = Config::fromFile($path);
20✔
188
        }
189

190
        if ($input->getOption('cli')) {
380✔
191
            $this->config['adapter'] = 'cli';
10✔
192
        } elseif ($input->hasParameterOption('--fcgi')) {
370✔
193
            $this->config['adapter'] = 'fastcgi';
10✔
194
            $this->config['fastcgiChroot'] = $input->getOption('fcgi-chroot') ?? $this->config['fastcgiChroot'];
10✔
195
            $this->config['fastcgi'] = $input->getOption('fcgi') ?? $this->config['fastcgi'];
10✔
196
        } elseif ($input->hasParameterOption('--web')) {
360✔
197
            $this->config['adapter'] = 'web';
30✔
198
            $this->config['webClient'] = $input->getOption('web') ?? 'FileGetContents';
30✔
199
            $this->config['webPath'] = $input->getOption('web-path') ?? $this->config['webPath'];
30✔
200
            $this->config['webUrl'] = $input->getOption('web-url') ?? $this->config['webUrl'];
30✔
201

202
            if ($this->config['webClient'] === 'SymfonyHttpClient') {
30✔
203
                $this->config['webAllowInsecure'] = $input->getOption('web-allow-insecure');
10✔
204
                $this->config['webBasicAuth'] = $input->getOption('web-basic-auth') ?? $this->config['webBasicAuth'];
10✔
205
                $this->config['webHost'] = $input->getOption('web-host') ?? $this->config['webHost'];
10✔
206
            }
207
        }
208

209
        if ($this->config['adapter'] === 'web') {
380✔
210
            $this->config['http'] = $this->buildHttpClient();
40✔
211
        }
212

213
        $this->config['temp_dir'] = $input->getOption('tmp-dir') ?? $this->config['temp_dir'];
380✔
214
    }
215

216
    /**
217
     * @return \CacheTool\Adapter\HttpInterface
218
     */
219
    private function buildHttpClient()
220
    {
221
        if ($this->config['webClient'] == 'SymfonyHttpClient') {
40✔
222
            $options = [
20✔
223
                'headers' => [],
20✔
224
            ];
20✔
225

226
            if ($this->config['webAllowInsecure']) {
20✔
227
                $options['verify_peer'] = false;
×
228
                $options['verify_host'] = false;
×
229
            }
230

231
            if ($this->config['webBasicAuth']) {
20✔
232
                $options['auth_basic'] = $this->config['webBasicAuth'];
10✔
233
            }
234

235
            if (isset($this->config['webHost'])) {
20✔
236
                $options['headers']['Host'] = $this->config['webHost'];
×
237
            }
238

239
            return new SymfonyHttpClient($this->config['webUrl'], $options);
20✔
240
        }
241

242
        if ($this->config['webClient'] !== 'FileGetContents') {
20✔
243
            $this->logger->warning(sprintf(
×
244
                'Web client `%s` not supported - defaulting to FileGetContents',
×
245
                $this->config['webClient']
×
246
            ));
×
247
        }
248

249
        return new FileGetContents($this->config['webUrl']);
20✔
250
    }
251

252
    /**
253
     * @return null|\CacheTool\Adapter\AbstractAdapter
254
     */
255
    private function getAdapter()
256
    {
257
        switch ($this->config['adapter']) {
380✔
258
            case 'cli':
380✔
259
                return new Cli();
300✔
260
            case 'fastcgi':
80✔
261
                return new FastCGI($this->config['fastcgi'], $this->config['fastcgiChroot']);
30✔
262
            case 'web':
50✔
263
                return new Web($this->config['webPath'], $this->config['http']);
40✔
264
        }
265

266
        throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli, fastcgi or web");
10✔
267
    }
268
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc