|
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\OpcacheConfigurationCommand(); |
|
75
|
|
|
$commands[] = new CacheToolCommand\OpcacheResetCommand(); |
|
76
|
|
|
$commands[] = new CacheToolCommand\OpcacheStatusCommand(); |
|
77
|
|
|
$commands[] = new CacheToolCommand\OpcacheStatusScriptsCommand(); |
|
78
|
|
|
$commands[] = new CacheToolCommand\OpcacheInvalidateScriptsCommand(); |
|
79
|
|
|
|
|
80
|
|
|
$commands[] = new CacheToolCommand\StatCacheClearCommand(); |
|
81
|
|
|
$commands[] = new CacheToolCommand\StatRealpathGetCommand(); |
|
82
|
|
|
$commands[] = new CacheToolCommand\StatRealpathSizeCommand(); |
|
83
|
|
|
|
|
84
|
|
|
return $commands; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getDefaultInputDefinition() |
|
91
|
|
|
{ |
|
92
|
|
|
$definition = parent::getDefaultInputDefinition(); |
|
93
|
|
|
$definition->addOption(new InputOption('--fcgi', null, InputOption::VALUE_OPTIONAL, 'If specified, used as a connection string to FastCGI server.')); |
|
94
|
|
|
$definition->addOption(new InputOption('--cli', null, InputOption::VALUE_NONE, 'If specified, forces adapter to cli')); |
|
95
|
|
|
$definition->addOption(new InputOption('--tmp-dir', '-t', InputOption::VALUE_REQUIRED, 'Temporary directory to write files to')); |
|
96
|
|
|
|
|
97
|
|
|
return $definition; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritDoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
104
|
|
|
{ |
|
105
|
|
|
$handler = new ConsoleHandler(); |
|
106
|
|
|
$handler->setOutput($output); |
|
107
|
|
|
$this->logger->pushHandler($handler); |
|
108
|
|
|
|
|
109
|
|
|
$exitCode = parent::doRun($input, $output); |
|
110
|
|
|
|
|
111
|
|
|
$handler->close(); |
|
112
|
|
|
|
|
113
|
|
|
return $exitCode; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($command instanceof ContainerAwareInterface) { |
|
122
|
|
|
$container = $this->buildContainer($input); |
|
123
|
|
|
$command->setContainer($container); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return parent::doRunCommand($command, $input, $output); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param InputInterface $input |
|
131
|
|
|
* @return ContainerInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
public function buildContainer(InputInterface $input) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($input->hasParameterOption('--cli')) { |
|
136
|
|
|
$this->config['adapter'] = 'cli'; |
|
137
|
|
|
} else if ($input->hasParameterOption('--fcgi')) { |
|
138
|
|
|
$this->config['adapter'] = 'fastcgi'; |
|
139
|
|
|
$this->config['fastcgi'] = $input->getParameterOption('--fcgi'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ($input->hasParameterOption('--tmp-dir') || $input->hasParameterOption('-t')) { |
|
143
|
|
|
$this->config['temp_dir'] = $input->getParameterOption('--tmp-dir') ?: $input->getParameterOption('-t'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
switch ($this->config['adapter']) { |
|
147
|
|
|
case 'cli': |
|
148
|
|
|
$adapter = new Cli(); |
|
149
|
|
|
break; |
|
150
|
|
|
|
|
151
|
|
|
case 'fastcgi': |
|
152
|
|
|
$adapter = new FastCGI($this->config['fastcgi']); |
|
153
|
|
|
break; |
|
154
|
|
|
|
|
155
|
|
|
default: |
|
156
|
|
|
throw new \RuntimeException("Adapter `{$this->config['adapter']}` is not one of cli or fastcgi"); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$cacheTool = CacheTool::factory($adapter, $this->config['temp_dir'], $this->logger); |
|
160
|
|
|
$container = new Container(); |
|
161
|
|
|
$container->set('cachetool', $cacheTool); |
|
162
|
|
|
$container->set('logger', $this->logger); |
|
163
|
|
|
|
|
164
|
|
|
return $container; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.