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