1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: yunwuxin <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
namespace think\console\output\formatter; |
13
|
|
|
|
14
|
|
|
class Stack |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Style[] |
19
|
|
|
*/ |
20
|
|
|
private $styles; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Style |
24
|
|
|
*/ |
25
|
|
|
private $emptyStyle; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 构造方法 |
29
|
|
|
* @param Style|null $emptyStyle |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Style $emptyStyle = null) |
32
|
|
|
{ |
33
|
|
|
$this->emptyStyle = $emptyStyle ?: new Style(); |
34
|
|
|
$this->reset(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 重置堆栈 |
39
|
|
|
*/ |
40
|
|
|
public function reset(): void |
41
|
|
|
{ |
42
|
|
|
$this->styles = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 推一个样式进入堆栈 |
47
|
|
|
* @param Style $style |
48
|
|
|
*/ |
49
|
|
|
public function push(Style $style): void |
50
|
|
|
{ |
51
|
|
|
$this->styles[] = $style; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 从堆栈中弹出一个样式 |
56
|
|
|
* @param Style|null $style |
57
|
|
|
* @return Style |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public function pop(Style $style = null): Style |
61
|
|
|
{ |
62
|
|
|
if (empty($this->styles)) { |
63
|
|
|
return $this->emptyStyle; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (null === $style) { |
67
|
|
|
return array_pop($this->styles); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var int $index |
72
|
|
|
* @var Style $stackedStyle |
73
|
|
|
*/ |
74
|
|
|
foreach (array_reverse($this->styles, true) as $index => $stackedStyle) { |
75
|
|
|
if ($style->apply('') === $stackedStyle->apply('')) { |
76
|
|
|
$this->styles = array_slice($this->styles, 0, $index); |
77
|
|
|
|
78
|
|
|
return $stackedStyle; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new \InvalidArgumentException('Incorrectly nested style tag found.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 计算堆栈的当前样式。 |
87
|
|
|
* @return Style |
88
|
|
|
*/ |
89
|
|
|
public function getCurrent(): Style |
90
|
|
|
{ |
91
|
|
|
if (empty($this->styles)) { |
92
|
|
|
return $this->emptyStyle; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->styles[count($this->styles) - 1]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Style $emptyStyle |
100
|
|
|
* @return Stack |
101
|
|
|
*/ |
102
|
|
|
public function setEmptyStyle(Style $emptyStyle) |
103
|
|
|
{ |
104
|
|
|
$this->emptyStyle = $emptyStyle; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Style |
111
|
|
|
*/ |
112
|
|
|
public function getEmptyStyle(): Style |
113
|
|
|
{ |
114
|
|
|
return $this->emptyStyle; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|