|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\ConsoleColour\Themes; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\ConsoleColour\ConsoleColor; |
|
6
|
|
|
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException; |
|
7
|
|
|
use AlecRabbit\ConsoleColour\Themes\Contracts\DefaultThemes; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @method comment(string $text) |
|
11
|
|
|
* @method error(string $text) |
|
12
|
|
|
* @method info(string $text) |
|
13
|
|
|
* |
|
14
|
|
|
* @method yellow(string $text) |
|
15
|
|
|
* @method red(string $text) |
|
16
|
|
|
* @method green(string $text) |
|
17
|
|
|
* @method cyan(string $text) |
|
18
|
|
|
* @method magenta(string $text) |
|
19
|
|
|
|
|
20
|
|
|
* @method italic(string $text) |
|
21
|
|
|
* @method bold(string $text) |
|
22
|
|
|
* @method dark(string $text) |
|
23
|
|
|
* @method darkItalic(string $text) |
|
24
|
|
|
* @method white(string $text) |
|
25
|
|
|
* @method whiteBold(string $text) |
|
26
|
|
|
* @method underlined(string $text) |
|
27
|
|
|
* @method underlinedBold(string $text) |
|
28
|
|
|
* @method underlinedItalic(string $text) |
|
29
|
|
|
*/ |
|
30
|
|
|
class Themes implements DefaultThemes |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
protected $definedThemes; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool */ |
|
36
|
|
|
protected $doColorize = false; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ConsoleColor */ |
|
39
|
|
|
protected $color; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Themed constructor. |
|
43
|
|
|
* @param null|bool $colorize |
|
44
|
|
|
* @throws InvalidStyleException |
|
45
|
|
|
*/ |
|
46
|
6 |
|
public function __construct(?bool $colorize = null) |
|
47
|
|
|
{ |
|
48
|
6 |
|
$this->color = new ConsoleColor(); |
|
49
|
6 |
|
$this->doColorize = $this->refineColorize($colorize); |
|
50
|
6 |
|
$this->setThemes(); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param null|bool $colorize |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
6 |
|
protected function refineColorize(?bool $colorize): bool |
|
58
|
|
|
{ |
|
59
|
6 |
|
if ($supported = $this->color->isSupported()) { |
|
60
|
6 |
|
return $colorize ?? $supported; |
|
61
|
|
|
} |
|
62
|
|
|
// @codeCoverageIgnoreStart |
|
63
|
|
|
return false; |
|
64
|
|
|
// @codeCoverageIgnoreEnd |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param bool $override |
|
69
|
|
|
* @throws InvalidStyleException |
|
70
|
|
|
*/ |
|
71
|
6 |
|
protected function setThemes(bool $override = false): void |
|
72
|
|
|
{ |
|
73
|
6 |
|
foreach ($this->getThemes() as $name => $styles) { |
|
74
|
6 |
|
$this->color->addTheme($name, $styles, $override); |
|
75
|
|
|
} |
|
76
|
6 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
* |
|
81
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function getThemes(): array |
|
84
|
|
|
{ |
|
85
|
6 |
|
if (null !== $this->definedThemes) { |
|
86
|
1 |
|
return $this->definedThemes; |
|
87
|
|
|
} |
|
88
|
|
|
return |
|
89
|
6 |
|
$this->definedThemes = $this->prepareThemes(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
6 |
|
protected function prepareThemes(): array |
|
96
|
|
|
{ |
|
97
|
6 |
|
return static::THEMES; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @param array $arguments |
|
103
|
|
|
* @return string |
|
104
|
|
|
* @throws \Throwable |
|
105
|
|
|
*/ |
|
106
|
6 |
|
public function __call(string $name, array $arguments): string |
|
107
|
|
|
{ |
|
108
|
6 |
|
$this->assertMethodName($name); |
|
109
|
4 |
|
$this->assertArgs($name, $arguments); |
|
110
|
|
|
|
|
111
|
|
|
return |
|
112
|
3 |
|
$this->apply($this->definedThemes[$name], $arguments[0]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
*/ |
|
118
|
6 |
|
protected function assertMethodName(string $name): void |
|
119
|
|
|
{ |
|
120
|
6 |
|
if (!\array_key_exists($name, $this->definedThemes)) { |
|
121
|
2 |
|
throw new \BadMethodCallException('Unknown method call [' . static::class . '::' . $name . '].'); |
|
122
|
|
|
} |
|
123
|
4 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* @param array $arguments |
|
128
|
|
|
*/ |
|
129
|
4 |
|
protected function assertArgs(string $name, array $arguments): void |
|
130
|
|
|
{ |
|
131
|
4 |
|
if (1 !== \count($arguments)) { |
|
132
|
2 |
|
throw new \ArgumentCountError( |
|
133
|
2 |
|
'Method [' . static::class . '::' . $name . '] accepts only one argument.' |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
3 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param array|string $style |
|
140
|
|
|
* @param string $text |
|
141
|
|
|
* @return string |
|
142
|
|
|
* @throws \Throwable |
|
143
|
|
|
*/ |
|
144
|
3 |
|
protected function apply($style, $text): string |
|
145
|
|
|
{ |
|
146
|
|
|
return |
|
147
|
3 |
|
$this->doColorize ? $this->color->apply($style, $text) : $text; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|