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\InputOption; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class ApcBinLoadCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
->setName('apc:bin:load') |
27
|
|
|
->setDescription('Load a binary dump into the APC file and user variables') |
28
|
|
|
->addOption('--file', '-f', InputOption::VALUE_OPTIONAL, "File to read binary data from") |
29
|
|
|
->addOption('--no-verification', '-a', InputOption::VALUE_NONE, "Don't perform MD5 & CRC32 verification before loading data") |
30
|
|
|
->setHelp(''); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
|
|
$this->ensureExtensionLoaded('apc'); |
39
|
|
|
|
40
|
|
|
$file = $input->getOption('file'); |
41
|
|
|
$noVerification = $input->getOption('no-verification'); |
42
|
|
|
|
43
|
|
|
if (!$file) { |
44
|
|
|
$file = 'php://stdin'; |
45
|
|
|
} else { |
46
|
|
|
if (!is_file($file) || !is_readable($file)) { |
47
|
|
|
throw new \InvalidArgumentException(sprintf("Could not read from file: %s", $file)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$dump = file_get_contents($file); |
52
|
|
|
$flags = 0; |
53
|
|
|
|
54
|
|
|
if (!$noVerification) { |
55
|
|
|
$flags = APC_BIN_VERIFY_MD5 | APC_BIN_VERIFY_CRC32; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$success = $this->getCacheTool()->apc_bin_load($dump, $flags); |
59
|
|
|
|
60
|
|
|
if ($output->isVerbose()) { |
|
|
|
|
61
|
|
|
if ($success) { |
62
|
|
|
$output->writeln("<comment>Load was successful</comment>"); |
63
|
|
|
} else { |
64
|
|
|
$output->writeln("<comment>Load was unsuccessful</comment>"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $success ? 0 : 1; |
69
|
|
|
} |
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: