1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BZIon\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
class Command extends ContainerAwareCommand |
12
|
|
|
{ |
13
|
|
|
private $progress; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Initialise the progress bar |
17
|
|
|
* @param OutputInterface $output The output |
18
|
|
|
* @param int $count The number of steps |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function initProgress($output, $count) |
22
|
|
|
{ |
23
|
|
|
$this->progress = new ProgressBar($output, $count); |
24
|
|
|
|
25
|
|
|
$this->progress->setBarCharacter('<comment>=</comment>'); |
26
|
|
|
$this->progress->start(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Advance the progress bar a step forward |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
protected function advance() |
34
|
|
|
{ |
35
|
|
|
if (!$this->progress) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->progress->advance(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Mark the progress bar as finished |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function finishProgress() |
47
|
|
|
{ |
48
|
|
|
if (!$this->progress) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->progress->finish(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function clearCache($output) |
56
|
|
|
{ |
57
|
|
|
$commandOutput = ($output->isVerbose()) ? $output : new NullOutput(); |
58
|
|
|
|
59
|
|
|
foreach (array('cache:clear', 'cache:warmup') as $commandName) { |
60
|
|
|
$command = $this->getApplication()->find($commandName); |
61
|
|
|
$arguments = array('command' => $commandName); |
62
|
|
|
$input = new ArrayInput($arguments); |
63
|
|
|
$command->run($input, $commandOutput); |
64
|
|
|
$this->advance(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return a function that can be used by Symfony's process to show the output |
70
|
|
|
* of a process live on our screen |
71
|
|
|
* |
72
|
|
|
* @param OutputInterface $output The console output |
73
|
|
|
* |
74
|
|
|
* @return null|\Closure |
75
|
|
|
*/ |
76
|
|
|
protected function getBufferFunction($output) |
77
|
|
|
{ |
78
|
|
|
if (!$output->isVerbose()) { |
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return function ($type, $buffer) { |
83
|
|
|
if (Process::ERR === $type) { |
84
|
|
|
echo 'ERR > ' . $buffer; |
85
|
|
|
} else { |
86
|
|
|
echo 'OUT > ' . $buffer; |
87
|
|
|
} |
88
|
|
|
}; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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: