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\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class ApcRegexpDeleteCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
->setName('apc:regexp:delete') |
27
|
|
|
->setDescription('Deletes all APC key matching a regexp') |
28
|
|
|
->addArgument('regexp', InputArgument::REQUIRED) |
29
|
|
|
->setHelp(''); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$this->ensureExtensionLoaded('apc'); |
38
|
|
|
|
39
|
|
|
$regexp = $input->getArgument('regexp'); |
40
|
|
|
|
41
|
|
|
$user = $this->getCacheTool()->apc_cache_info('user'); |
42
|
|
|
|
43
|
|
|
$keys = array(); |
44
|
|
|
foreach ($user['cache_list'] as $key) { |
45
|
|
|
$string = $key['info']; |
46
|
|
|
if (preg_match('|' . $regexp . '|', $string)) { |
47
|
|
|
$keys[] = $key; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$cpt = 0; |
51
|
|
|
$table = $this->getHelper('table'); |
52
|
|
|
$table->setHeaders(array('Key', 'TTL', )); |
53
|
|
|
$table->setRows($keys); |
54
|
|
|
$table->render($output); |
55
|
|
|
foreach ($keys as $key) { |
56
|
|
|
$success = $this->getCacheTool()->apc_delete($key['info']); |
57
|
|
|
if ($output->isVerbose()) { |
|
|
|
|
58
|
|
|
if ($success) { |
59
|
|
|
$output->writeln("<comment>APC key <info>{$key['info']}</info> was deleted</comment>"); |
60
|
|
|
} else { |
61
|
|
|
$output->writeln("<comment>APC key <info>{$key['info']}</info> could not be deleted.</comment>"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$cpt ++; |
65
|
|
|
} |
66
|
|
|
if ($output->isVerbose()) { |
|
|
|
|
67
|
|
|
$output->writeln("<comment>APC key <info>{$cpt}</info> keys treated.</comment>"); |
68
|
|
|
} |
69
|
|
|
return 1; |
70
|
|
|
} |
71
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: