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\CacheTool; |
17
|
|
|
use CacheTool\Command as CacheToolCommand; |
18
|
|
|
use CacheTool\Monolog\ConsoleHandler; |
19
|
|
|
use Monolog\Logger; |
20
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
21
|
|
|
use Symfony\Component\Console\Command\Command; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
26
|
|
|
use Symfony\Component\DependencyInjection\Container; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
28
|
|
|
|
29
|
|
|
class Application extends BaseApplication |
30
|
|
|
{ |
31
|
|
|
const VERSION = '@package_version@'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Config |
35
|
|
|
*/ |
36
|
|
|
protected $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Logger |
40
|
|
|
*/ |
41
|
|
|
protected $logger; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Config $config |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Config $config) |
47
|
|
|
{ |
48
|
|
|
parent::__construct('CacheTool', self::VERSION); |
49
|
|
|
|
50
|
|
|
$this->config = $config; |
51
|
|
|
$this->logger = new Logger('cachetool'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function getDefaultCommands() |
58
|
|
|
{ |
59
|
|
|
$commands = parent::getDefaultCommands(); |
60
|
|
|
$commands[] = new CacheToolCommand\SelfUpdateCommand(); |
61
|
|
|
|
62
|
|
|
$commands[] = new CacheToolCommand\ApcBinDumpCommand(); |
63
|
|
|
$commands[] = new CacheToolCommand\ApcBinLoadCommand(); |
64
|
|
|
$commands[] = new CacheToolCommand\ApcCacheClearCommand(); |
65
|
|
|
$commands[] = new CacheToolCommand\ApcCacheInfoCommand(); |
66
|
|
|
$commands[] = new CacheToolCommand\ApcCacheInfoFileCommand(); |
67
|
|
|
$commands[] = new CacheToolCommand\ApcKeyDeleteCommand(); |
68
|
|
|
$commands[] = new CacheToolCommand\ApcKeyExistsCommand(); |
69
|
|
|
$commands[] = new CacheToolCommand\ApcKeyFetchCommand(); |
70
|
|
|
$commands[] = new CacheToolCommand\ApcKeyStoreCommand(); |
71
|
|
|
$commands[] = new CacheToolCommand\ApcSmaInfoCommand(); |
72
|
|
|
$commands[] = new CacheToolCommand\ApcRegexpDeleteCommand(); |
73
|
|
|
|
74
|
|
|
$commands[] = new CacheToolCommand\ApcuCacheClearCommand(); |
75
|
|
|
$commands[] = new CacheToolCommand\ApcuCacheInfoCommand(); |
76
|
|
|
$commands[] = new CacheToolCommand\ApcuCacheInfoKeysCommand(); |
77
|
|
|
$commands[] = new CacheToolCommand\ApcuKeyDeleteCommand(); |
78
|
|
|
$commands[] = new CacheToolCommand\ApcuKeyExistsCommand(); |
79
|
|
|
$commands[] = new CacheToolCommand\ApcuKeyFetchCommand(); |
80
|
|
|
$commands[] = new CacheToolCommand\ApcuKeyStoreCommand(); |
81
|
|
|
$commands[] = new CacheToolCommand\ApcuSmaInfoCommand(); |
82
|
|
|
$commands[] = new CacheToolCommand\ApcuRegexpDeleteCommand(); |
83
|
|
|
|
84
|
|
|
$commands[] = new CacheToolCommand\OpcacheConfigurationCommand(); |
85
|
|
|
$commands[] = new CacheToolCommand\OpcacheResetCommand(); |
86
|
|
|
$commands[] = new CacheToolCommand\OpcacheStatusCommand(); |
87
|
|
|
$commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand(); |
88
|
|
|
$commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand(); |
89
|
|
|
|
90
|
|
|
$commands[] = new CacheToolCommand\StatCacheClearCommand(); |
91
|
|
|
$commands[] = new CacheToolCommand\StatRealpathGetCommand(); |
92
|
|
|
$commands[] = new CacheToolCommand\StatRealpathSizeCommand(); |
93
|
|
|
|
94
|
|
|
return $commands; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
protected function getDefaultInputDefinition() |
101
|
|
|
{ |
102
|
|
|
$definition = parent::getDefaultInputDefinition(); |
103
|
|
|
$definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.')); |
104
|
|
|
$definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli')); |
105
|
|
|
$definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to')); |
106
|
|
|
|
107
|
|
|
return $definition; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
114
|
|
|
{ |
115
|
|
|
$handler = new ConsoleHandler(); |
116
|
|
|
$handler->setOutput($output); |
117
|
|
|
$this->logger->pushHandler($handler); |
118
|
|
|
|
119
|
|
|
$exitCode = parent::doRun($input, $output); |
120
|
|
|
|
121
|
|
|
$handler->close(); |
122
|
|
|
|
123
|
|
|
return $exitCode; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
|
|
public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
130
|
|
|
{ |
131
|
|
|
if ($command instanceof ContainerAwareInterface) { |
132
|
|
|
$container = $this->buildContainer($input); |
133
|
|
|
$command->setContainer($container); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return parent::doRunCommand($command, $input, $output); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param InputInterface $input |
141
|
|
|
* @return ContainerInterface |
142
|
|
|
*/ |
143
|
|
|
public function buildContainer(InputInterface $input) |
144
|
|
|
{ |
145
|
|
|
if ($input->hasParameterOption('--cli')) { |
146
|
|
|
$this->config['adapter'] = 'cli'; |
147
|
|
|
} elseif ($input->hasParameterOption('--fcgi')) { |
148
|
|
|
$this->config['adapter'] = 'fastcgi'; |
149
|
|
|
$this->config['fastcgi'] = $input->getParameterOption('--fcgi'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) { |
153
|
|
|
$this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
switch ($this->config['adapter']) { |
157
|
|
|
case 'cli': |
158
|
|
|
$adapter = new Cli(); |
159
|
|
|
break; |
160
|
|
|
|
161
|
|
|
case 'fastcgi': |
162
|
|
|
$adapter = new FastCGI($this->config['fastcgi']); |
163
|
|
|
break; |
164
|
|
|
|
165
|
|
|
default: |
166
|
|
|
throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli or fastcgi"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger); |
170
|
|
|
$container = new Container(); |
171
|
|
|
$container->set('cachetool', $cacheTool); |
172
|
|
|
$container->set('logger', $this->logger); |
173
|
|
|
|
174
|
|
|
return $container; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.