1
|
|
|
<?php namespace Tequilarapido\Cli\Commands\Base; |
2
|
|
|
|
3
|
|
|
use Datum\Datum; |
4
|
|
|
use Symfony\Component\Console\Command\Command; |
5
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
|
9
|
|
|
abstract class AbstractCommand extends Command |
10
|
|
|
{ |
11
|
|
|
protected $startTime; |
12
|
|
|
protected $progress; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var InputInterface |
16
|
|
|
*/ |
17
|
|
|
protected $input; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var OutputInterface |
22
|
|
|
* We need it public, as it is used inside a closure in AbstractCommand::elapsed method |
23
|
|
|
*/ |
24
|
|
|
public $output; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function __construct($name = null) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($name); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
// Output |
35
|
|
|
$this->output = $output; |
36
|
|
|
$this->input = $input; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function table($data) |
40
|
|
|
{ |
41
|
|
|
$table = $this->getHelperSet()->get('table'); |
42
|
|
|
$table->setHeaders(array('Property', 'Value')); |
43
|
|
|
foreach ($data as $key => $value) { |
44
|
|
|
$table->addRow(array($key, $value)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$table->render($this->output); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
// |
52
|
|
|
// Time / Benchmarking stuff |
53
|
|
|
// |
54
|
|
|
protected function elapsed() |
55
|
|
|
{ |
56
|
|
|
// Start |
57
|
|
|
$this->startTime = time(); |
58
|
|
|
$self = $this; |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$this->getApplication()->getDispatcher()->addListener(ConsoleEvents::TERMINATE, function () use ($self) { |
|
|
|
|
62
|
|
|
$dt = Datum::createFromTimestamp($self->getStartTime()); |
63
|
|
|
$elapsed = time() - $self->getStartTime(); |
64
|
|
|
|
65
|
|
|
$self->output->info(''); |
|
|
|
|
66
|
|
|
$self->output->info('Took about ' . $dt->diffInMinutes() . ' min. ( ' . number_format($elapsed) . ' sec.) '); |
|
|
|
|
67
|
|
|
}); |
68
|
|
|
} catch (\Exception $e) { |
69
|
|
|
// Do nothing. |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getStartTime() |
74
|
|
|
{ |
75
|
|
|
return $this->startTime; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function iAmHungry($memory_limit = '1024M') |
79
|
|
|
{ |
80
|
|
|
ini_set('memory_limit', $memory_limit); |
81
|
|
|
set_time_limit(0); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
} |
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 sub-classes 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 parent class: