1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core\Config\A; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\IDriver; |
8
|
|
|
use AlecRabbit\Spinner\Core\Config\Config; |
9
|
|
|
use AlecRabbit\Spinner\Core\Config\Contract\IConfig; |
10
|
|
|
use AlecRabbit\Spinner\Core\Config\Contract\IConfigBuilder; |
11
|
|
|
use AlecRabbit\Spinner\Core\Config\LoopConfig; |
12
|
|
|
use AlecRabbit\Spinner\Core\Config\SpinnerConfig; |
13
|
|
|
use AlecRabbit\Spinner\Core\Contract\IDriverBuilder; |
14
|
|
|
use AlecRabbit\Spinner\Core\Defaults\Contract\IDefaults; |
15
|
|
|
use AlecRabbit\Spinner\Core\Factory\DriverFactory; |
16
|
|
|
use AlecRabbit\Spinner\Core\Factory\RevolverFactory; |
17
|
|
|
use AlecRabbit\Spinner\Core\Factory\WidgetFactory; |
18
|
|
|
use AlecRabbit\Spinner\Core\Output\StreamOutput; |
19
|
|
|
use AlecRabbit\Spinner\Core\Pattern\Contract\IPattern; |
20
|
|
|
use AlecRabbit\Spinner\Core\Revolver\Contract\IFrameRevolverBuilder; |
21
|
|
|
use AlecRabbit\Spinner\Core\Timer; |
22
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetBuilder; |
23
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetComposite; |
24
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetRevolverBuilder; |
25
|
|
|
use ArrayObject; |
26
|
|
|
use Traversable; |
27
|
|
|
|
28
|
|
|
abstract class AConfigBuilder implements IConfigBuilder |
29
|
|
|
{ |
30
|
|
|
protected IDriverBuilder $driverBuilder; |
31
|
|
|
protected ?IWidgetComposite $rootWidget = null; |
32
|
|
|
protected ?IPattern $rootWidgetCharPattern = null; |
33
|
|
|
protected ?IPattern $rootWidgetStylePattern = null; |
34
|
|
|
protected IFrameRevolverBuilder $frameRevolverBuilder; |
35
|
|
|
protected IWidgetBuilder $widgetBuilder; |
36
|
|
|
protected IWidgetRevolverBuilder $widgetRevolverBuilder; |
37
|
|
|
protected ?Traversable $widgets = null; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
protected IDefaults $defaults, |
41
|
|
|
) { |
42
|
|
|
$this->widgetBuilder = WidgetFactory::getWidgetBuilder($this->defaults); |
43
|
|
|
$this->widgetRevolverBuilder = WidgetFactory::getWidgetRevolverBuilder($this->defaults); |
44
|
|
|
$this->driverBuilder = DriverFactory::getDriverBuilder($this->defaults); |
45
|
|
|
$this->frameRevolverBuilder = RevolverFactory::getRevolverBuilder($this->defaults); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function withRootWidget(IWidgetComposite $widget): static |
49
|
|
|
{ |
50
|
|
|
$clone = clone $this; |
51
|
|
|
$clone->rootWidget = $widget; |
52
|
|
|
return $clone; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function withWidgets(Traversable $widgets): static |
56
|
|
|
{ |
57
|
|
|
$clone = clone $this; |
58
|
|
|
$clone->widgets = $widgets; |
59
|
|
|
return $clone; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function withStylePattern(IPattern $pattern): static |
63
|
|
|
{ |
64
|
|
|
$clone = clone $this; |
65
|
|
|
$clone->rootWidgetStylePattern = $pattern; |
66
|
|
|
return $clone; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function withCharPattern(IPattern $pattern): static |
70
|
|
|
{ |
71
|
|
|
$clone = clone $this; |
72
|
|
|
$clone->rootWidgetCharPattern = $pattern; |
73
|
|
|
return $clone; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function build(): IConfig |
77
|
|
|
{ |
78
|
|
|
return |
79
|
|
|
new Config( |
80
|
|
|
driver: $this->createDriver(), |
81
|
|
|
spinnerConfig: new SpinnerConfig( |
82
|
|
|
rootWidget: $this->rootWidget ?? $this->createRootWidget(), |
83
|
|
|
initialization: $this->defaults->getSpinnerSettings()->getInitializationOption(), |
84
|
|
|
widgets: $this->widgets ?? new ArrayObject([]), |
85
|
|
|
), |
86
|
|
|
loopConfig: new LoopConfig( |
87
|
|
|
runMode: $this->defaults->getRunMode(), |
88
|
|
|
autoStart: $this->defaults->getLoopSettings()->getAutoStartOption(), |
89
|
|
|
signalHandlersOption: $this->defaults->getLoopSettings()->getSignalHandlersOption(), |
90
|
|
|
), |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function createDriver(): IDriver |
95
|
|
|
{ |
96
|
|
|
return |
97
|
|
|
$this->driverBuilder |
98
|
|
|
->withOutput(new StreamOutput($this->defaults->getOutputStream())) |
99
|
|
|
->withTimer(new Timer()) |
100
|
|
|
->build(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function createRootWidget(): IWidgetComposite |
104
|
|
|
{ |
105
|
|
|
return |
106
|
|
|
$this->widgetBuilder |
107
|
|
|
->withWidgetRevolver( |
108
|
|
|
$this->widgetRevolverBuilder |
109
|
|
|
->withStyleRevolver( |
110
|
|
|
$this->frameRevolverBuilder |
111
|
|
|
->withPattern( |
112
|
|
|
$this->rootWidgetStylePattern |
113
|
|
|
?? $this->defaults->getRootWidgetSettings()->getStylePattern() |
114
|
|
|
?? $this->defaults->getStylePattern() |
115
|
|
|
) |
116
|
|
|
->build() |
117
|
|
|
) |
118
|
|
|
->withCharRevolver( |
119
|
|
|
$this->frameRevolverBuilder |
120
|
|
|
->withPattern( |
121
|
|
|
$this->rootWidgetCharPattern |
122
|
|
|
?? $this->defaults->getRootWidgetSettings()->getCharPattern() |
123
|
|
|
?? $this->defaults->getCharPattern() |
124
|
|
|
) |
125
|
|
|
->build() |
126
|
|
|
) |
127
|
|
|
->build() |
128
|
|
|
) |
129
|
|
|
->withLeadingSpacer($this->defaults->getRootWidgetSettings()->getLeadingSpacer()) |
130
|
|
|
->withTrailingSpacer($this->defaults->getRootWidgetSettings()->getTrailingSpacer()) |
131
|
|
|
->build(); |
132
|
|
|
// return |
133
|
|
|
// $this->widgetBuilder |
134
|
|
|
// ->withWidgetRevolver( |
135
|
|
|
// $this->widgetRevolverBuilder |
136
|
|
|
// ->withStyleRevolver( |
137
|
|
|
// RevolverFactory::createFrom( |
138
|
|
|
// $this->rootWidgetStylePattern |
139
|
|
|
// ?? $this->defaults->getRootWidgetSettings()->getStylePattern() |
140
|
|
|
// ?? $this->defaults->getStylePattern() |
141
|
|
|
// ) |
142
|
|
|
// ) |
143
|
|
|
// ->withCharRevolver( |
144
|
|
|
// RevolverFactory::createFrom( |
145
|
|
|
// $this->rootWidgetCharPattern |
146
|
|
|
// ?? $this->defaults->getRootWidgetSettings()->getCharPattern() |
147
|
|
|
// ?? $this->defaults->getCharPattern() |
148
|
|
|
// ) |
149
|
|
|
// ) |
150
|
|
|
// ->build() |
151
|
|
|
// ) |
152
|
|
|
// ->withLeadingSpacer($this->defaults->getRootWidgetSettings()->getLeadingSpacer()) |
153
|
|
|
// ->withTrailingSpacer($this->defaults->getRootWidgetSettings()->getTrailingSpacer()) |
154
|
|
|
// ->build(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|