1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Console; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ReflectionFunction; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Steal `Illuminate\Foundation\Console\ClosureCommand`. |
13
|
|
|
*/ |
14
|
|
|
class ClosureCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The command callback. |
18
|
|
|
* |
19
|
|
|
* @var \Closure |
20
|
|
|
*/ |
21
|
|
|
protected $callback; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new command instance. |
25
|
|
|
* |
26
|
|
|
* @param string $signature |
27
|
|
|
* @param \Closure $callback |
28
|
|
|
* @return void |
|
|
|
|
29
|
|
|
*/ |
30
|
1 |
|
public function __construct($signature, Closure $callback) |
31
|
|
|
{ |
32
|
1 |
|
$this->callback = $callback; |
33
|
1 |
|
$this->signature = $signature; |
34
|
|
|
|
35
|
1 |
|
parent::__construct(); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the console command. |
40
|
|
|
* |
41
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
42
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$inputs = array_merge($input->getArguments(), $input->getOptions()); |
48
|
|
|
|
49
|
|
|
$parameters = []; |
50
|
|
|
|
51
|
|
|
foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) { |
52
|
|
|
if (isset($inputs[$parameter->name])) { |
53
|
|
|
$parameters[$parameter->name] = $inputs[$parameter->name]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->laravel->call( |
58
|
|
|
$this->callback->bindTo($this, $this), $parameters |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the description for the command. |
64
|
|
|
* |
65
|
|
|
* @param string $description |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
1 |
|
public function describe($description) |
69
|
|
|
{ |
70
|
1 |
|
$this->setDescription($description); |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.