1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
9
|
|
|
use MadWeb\Initializer\Contracts\Executor as ExecutorContract; |
10
|
|
|
|
11
|
|
|
class Executor implements ExecutorContract |
12
|
|
|
{ |
13
|
|
|
protected $artisanCommand; |
14
|
|
|
|
15
|
138 |
|
public function __construct(Command $artisanCommand) |
16
|
|
|
{ |
17
|
138 |
|
$this->artisanCommand = $artisanCommand; |
18
|
138 |
|
} |
19
|
|
|
|
20
|
132 |
|
public function exec(array $commands) |
21
|
|
|
{ |
22
|
132 |
|
foreach ($commands as $command) { |
23
|
132 |
|
$this->{$command['type']}($command['command'], $command['arguments']); |
24
|
|
|
} |
25
|
132 |
|
} |
26
|
|
|
|
27
|
60 |
|
public function artisan(string $command, array $arguments = []) |
28
|
|
|
{ |
29
|
60 |
|
$this->artisanCommand->call($command, $arguments); |
30
|
60 |
|
} |
31
|
|
|
|
32
|
12 |
|
public function external(string $command, array $arguments = []) |
33
|
|
|
{ |
34
|
12 |
|
$Process = new Process(empty($arguments) ? $command : array_merge([$command], $arguments)); |
|
|
|
|
35
|
12 |
|
$Process->setTimeout(null); |
36
|
|
|
|
37
|
12 |
|
if (Process::isTtySupported()) { |
38
|
12 |
|
$Process->setTty(true); |
39
|
|
|
} elseif (Process::isPtySupported()) { |
40
|
|
|
$Process->setPty(true); |
41
|
|
|
} |
42
|
|
|
$Process->run(function ($type, $buffer) { |
43
|
|
|
if (Process::ERR === $type) { |
44
|
|
|
$this->artisanCommand->error($buffer); |
45
|
|
|
} else { |
46
|
|
|
$this->artisanCommand->line($buffer); |
47
|
|
|
} |
48
|
12 |
|
}); |
49
|
12 |
|
} |
50
|
|
|
|
51
|
6 |
|
public function callable(callable $function, array $arguments = []) |
52
|
|
|
{ |
53
|
6 |
|
call_user_func($function, ...$arguments); |
54
|
|
|
|
55
|
6 |
|
is_callable($function, false, $name); |
56
|
6 |
|
$this->artisanCommand->info("Callable: $name called"); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
48 |
|
public function dispatch($job) |
60
|
|
|
{ |
61
|
48 |
|
$this->printJob($job, Container::getInstance()->make(Dispatcher::class)->dispatch($job)); |
62
|
48 |
|
} |
63
|
|
|
|
64
|
12 |
|
public function dispatchNow($job) |
65
|
|
|
{ |
66
|
12 |
|
$this->printJob($job, Container::getInstance()->make(Dispatcher::class)->dispatchNow($job)); |
67
|
12 |
|
} |
68
|
|
|
|
69
|
54 |
|
protected function printJob($job, $result) |
70
|
|
|
{ |
71
|
54 |
|
$message = 'Job "'.get_class($job).'" has been processed'; |
72
|
|
|
|
73
|
54 |
|
$message .= is_string($result) ? '. Result: '.$result : ''; |
74
|
|
|
|
75
|
54 |
|
$this->artisanCommand->info($message); |
76
|
54 |
|
} |
77
|
|
|
} |
78
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.