1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tequilarapido\Consolify\Output; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Tequilarapido\Consolify\Trace\TraceReserved; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
abstract class DualOutputCommand extends Command |
12
|
|
|
{ |
13
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
14
|
|
|
{ |
15
|
|
|
$this->beforeRun($input); |
16
|
|
|
|
17
|
|
|
// If we run the manually with -vvv option |
18
|
|
|
// we will execute the command without exception handling |
19
|
|
|
// to be able to debug as usual. |
20
|
|
|
if ($this->isVerbose($input)) { |
21
|
|
|
return $this->effectiveRun($input, $output); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
return $this->effectiveRun($input, $output); |
26
|
|
|
} catch (Exception $e) { |
27
|
|
|
$this->handeException($e); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Override this method to be able to do some wide system setup |
33
|
|
|
* before the command runs. |
34
|
|
|
* |
35
|
|
|
* ie. Disable query loging for performance response (DB::disableQueryLog()) |
36
|
|
|
* |
37
|
|
|
* @param InputInterface $input |
38
|
|
|
* |
39
|
|
|
* @return |
40
|
|
|
*/ |
41
|
|
|
abstract protected function beforeRun(InputInterface $input); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Must return the full path for the output file where the console |
45
|
|
|
* output will be streamed. |
46
|
|
|
* |
47
|
|
|
* @param InputInterface $input |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
abstract protected function outputFilePath(InputInterface $input); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Run the command. |
55
|
|
|
* |
56
|
|
|
* @param $input |
57
|
|
|
* @param $output |
58
|
|
|
* |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
protected function effectiveRun($input, $output) |
62
|
|
|
{ |
63
|
|
|
$outputFile = $this->outputFilePath($input); |
64
|
|
|
|
65
|
|
|
$dualOutput = (new DualOutput(fopen($outputFile, 'a', false))) |
|
|
|
|
66
|
|
|
->setFile($outputFile) |
67
|
|
|
->setConsoleOutput($output); |
68
|
|
|
|
69
|
|
|
return parent::run( |
70
|
|
|
$this->input = $input, $this->output = $dualOutput |
|
|
|
|
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Handles exceptions that occurs while running the command. |
76
|
|
|
* |
77
|
|
|
* This give us a way to customize the actions that must be taken when something |
78
|
|
|
* went sideways. (ie. flag something as failed in database). |
79
|
|
|
* |
80
|
|
|
* @param Exception $e |
81
|
|
|
*/ |
82
|
|
|
protected function handeException(Exception $e) |
83
|
|
|
{ |
84
|
|
|
$this->error(TraceReserved::FAILED." {$e->getMessage()}"); |
85
|
|
|
|
86
|
|
|
app()->log->error($e); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* For some operations we need to get the value of an argument passed to the command. |
91
|
|
|
* The issue here is that parent::__constructor() is not called yet, so the symfony/console |
92
|
|
|
* has not parsed yet the arguments, so we need to to hack it manually. |
93
|
|
|
* |
94
|
|
|
* @param $input |
95
|
|
|
* @param $index |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getNotParsedArgument(InputInterface $input, $index) |
100
|
|
|
{ |
101
|
|
|
return explode(' ', (string) $input)[$index]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Do `-vvv` was used when calling the command. |
106
|
|
|
* |
107
|
|
|
* @param InputInterface $input |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
protected function isVerbose(InputInterface $input) |
112
|
|
|
{ |
113
|
|
|
return (bool) substr_count((string) $input, '-vvv'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|