bluetree-service /
symfony-console-style
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace BlueConsole; |
||||
| 6 | |||||
| 7 | use Symfony\Component\Console\Style\SymfonyStyle; |
||||
| 8 | |||||
| 9 | class Style extends SymfonyStyle |
||||
| 10 | { |
||||
| 11 | use AdditionalStyles; |
||||
| 12 | |||||
| 13 | public const DAY_SECONDS = 86400; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * @param bool $newLine |
||||
| 17 | * @return Style |
||||
| 18 | * @throws \Exception |
||||
| 19 | */ |
||||
| 20 | 4 | public function timer(bool $newLine = true): self |
|||
| 21 | { |
||||
| 22 | 4 | $this->write($this->getTimer()); |
|||
| 23 | |||||
| 24 | 4 | if ($newLine) { |
|||
| 25 | 4 | $this->newLine(); |
|||
| 26 | } |
||||
| 27 | |||||
| 28 | 4 | return $this; |
|||
| 29 | } |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * @param string $section |
||||
| 33 | * @param string $message |
||||
| 34 | * @param string $style |
||||
| 35 | * @return $this |
||||
| 36 | * @throws \Exception |
||||
| 37 | * @throws \InvalidArgumentException |
||||
| 38 | */ |
||||
| 39 | 4 | public function formatSection(string $section, string $message, string $style = 'info'): self |
|||
| 40 | { |
||||
| 41 | 4 | $timer = $this->getTimer(true); |
|||
| 42 | |||||
| 43 | 4 | $this->writeln( |
|||
| 44 | 4 | $timer . $this->formatter->formatSection( |
|||
| 45 | 4 | $section, |
|||
| 46 | 2 | $message, |
|||
| 47 | 2 | $style |
|||
| 48 | ) |
||||
| 49 | ); |
||||
| 50 | |||||
| 51 | 4 | return $this; |
|||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @param string|array $messages |
||||
| 56 | * @param string $style |
||||
| 57 | * @param bool $large |
||||
| 58 | * @return $this |
||||
| 59 | * @throws \Exception |
||||
| 60 | * @throws \InvalidArgumentException |
||||
| 61 | * @noinspection MissingParameterTypeDeclarationInspection |
||||
| 62 | */ |
||||
| 63 | 12 | public function formatBlock($messages, string $style, bool $large = false): self |
|||
| 64 | { |
||||
| 65 | 12 | $timer = $this->getTimer(true); |
|||
| 66 | |||||
| 67 | 12 | if (!\is_array($messages)) { |
|||
| 68 | 4 | $messages = [$messages]; |
|||
| 69 | } |
||||
| 70 | |||||
| 71 | 12 | foreach ($messages as $message) { |
|||
| 72 | 12 | $this->writeln( |
|||
| 73 | 12 | $timer . $this->formatter->formatBlock( |
|||
| 74 | 12 | $message, |
|||
| 75 | 6 | $style, |
|||
| 76 | 6 | $large |
|||
| 77 | ) |
||||
| 78 | ); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | 12 | return $this; |
|||
| 82 | } |
||||
| 83 | |||||
| 84 | /** |
||||
| 85 | * @param array $message |
||||
| 86 | * @return $this |
||||
| 87 | * @throws \Exception |
||||
| 88 | * @throws \InvalidArgumentException |
||||
| 89 | */ |
||||
| 90 | 4 | public function errorLine(array $message): self |
|||
| 91 | { |
||||
| 92 | 4 | return $this->formatBlock($message, 'error'); |
|||
| 93 | } |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * @param $message |
||||
| 97 | * @return $this |
||||
| 98 | * @throws \InvalidArgumentException |
||||
| 99 | * @throws \Exception |
||||
| 100 | */ |
||||
| 101 | 12 | public function okMessage(string $message): self |
|||
| 102 | { |
||||
| 103 | 12 | return $this->renderBlock(' <info>OK</info> ', $message); |
|||
| 104 | } |
||||
| 105 | |||||
| 106 | /** |
||||
| 107 | * @param $message |
||||
| 108 | * @return $this |
||||
| 109 | * @throws \InvalidArgumentException |
||||
| 110 | * @throws \Exception |
||||
| 111 | */ |
||||
| 112 | 12 | public function errorMessage(string $message): self |
|||
| 113 | { |
||||
| 114 | 12 | return $this->renderBlock(' <fg=red>FAIL</> ', $message); |
|||
| 115 | } |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * @param string $message |
||||
| 119 | * @return $this |
||||
| 120 | * @throws \InvalidArgumentException |
||||
| 121 | * @throws \Exception |
||||
| 122 | */ |
||||
| 123 | 12 | public function warningMessage(string $message): self |
|||
| 124 | { |
||||
| 125 | 12 | return $this->renderBlock(' <comment>WARN</comment> ', $message); |
|||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * @param string $message |
||||
| 130 | * @return $this |
||||
| 131 | * @throws \InvalidArgumentException |
||||
| 132 | * @throws \Exception |
||||
| 133 | */ |
||||
| 134 | 12 | public function infoMessage(string $message): self |
|||
| 135 | { |
||||
| 136 | 12 | return $this->renderBlock(' <fg=blue>INFO</> ', $message); |
|||
| 137 | } |
||||
| 138 | |||||
| 139 | /** |
||||
| 140 | * @param string $block |
||||
| 141 | * @param string $message |
||||
| 142 | * @return Style |
||||
| 143 | * @throws \Exception |
||||
| 144 | */ |
||||
| 145 | 48 | public function renderBlock(string $block, string $message): self |
|||
| 146 | { |
||||
| 147 | 48 | $timer = $this->getTimer(true); |
|||
| 148 | 48 | $alignment = $this->align(8, $this->align); |
|||
| 149 | |||||
| 150 | 48 | $this->write("{$timer}[$block]"); |
|||
| 151 | 48 | $this->write($alignment); |
|||
| 152 | 48 | $this->writeln($message); |
|||
| 153 | |||||
| 154 | 48 | return $this; |
|||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * @param string $message |
||||
| 159 | * @return $this |
||||
| 160 | * @throws \InvalidArgumentException |
||||
| 161 | * @throws \Exception |
||||
| 162 | */ |
||||
| 163 | 4 | public function note($message): self |
|||
| 164 | { |
||||
| 165 | 4 | return $this->genericBlock($message, 'blue', 'note'); |
|||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * @param string $message |
||||
| 170 | * @return $this |
||||
| 171 | * @throws \InvalidArgumentException |
||||
| 172 | * @throws \Exception |
||||
| 173 | */ |
||||
| 174 | 4 | public function caution($message): self |
|||
| 175 | { |
||||
| 176 | 4 | return $this->genericBlock($message, 'magenta', 'caution'); |
|||
| 177 | } |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * @param string $message |
||||
| 181 | * @return $this |
||||
| 182 | * @throws \InvalidArgumentException |
||||
| 183 | * @throws \Exception |
||||
| 184 | */ |
||||
| 185 | 4 | public function success($message): self |
|||
| 186 | { |
||||
| 187 | 4 | return $this->genericBlock($message, 'green', 'success'); |
|||
| 188 | } |
||||
| 189 | |||||
| 190 | /** |
||||
| 191 | * @param string $message |
||||
| 192 | * @return $this |
||||
| 193 | * @throws \InvalidArgumentException |
||||
| 194 | * @throws \Exception |
||||
| 195 | */ |
||||
| 196 | 4 | public function warning($message): self |
|||
| 197 | { |
||||
| 198 | 4 | return $this->genericBlock($message, 'yellow', 'warning'); |
|||
| 199 | } |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * @param string $message |
||||
| 203 | * @return $this |
||||
| 204 | * @throws \InvalidArgumentException |
||||
| 205 | * @throws \Exception |
||||
| 206 | */ |
||||
| 207 | 4 | public function error($message): self |
|||
| 208 | { |
||||
| 209 | 4 | return $this->genericBlock($message, 'red', 'error'); |
|||
| 210 | } |
||||
| 211 | |||||
| 212 | /** |
||||
| 213 | * @param string $message |
||||
| 214 | * @param string $background |
||||
| 215 | * @param string|null $type |
||||
| 216 | * @param int $length |
||||
| 217 | * @return $this |
||||
| 218 | * @throws \InvalidArgumentException |
||||
| 219 | * @throws \Exception |
||||
| 220 | */ |
||||
| 221 | 28 | public function genericBlock(string $message, string $background, ?string $type, int $length = 100): self |
|||
| 222 | { |
||||
| 223 | 28 | if (\trim($type) === '') { |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 224 | 4 | return $this; |
|||
| 225 | } |
||||
| 226 | |||||
| 227 | 24 | $type = \strtoupper($type); |
|||
|
0 ignored issues
–
show
It seems like
$type can also be of type null; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 228 | 24 | $alignment = $this->align(0, $length); |
|||
| 229 | 24 | $alignmentMessage = $this->align($message, $length - (\mb_strlen($type) + 5)); |
|||
| 230 | |||||
| 231 | 24 | $timer = $this->getTimer(true); |
|||
| 232 | 24 | $timer ? $this->writeln($timer) : null; |
|||
| 233 | |||||
| 234 | 24 | $this->writeln("<bg=$background>$alignment</>"); |
|||
| 235 | 24 | $this->writeln("<fg=white;bg=$background> [$type] $message$alignmentMessage</>"); |
|||
| 236 | 24 | $this->writeln("<bg=$background>$alignment</>"); |
|||
| 237 | 24 | $this->newLine(); |
|||
| 238 | |||||
| 239 | 24 | return $this; |
|||
| 240 | } |
||||
| 241 | |||||
| 242 | public function floatingProgressBar(): void |
||||
| 243 | { |
||||
| 244 | |||||
| 245 | } |
||||
| 246 | |||||
| 247 | public function longRunCommand(string $command): void |
||||
| 248 | { |
||||
| 249 | //[ *** ] |
||||
| 250 | } |
||||
| 251 | |||||
| 252 | /** |
||||
| 253 | * @todo add multi line block |
||||
| 254 | */ |
||||
| 255 | |||||
| 256 | /* |
||||
| 257 | http://symfony.com/doc/current/components/console/helpers/formatterhelper.html |
||||
| 258 | https://symfony.com/doc/current/console/style.html |
||||
| 259 | */ |
||||
| 260 | } |
||||
| 261 |