1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Game\Lib\Component; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Stu\Component\Game\GameEnum; |
9
|
|
|
use Stu\Component\Game\ModuleViewEnum; |
10
|
|
|
use Stu\Module\Control\GameControllerInterface; |
11
|
|
|
use Stu\Module\Control\Render\Fragments\RenderFragmentInterface; |
12
|
|
|
use Stu\Module\Game\View\ShowComponent\ShowComponent; |
13
|
|
|
use Stu\Module\Twig\TwigPageInterface; |
14
|
|
|
|
15
|
|
|
final class ComponentLoader implements ComponentLoaderInterface |
16
|
|
|
{ |
17
|
|
|
/** @var array<int, RenderFragmentInterface> */ |
18
|
|
|
private array $componentProviders; |
19
|
|
|
|
20
|
|
|
/** @var array<string, ComponentUpdate> */ |
21
|
|
|
private array $componentUpdates = []; |
22
|
|
|
|
23
|
|
|
/** @var array<ComponentEnum> */ |
24
|
|
|
private array $neededComponents = []; |
25
|
|
|
|
26
|
|
|
/** @param array<int, RenderFragmentInterface> $componentProviders */ |
27
|
10 |
|
public function __construct( |
28
|
|
|
array $componentProviders |
29
|
|
|
) { |
30
|
10 |
|
$this->componentProviders = $componentProviders; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
4 |
|
public function addComponentUpdate(ComponentEnum $component, bool $isInstantUpdate = true): void |
35
|
|
|
{ |
36
|
4 |
|
if (!array_key_exists($component->value, $this->componentUpdates)) { |
37
|
4 |
|
$this->componentUpdates[$component->value] = new ComponentUpdate($component, $isInstantUpdate); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Adds the execute javascript after render. |
43
|
|
|
*/ |
44
|
4 |
|
public function loadComponentUpdates(GameControllerInterface $game): void |
45
|
|
|
{ |
46
|
4 |
|
foreach ($this->componentUpdates as $update) { |
47
|
|
|
|
48
|
4 |
|
$component = $update->getComponent(); |
49
|
4 |
|
$isInstantUpdate = $update->isInstantUpdate(); |
50
|
|
|
|
51
|
4 |
|
if ($isInstantUpdate) { |
52
|
2 |
|
$this->addExecuteJs( |
53
|
2 |
|
$component->value, |
54
|
2 |
|
'', |
55
|
2 |
|
$game |
56
|
2 |
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$refreshInterval = $component->getRefreshIntervalInSeconds(); |
60
|
|
|
|
61
|
4 |
|
if (!$isInstantUpdate || $refreshInterval !== null) { |
62
|
3 |
|
$this->addExecuteJs( |
63
|
3 |
|
$component->value, |
64
|
3 |
|
$refreshInterval === null ? '' : sprintf(', %d', $refreshInterval * 1000), |
65
|
3 |
|
$game |
66
|
3 |
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
private function addExecuteJs(string $componentValue, string $refreshParam, GameControllerInterface $game): void |
72
|
|
|
{ |
73
|
4 |
|
$game->addExecuteJS(sprintf( |
74
|
4 |
|
"updateComponent('navlet_%s', '/%s?%s=1&component=%s'%s);", |
75
|
4 |
|
$componentValue, |
76
|
4 |
|
ModuleViewEnum::GAME->getPhpPage(), |
77
|
4 |
|
ShowComponent::VIEW_IDENTIFIER, |
78
|
4 |
|
$componentValue, |
79
|
4 |
|
$refreshParam |
80
|
4 |
|
), GameEnum::JS_EXECUTION_AFTER_RENDER); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function registerComponent(ComponentEnum $component): void |
84
|
|
|
{ |
85
|
2 |
|
if (!in_array($component, $this->neededComponents)) { |
86
|
2 |
|
$this->neededComponents[] = $component; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function loadRegisteredComponents( |
91
|
|
|
TwigPageInterface $twigPage, |
92
|
|
|
GameControllerInterface $game |
93
|
|
|
): void { |
94
|
|
|
|
95
|
2 |
|
foreach ($this->neededComponents as $component) { |
96
|
2 |
|
if (!array_key_exists($component->value, $this->componentProviders)) { |
97
|
1 |
|
throw new RuntimeException(sprintf('componentProvider with follwing id does not exist: %s', $component->value)); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$componentProvider = $this->componentProviders[$component->value]; |
101
|
1 |
|
$componentProvider->render($game->getUser(), $twigPage, $game); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|