|
9 | 9 | */ |
10 | 10 | namespace Phpbb\Epv\Output; |
11 | 11 |
|
12 | | -use SensioLabs\AnsiConverter\AnsiToHtmlConverter; |
13 | | -use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
14 | | -use Symfony\Component\Console\Output\OutputInterface; |
15 | | - |
16 | | -class HtmlOutput implements OutputInterface |
17 | | -{ |
18 | | - const TYPE_HTML = 1; |
19 | | - const TYPE_BBCODE = 2; |
20 | | - |
21 | | - private $buffer = ""; |
22 | | - private $type; |
23 | | - |
24 | | - /** |
25 | | - * @param int $type Output type (HTML or BBCode) |
26 | | - */ |
27 | | - public function __construct($type = self::TYPE_HTML) |
28 | | - { |
29 | | - $this->type = $type; |
30 | | - } |
31 | | - |
32 | | - /** |
33 | | - * Writes a message to the output. |
34 | | - * |
35 | | - * @param string|array $messages The message as an array of lines or a single string |
36 | | - * @param bool $newline Whether to add a newline |
37 | | - * @param int $type The type of output (one of the OUTPUT constants) |
38 | | - * |
39 | | - * @throws \InvalidArgumentException When unknown output type is given |
40 | | - * |
41 | | - * @api |
42 | | - */ |
43 | | - public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) |
44 | | - { |
45 | | - if (!is_array($messages)) |
46 | | - { |
47 | | - $messages = array($messages); |
48 | | - } |
49 | | - |
50 | | - foreach ($messages as $message) |
51 | | - { |
52 | | - $this->buffer .= $this->parse($message); |
53 | | - if ($newline) |
54 | | - { |
55 | | - $this->buffer .= "\n"; |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Writes a message to the output and adds a newline at the end. |
62 | | - * |
63 | | - * @param string|array $messages The message as an array of lines of a single string |
64 | | - * @param int $type The type of output (one of the OUTPUT constants) |
65 | | - * |
66 | | - * @throws \InvalidArgumentException When unknown output type is given |
67 | | - * |
68 | | - * @api |
69 | | - */ |
70 | | - public function writeln($messages, $type = self::OUTPUT_NORMAL) |
71 | | - { |
72 | | - $this->write($messages, true, $type); |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Sets the verbosity of the output. |
77 | | - * |
78 | | - * @param int $level The level of verbosity (one of the VERBOSITY constants) |
79 | | - * |
80 | | - * @api |
81 | | - */ |
82 | | - public function setVerbosity($level) |
83 | | - { |
84 | | - |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * Gets the current verbosity of the output. |
89 | | - * |
90 | | - * @return int The current level of verbosity (one of the VERBOSITY constants) |
91 | | - * |
92 | | - * @api |
93 | | - */ |
94 | | - public function getVerbosity(): int |
95 | | - { |
96 | | - |
97 | | - } |
98 | | - |
99 | | - /** |
100 | | - * Sets the decorated flag. |
101 | | - * |
102 | | - * @param bool $decorated Whether to decorate the messages |
103 | | - * |
104 | | - * @api |
105 | | - */ |
106 | | - public function setDecorated($decorated) |
107 | | - { |
108 | | - |
109 | | - } |
110 | | - |
111 | | - /** |
112 | | - * Gets the decorated flag. |
113 | | - * |
114 | | - * @return bool true if the output will decorate messages, false otherwise |
115 | | - * |
116 | | - * @api |
117 | | - */ |
118 | | - public function isDecorated(): bool |
119 | | - { |
120 | | - |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Sets output formatter. |
125 | | - * |
126 | | - * @param OutputFormatterInterface $formatter |
127 | | - * |
128 | | - * @api |
129 | | - */ |
130 | | - public function setFormatter(OutputFormatterInterface $formatter) |
131 | | - { |
132 | | - |
133 | | - } |
134 | | - |
135 | | - /** |
136 | | - * Returns current output formatter instance. |
137 | | - * |
138 | | - * @return OutputFormatterInterface |
139 | | - * |
140 | | - * @api |
141 | | - */ |
142 | | - public function getFormatter(): OutputFormatterInterface |
143 | | - { |
144 | | - |
145 | | - } |
146 | | - |
147 | | - public function getBuffer() |
148 | | - { |
149 | | - if ($this->type == self::TYPE_HTML) |
150 | | - { |
151 | | - $formatter = new OutputFormatter(true); |
152 | | - |
153 | | - $convertor = new AnsiToHtmlConverter(); |
154 | | - |
155 | | - return nl2br($convertor->convert($formatter->format($this->buffer))); |
156 | | - } |
157 | | - |
158 | | - return $this->buffer; |
159 | | - } |
160 | | - |
161 | | - /** |
162 | | - * Parse the code from the CLI to html. |
163 | | - * |
164 | | - * @param $message |
165 | | - * |
166 | | - * @return mixed Parsed message |
167 | | - */ |
168 | | - private function parse($message) |
169 | | - { |
170 | | - return $message; |
171 | | - } |
172 | | - |
173 | | - /** |
174 | | - * Returns whether verbosity is quiet (-q). |
175 | | - * |
176 | | - * @return bool true if verbosity is set to VERBOSITY_QUIET, false otherwise |
177 | | - */ |
178 | | - public function isQuiet(): bool |
179 | | - { |
180 | | - } |
181 | | - |
182 | | - /** |
183 | | - * Returns whether verbosity is verbose (-v). |
184 | | - * |
185 | | - * @return bool true if verbosity is set to VERBOSITY_VERBOSE, false otherwise |
186 | | - */ |
187 | | - public function isVerbose(): bool |
188 | | - { |
189 | | - } |
190 | | - |
191 | | - /** |
192 | | - * Returns whether verbosity is very verbose (-vv). |
193 | | - * |
194 | | - * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise |
195 | | - */ |
196 | | - public function isVeryVerbose(): bool |
197 | | - { |
198 | | - } |
199 | | - |
200 | | - /** |
201 | | - * Returns whether verbosity is debug (-vvv). |
202 | | - * |
203 | | - * @return bool true if verbosity is set to VERBOSITY_DEBUG, false otherwise |
204 | | - */ |
205 | | - public function isDebug(): bool |
206 | | - { |
207 | | - } |
| 12 | +// Runtime selection of HtmlOutput implementation based on PHP version and Symfony interface |
| 13 | +if (PHP_VERSION_ID >= 80000) { |
| 14 | + // Check if Symfony interface has union types (Symfony 7+) |
| 15 | + $reflection = new \ReflectionMethod('\Symfony\Component\Console\Output\OutputInterface', 'write'); |
| 16 | + $parameters = $reflection->getParameters(); |
| 17 | + $hasUnionTypes = $parameters[0]->getType() && $parameters[0]->getType()->__toString() === 'string|iterable'; |
| 18 | + |
| 19 | + if ($hasUnionTypes) { |
| 20 | + class_alias('\Phpbb\Epv\Output\HtmlOutputPhp8', '\Phpbb\Epv\Output\HtmlOutput'); |
| 21 | + } else { |
| 22 | + class_alias('\Phpbb\Epv\Output\HtmlOutputLegacy', '\Phpbb\Epv\Output\HtmlOutput'); |
| 23 | + } |
| 24 | +} else { |
| 25 | + class_alias('\Phpbb\Epv\Output\HtmlOutputLegacy', '\Phpbb\Epv\Output\HtmlOutput'); |
208 | 26 | } |
0 commit comments