|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpSchool\CliMenu\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSchool\CliMenu\CliMenu; |
|
6
|
|
|
use PhpSchool\CliMenu\MenuItem\LineBreakItem; |
|
7
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuMenuItem; |
|
8
|
|
|
use PhpSchool\CliMenu\MenuItem\SelectableItem; |
|
9
|
|
|
use PhpSchool\CliMenu\MenuItem\SplitItem; |
|
10
|
|
|
use PhpSchool\CliMenu\MenuItem\StaticItem; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Aydin Hassan <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class SplitItemBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CliMenu |
|
19
|
|
|
*/ |
|
20
|
|
|
private $menu; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var SplitItem |
|
24
|
|
|
*/ |
|
25
|
|
|
private $splitItem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Whether or not to auto create keyboard shortcuts for items |
|
29
|
|
|
* when they contain square brackets. Eg: [M]y item |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $autoShortcuts = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Regex to auto match for shortcuts defaults to looking |
|
37
|
|
|
* for a single character encased in square brackets |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $autoShortcutsRegex = '/\[(.)\]/'; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(CliMenu $menu) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->menu = $menu; |
|
46
|
|
|
$this->splitItem = new SplitItem(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function addItem( |
|
50
|
|
|
string $text, |
|
51
|
|
|
callable $itemCallable, |
|
52
|
|
|
bool $showItemExtra = false, |
|
53
|
|
|
bool $disabled = false |
|
54
|
|
|
) : self { |
|
55
|
|
|
$this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function addStaticItem(string $text) : self |
|
61
|
|
|
{ |
|
62
|
|
|
$this->splitItem->addItem(new StaticItem($text)); |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
|
68
|
|
|
{ |
|
69
|
|
|
$this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function addSubMenu(string $text, \Closure $callback) : self |
|
75
|
|
|
{ |
|
76
|
|
|
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->autoShortcuts) { |
|
79
|
|
|
$builder->enableAutoShortcuts($this->autoShortcutsRegex); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$callback = $callback->bindTo($builder); |
|
83
|
|
|
$callback($builder); |
|
84
|
|
|
|
|
85
|
|
|
$menu = $builder->build(); |
|
86
|
|
|
$menu->setParent($this->menu); |
|
87
|
|
|
|
|
88
|
|
|
$this->splitItem->addItem(new MenuMenuItem( |
|
89
|
|
|
$text, |
|
90
|
|
|
$menu, |
|
91
|
|
|
$builder->isMenuDisabled() |
|
92
|
|
|
)); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setGutter(int $gutter) : self |
|
98
|
|
|
{ |
|
99
|
|
|
$this->splitItem->setGutter($gutter); |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function enableAutoShortcuts(string $regex = null) : self |
|
105
|
|
|
{ |
|
106
|
|
|
$this->autoShortcuts = true; |
|
107
|
|
|
|
|
108
|
|
|
if (null !== $regex) { |
|
109
|
|
|
$this->autoShortcutsRegex = $regex; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function build() : SplitItem |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->splitItem; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|