|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Formatter; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
15
|
|
|
use Webmozart\Console\Adapter\StyleConverter; |
|
16
|
|
|
use Webmozart\Console\Api\Formatter\Formatter; |
|
17
|
|
|
use Webmozart\Console\Api\Formatter\Style; |
|
18
|
|
|
use Webmozart\Console\Api\Formatter\StyleSet; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A formatter that replaces style tags by ANSI format codes. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 1.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class AnsiFormatter implements Formatter |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var OutputFormatter |
|
31
|
|
|
*/ |
|
32
|
|
|
private $innerFormatter; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates the formatter. |
|
36
|
|
|
* |
|
37
|
|
|
* @param StyleSet $styleSet The style set to use. |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
146 |
View Code Duplication |
public function __construct(StyleSet $styleSet = null) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
146 |
|
$this->innerFormatter = new OutputFormatter(true); |
|
42
|
|
|
|
|
43
|
146 |
|
if (!$styleSet) { |
|
44
|
143 |
|
$styleSet = new DefaultStyleSet(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
146 |
|
foreach ($styleSet->toArray() as $tag => $style) { |
|
48
|
146 |
|
$this->innerFormatter->setStyle($tag, StyleConverter::convert($style)); |
|
49
|
|
|
} |
|
50
|
146 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function format($string, Style $style = null) |
|
56
|
|
|
{ |
|
57
|
2 |
|
if (null !== $style) { |
|
58
|
1 |
|
$this->innerFormatter->getStyleStack()->push(StyleConverter::convert($style)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
$formatted = $this->innerFormatter->format($string); |
|
62
|
|
|
|
|
63
|
2 |
|
if (null !== $style) { |
|
64
|
1 |
|
$this->innerFormatter->getStyleStack()->pop(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $formatted; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function removeFormat($string) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->innerFormatter->setDecorated(false); |
|
76
|
1 |
|
$formatted = $this->innerFormatter->format($string); |
|
77
|
1 |
|
$this->innerFormatter->setDecorated(true); |
|
78
|
|
|
|
|
79
|
1 |
|
return $formatted; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.