|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Component\Console\Style; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Exception\InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
18
|
|
|
use Symfony\Component\Console\Terminal; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
final class ImagineStyle implements ImagineStyleInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var SymfonyStyle |
|
27
|
|
|
*/ |
|
28
|
|
|
private $io; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $decoration; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(InputInterface $input, OutputInterface $output, bool $decoration = true) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->io = new SymfonyStyle($input, $output); |
|
38
|
|
|
$this->decoration = $decoration; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function text(string $string, array $replacements = []): ImagineStyleInterface |
|
42
|
|
|
{ |
|
43
|
|
|
$this->io->write($this->compileString($string, $replacements)); |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function line(string $string, array $replacements = []): ImagineStyleInterface |
|
49
|
|
|
{ |
|
50
|
|
|
$this->io->writeln($this->compileString($string, $replacements)); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function newline(int $count = 1): ImagineStyleInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$this->io->newLine($count); |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function space(int $count = 1): ImagineStyleInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->text(str_repeat(' ', $count)); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function separator(string $character = null, int $width = null, string $fg = null, bool $newline = true): ImagineStyleInterface |
|
68
|
|
|
{ |
|
69
|
|
|
if (null === $width) { |
|
70
|
|
|
$width = (new Terminal())->getWidth(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->text('<fg=%2$s;>%1$s</>', [str_repeat($character ?: '-', $width), $fg ?: 'default']); |
|
74
|
|
|
|
|
75
|
|
|
if ($newline) { |
|
76
|
|
|
$this->newline(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function status(string $status, string $fg = null, string $bg = null): ImagineStyleInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->text( |
|
|
|
|
|
|
85
|
|
|
sprintf('<fg=%2$s;bg=%3$s>(</><fg=%2$s;bg=%3$s;options=bold>%1$s</><fg=%2$s;bg=%3$s>)</>', $status, $fg ?: 'default', $bg ?: 'default') |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function group(string $item, string $group, string $fg = null, string $bg = null): ImagineStyleInterface |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->text( |
|
|
|
|
|
|
92
|
|
|
sprintf('<fg=%3$s;bg=%4$s;options=bold>%1$s[</><fg=%3$s;bg=%4$s>%2$s</><fg=%3$s;bg=%4$s;options=bold>]</>', $item, $group, $fg ?: 'default', $bg ?: 'default') |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function title(string $title, string $type = null, string $fg = null, string $bg = null): ImagineStyleInterface |
|
97
|
|
|
{ |
|
98
|
|
|
if (!$this->decoration) { |
|
99
|
|
|
return $this->plainTitle($title, $type); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->block($title, $type, $fg ?: 'white', $bg ?: 'magenta'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function smallBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->block($string, $type, $fg, $bg, $prefix, false); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function largeBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->block($string, $type, $fg, $bg, $prefix, true); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function okayBlock(string $string, array $replacements = []): ImagineStyleInterface |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'OKAY', 'black', 'green', '-'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function noteBlock(string $string, array $replacements = []): ImagineStyleInterface |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'NOTE', 'yellow', 'black', '/'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function critBlock(string $string, array $replacements = []): ImagineStyleInterface |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'ERROR', 'white', 'red', '#'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
private function plainTitle(string $title, string $type = null): ImagineStyleInterface |
|
131
|
|
|
{ |
|
132
|
|
|
$this->newline(); |
|
133
|
|
|
|
|
134
|
|
|
if ($type) { |
|
|
|
|
|
|
135
|
|
|
$this->line('# [%s] %s', [$type, $title]); |
|
136
|
|
|
} else { |
|
137
|
|
|
$this->line('# %s', [$title]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this->newline(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function block(string $string, string $type = null, string $fg = null, string $bg = null, string $prefix = null, bool $padding = true): ImagineStyleInterface |
|
144
|
|
|
{ |
|
145
|
|
|
if (!$this->decoration) { |
|
146
|
|
|
return $this->plainBlock($string, $type); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->io->block($string, $type, sprintf('fg=%s;bg=%s', $fg ?: 'default', $bg ?: 'default'), $prefix ? sprintf(' %s ', $prefix) : ' ', $padding); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function plainBlock(string $string, string $type): ImagineStyleInterface |
|
155
|
|
|
{ |
|
156
|
|
|
return $this |
|
157
|
|
|
->newline() |
|
158
|
|
|
->line('[%s] %s', [$type, $string]) |
|
159
|
|
|
->newline(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function compileString(string $format, array $replacements = []): string |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$this->decoration) { |
|
165
|
|
|
$format = strip_tags($format); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (0 === count($replacements)) { |
|
169
|
|
|
return $format; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (false !== $compiled = @vsprintf($format, $replacements)) { |
|
173
|
|
|
return $compiled; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
throw new InvalidArgumentException( |
|
177
|
|
|
sprintf('Invalid string format "%s" or replacements "%s".', $format, implode(', ', array_map(function ($replacement) { |
|
178
|
|
|
return var_export($replacement, true); |
|
179
|
|
|
}, $replacements))) |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.