1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Robo\Common; |
4
|
|
|
|
5
|
|
|
use Robo\Robo; |
6
|
|
|
use Robo\TaskInfo; |
7
|
|
|
use Robo\Contract\OutputAdapterInterface; |
8
|
|
|
use Robo\Contract\VerbosityThresholdInterface; |
9
|
|
|
use Consolidation\Log\ConsoleLogLevel; |
10
|
|
|
use Psr\Log\LoggerAwareTrait; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use Robo\Contract\ProgressIndicatorAwareInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Task input/output methods. TaskIO is 'used' in BaseTask, so any |
17
|
|
|
* task that extends this class has access to all of the methods here. |
18
|
|
|
* printTaskInfo, printTaskSuccess, and printTaskError are the three |
19
|
|
|
* primary output methods that tasks are encouraged to use. Tasks should |
20
|
|
|
* avoid using the IO trait output methods. |
21
|
|
|
*/ |
22
|
|
|
trait VerbosityThresholdTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \Robo\Contract\OutputAdapterInterface |
26
|
|
|
*/ |
27
|
|
|
protected $outputAdapter; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $verbosityThreshold = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Required verbosity level before any TaskIO output will be produced. |
36
|
|
|
* e.g. OutputInterface::VERBOSITY_VERBOSE |
37
|
|
|
* |
38
|
|
|
* @param int $verbosityThreshold |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function setVerbosityThreshold($verbosityThreshold) |
43
|
|
|
{ |
44
|
|
|
$this->verbosityThreshold = $verbosityThreshold; |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function verbosityThreshold() |
52
|
|
|
{ |
53
|
|
|
return $this->verbosityThreshold; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setOutputAdapter(OutputAdapterInterface $outputAdapter) |
57
|
|
|
{ |
58
|
|
|
$this->outputAdapter = $outputAdapter; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \Robo\Contract\OutputAdapterInterface |
63
|
|
|
*/ |
64
|
|
|
public function outputAdapter() |
65
|
|
|
{ |
66
|
|
|
return $this->outputAdapter; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public function hasOutputAdapter() |
73
|
|
|
{ |
74
|
|
|
return isset($this->outputAdapter); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function verbosityMeetsThreshold() |
81
|
|
|
{ |
82
|
|
|
if ($this->hasOutputAdapter()) { |
83
|
|
|
return $this->outputAdapter()->verbosityMeetsThreshold($this->verbosityThreshold()); |
84
|
|
|
} |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Print a message if the selected verbosity level is over this task's |
90
|
|
|
* verbosity threshold. |
91
|
|
|
* |
92
|
|
|
* @param string $message |
93
|
|
|
*/ |
94
|
|
|
public function writeMessage($message) |
95
|
|
|
{ |
96
|
|
|
if (!$this->verbosityMeetsThreshold()) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
$this->outputAdapter()->writeMessage($message); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|