|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Yawik project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anthonius Munthi |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Yawik\Composer; |
|
13
|
|
|
|
|
14
|
|
|
use Composer\IO\IOInterface; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
use Psr\Log\LogLevel; |
|
17
|
|
|
use Symfony\Component\Console\Input\StringInput; |
|
18
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
21
|
|
|
use Yawik\Composer\Event\ActivateEvent; |
|
22
|
|
|
|
|
23
|
|
|
trait LogTrait |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var OutputInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $output; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var InputInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $input; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var LoggerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $logger; |
|
40
|
|
|
|
|
41
|
|
|
public function onActivateEvent(ActivateEvent $event) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->setOutputFromComposerIO($event->getOutput()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setOutputFromComposerIO(IOInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
$level = OutputInterface::VERBOSITY_NORMAL; |
|
49
|
|
|
|
|
50
|
|
|
if ($output->isVeryVerbose() || $output->isDebug()) { |
|
51
|
|
|
$level = OutputInterface::VERBOSITY_VERY_VERBOSE; |
|
52
|
|
|
} elseif ($output->isVerbose()) { |
|
53
|
|
|
$level = OutputInterface::VERBOSITY_VERBOSE; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->getOutput()->setVerbosity($level); |
|
57
|
|
|
$this->getOutput()->setDecorated($output->isDecorated()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getInput() |
|
61
|
|
|
{ |
|
62
|
|
|
if (is_null($this->input)) { |
|
63
|
|
|
$this->input = new StringInput(''); |
|
64
|
|
|
} |
|
65
|
|
|
return $this->input; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param InputInterface $input |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setInput($input) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->input = $input; |
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return OutputInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getOutput() |
|
82
|
|
|
{ |
|
83
|
|
|
if (is_null($this->output)) { |
|
84
|
|
|
$this->output = new ConsoleOutput(); |
|
85
|
|
|
} |
|
86
|
|
|
return $this->output; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param OutputInterface $output |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setOutput($output) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->output = $output; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set a logger to use |
|
102
|
|
|
* @param LoggerInterface $logger |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setLogger(LoggerInterface $logger) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->logger = $logger; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $message |
|
114
|
|
|
*/ |
|
115
|
|
|
public function logDebug($message, $context = 'yawik') |
|
116
|
|
|
{ |
|
117
|
|
|
$this->doLog(LogLevel::DEBUG, $message, $context); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $message |
|
122
|
|
|
*/ |
|
123
|
|
|
public function logError($message, $context = 'yawik') |
|
124
|
|
|
{ |
|
125
|
|
|
$this->doLog(LogLevel::ERROR, $message, $context); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $message |
|
130
|
|
|
* @param string $context |
|
131
|
|
|
*/ |
|
132
|
|
|
public function log($message, $context = 'yawik') |
|
133
|
|
|
{ |
|
134
|
|
|
$this->doLog(LogLevel::INFO, $message, $context); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function isCli() |
|
138
|
|
|
{ |
|
139
|
|
|
return php_sapi_name() === 'cli'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function doLog($level, $message, $context = 'yawik') |
|
143
|
|
|
{ |
|
144
|
|
|
$message = str_replace(getcwd().DIRECTORY_SEPARATOR, '', $message); |
|
145
|
|
|
if (is_object($this->logger)) { |
|
146
|
|
|
$this->logger->log($level, $message, [$context]); |
|
147
|
|
|
} |
|
148
|
|
|
if ($this->isCli()) { |
|
149
|
|
|
switch ($level) { |
|
150
|
|
|
case LogLevel::DEBUG: |
|
151
|
|
|
$outputLevel = OutputInterface::VERBOSITY_VERY_VERBOSE; |
|
152
|
|
|
break; |
|
153
|
|
|
case LogLevel::ERROR: |
|
154
|
|
|
$message = '<error>'.$message.'</error>'; |
|
155
|
|
|
$outputLevel = OutputInterface::OUTPUT_NORMAL; |
|
156
|
|
|
break; |
|
157
|
|
|
case LogLevel::INFO: |
|
158
|
|
|
default: |
|
159
|
|
|
$outputLevel = OutputInterface::OUTPUT_NORMAL; |
|
160
|
|
|
break; |
|
161
|
|
|
} |
|
162
|
|
|
$this->doWrite($message, $outputLevel, $context); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function doWrite($message, $outputLevel = 0, $context = 'yawik') |
|
167
|
|
|
{ |
|
168
|
|
|
$message = sprintf( |
|
169
|
|
|
'<info>[%s]</info> %s', |
|
170
|
|
|
$context, |
|
171
|
|
|
$message |
|
172
|
|
|
); |
|
173
|
|
|
$this->getOutput()->writeln($message, $outputLevel); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|