|
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<ComponentEnum> */ |
|
21
|
|
|
private array $componentUpdates = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<ComponentEnum> */ |
|
24
|
|
|
private array $neededComponents = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @param array<int, RenderFragmentInterface> $componentProviders */ |
|
27
|
8 |
|
public function __construct( |
|
28
|
|
|
array $componentProviders |
|
29
|
|
|
) { |
|
30
|
8 |
|
$this->componentProviders = $componentProviders; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
2 |
|
public function addComponentUpdate(ComponentEnum $component): void |
|
35
|
|
|
{ |
|
36
|
2 |
|
if (!in_array($component, $this->componentUpdates)) { |
|
37
|
2 |
|
$this->componentUpdates[] = $component; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Adds the execute javascript after render. |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function loadComponentUpdates(GameControllerInterface $game): void |
|
45
|
|
|
{ |
|
46
|
2 |
|
foreach ($this->componentUpdates as $component) { |
|
47
|
2 |
|
$refreshInterval = $component->getRefreshIntervalInSeconds(); |
|
48
|
|
|
|
|
49
|
2 |
|
$game->addExecuteJS(sprintf( |
|
50
|
2 |
|
"updateComponent('navlet_%s', '/%s?%s=1&component=%s'%s);", |
|
51
|
2 |
|
$component->value, |
|
52
|
2 |
|
ModuleViewEnum::GAME->getPhpPage(), |
|
53
|
2 |
|
ShowComponent::VIEW_IDENTIFIER, |
|
54
|
2 |
|
$component->value, |
|
55
|
2 |
|
$refreshInterval === null ? '' : sprintf(', %d', $refreshInterval * 1000) |
|
56
|
2 |
|
), GameEnum::JS_EXECUTION_AFTER_RENDER); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function registerComponent(ComponentEnum $component): void |
|
61
|
|
|
{ |
|
62
|
2 |
|
if (!in_array($component, $this->neededComponents)) { |
|
63
|
2 |
|
$this->neededComponents[] = $component; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function loadRegisteredComponents( |
|
68
|
|
|
TwigPageInterface $twigPage, |
|
69
|
|
|
GameControllerInterface $game |
|
70
|
|
|
): void { |
|
71
|
|
|
|
|
72
|
2 |
|
foreach ($this->neededComponents as $component) { |
|
73
|
2 |
|
if (!array_key_exists($component->value, $this->componentProviders)) { |
|
74
|
1 |
|
throw new RuntimeException(sprintf('componentProvider with follwing id does not exist: %s', $component->value)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$componentProvider = $this->componentProviders[$component->value]; |
|
78
|
1 |
|
$componentProvider->render($game->getUser(), $twigPage, $game); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|