1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core\Factory\A; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Core\A\ASpinner; |
8
|
|
|
use AlecRabbit\Spinner\Core\Config\ConfigBuilder; |
9
|
|
|
use AlecRabbit\Spinner\Core\Config\Contract\IConfig; |
10
|
|
|
use AlecRabbit\Spinner\Core\Config\Contract\IConfigBuilder; |
11
|
|
|
use AlecRabbit\Spinner\Core\Config\Contract\IConfigBuilderGetter; |
12
|
|
|
use AlecRabbit\Spinner\Core\Contract\ILoopHelper; |
13
|
|
|
use AlecRabbit\Spinner\Core\Contract\ISpinner; |
14
|
|
|
use AlecRabbit\Spinner\Core\Factory\Contract\ISpinnerFactory; |
15
|
|
|
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetComposite; |
16
|
|
|
use AlecRabbit\Spinner\Exception\DomainException; |
17
|
|
|
use AlecRabbit\Spinner\Facade; |
18
|
|
|
|
19
|
|
|
abstract class ASpinnerFactory extends ADefaultsAwareClass implements |
20
|
|
|
ISpinnerFactory, |
21
|
|
|
IConfigBuilderGetter |
22
|
|
|
{ |
23
|
|
|
protected static IConfig $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws DomainException |
27
|
|
|
*/ |
28
|
|
|
public static function createSpinner(IConfig $config = null): ISpinner |
29
|
|
|
{ |
30
|
|
|
self::$config = self::refineConfig($config); |
31
|
|
|
|
32
|
|
|
$spinner = self::doCreateSpinner(self::$config); |
33
|
|
|
|
34
|
|
|
self::addWidgets($spinner); |
35
|
|
|
|
36
|
|
|
return |
37
|
|
|
self::initializeSpinner($spinner); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected static function refineConfig(?IConfig $config): IConfig |
41
|
|
|
{ |
42
|
|
|
return |
43
|
|
|
$config ?? self::getConfigBuilder()->build(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getConfigBuilder(): IConfigBuilder |
47
|
|
|
{ |
48
|
|
|
return |
49
|
|
|
new ConfigBuilder(self::getDefaults()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private static function doCreateSpinner(IConfig $config): ISpinner |
53
|
|
|
{ |
54
|
|
|
return |
55
|
|
|
new class ( |
56
|
|
|
$config->getDriver(), |
57
|
|
|
$config->getSpinnerConfig()->getRootWidget(), |
58
|
|
|
) extends ASpinner { |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected static function addWidgets(ISpinner $spinner): void |
63
|
|
|
{ |
64
|
|
|
/** @var IWidgetComposite $widget */ |
65
|
|
|
foreach (self::$config->getSpinnerConfig()->getWidgets() as $widget) { |
66
|
|
|
if ($widget instanceof IWidgetComposite) { |
67
|
|
|
$spinner->add($widget); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws DomainException |
74
|
|
|
*/ |
75
|
|
|
protected static function initializeSpinner(ISpinner $spinner): ISpinner |
76
|
|
|
{ |
77
|
|
|
/** @var ILoopHelper $loopHelper */ |
78
|
|
|
$loopHelper = Facade::getLoopHelper(); |
79
|
|
|
|
80
|
|
|
if (self::$config->getLoopConfig()->isAsynchronous()) { |
81
|
|
|
$loopHelper::attach($spinner); |
82
|
|
|
|
83
|
|
|
if (self::$config->getLoopConfig()->areSignalHandlersEnabled()) { |
84
|
|
|
$loopHelper::setSignalHandlers($spinner, self::$config->getLoopConfig()->getSignalHandlers()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (self::$config->getLoopConfig()->isAutoStartEnabled()) { |
88
|
|
|
$loopHelper::autoStart(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (self::$config->getSpinnerConfig()->isInitializationEnabled() |
93
|
|
|
|| self::$config->getLoopConfig()->isAsynchronous()) { |
94
|
|
|
$spinner->initialize(); |
95
|
|
|
} |
96
|
|
|
return $spinner; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|