ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
29 public function render(OfComponent ...$components): string
30 {
31 $component_lookup = array_flip(
32 array_map(
33 fn($c) => $c->getComponentName(),
35 )
36 );
37
38 return
39 $this->renderHeader() .
40 join("\n", array_map(
41 fn($c) => $this->renderComponent($component_lookup, $c),
43 )) .
44 $this->renderEntryPointsSection($component_lookup, ...$components);
45 }
46
47 protected function renderHeader(): string
48 {
49 return <<<PHP
50<?php
51
68require_once(__DIR__ . "/../vendor/composer/vendor/autoload.php");
69
70function entry_point(string \$name): int
71{
72 \$null_dic = new ILIAS\Component\Dependencies\NullDIC();
73 \$implement = new Pimple\Container();
74 \$contribute = new Pimple\Container();
75 \$provide = new Pimple\Container();
76
77
78PHP;
79 }
80
81 protected function renderComponent(array $component_lookup, OfComponent $component): string
82 {
83 $me = $component_lookup[$component->getComponentName()];
84 $use = $this->renderUse($component_lookup, $component);
85 $seek = $this->renderSeek($component_lookup, $component);
86 $pull = $this->renderPull($component_lookup, $component);
87 return <<<PHP
88
89 \$component_$me = new {$component->getComponentName()}();
91 \$implement[$me] = new ILIAS\Component\Dependencies\RenamingDIC(new Pimple\Container());
92 \$use = new Pimple\Container();{$use}
93 \$contribute[$me] = new ILIAS\Component\Dependencies\RenamingDIC(new Pimple\Container());
94 \$seek = new Pimple\Container();{$seek}
95 \$provide[$me] = new Pimple\Container();
96 \$pull = new Pimple\Container();{$pull}
97 \$internal = new Pimple\Container();
98
99 \$component_{$me}->init(\$null_dic, \$implement[$me], \$use, \$contribute[$me], \$seek, \$provide[$me], \$pull, \$internal);
100
101PHP;
102 }
103
104 protected function renderUse(array $component_lookup, OfComponent $component): string
105 {
106 $use = "";
107 foreach ($component->getInDependenciesOf(InType::USE) as $in) {
108 $r = $in->getResolvedBy()[0];
109 $p = $r->aux["position"];
110 $o = $component_lookup[$r->getComponent()->getComponentName()];
111 $use .= "\n" . <<<PHP
112 \$use[{$in->getName()}::class] = fn() => \$implement[{$o}][{$r->getName()}::class . "_{$p}"];
113PHP;
114 }
115 return $use;
116 }
117
118 protected function renderSeek(array $component_lookup, OfComponent $component): string
119 {
120 $seek = "";
121 foreach ($component->getInDependenciesOf(InType::SEEK) as $in) {
122 $rs = $in->getResolvedBy();
123 $u = [];
124 $a = "";
125 foreach ($rs as $r) {
126 $p = $r->aux["position"];
127 $o = $component_lookup[$r->getComponent()->getComponentName()];
128 $u[] = "\$contribute_{$o}";
129 $a .= "\n" . <<<PHP
130 \$contribute[$o][{$r->getName()}::class . "_{$p}"],
131PHP;
132 }
133 $u = join(", ", array_unique($u));
134 $seek .= "\n" . <<<PHP
135 \$seek[{$in->getName()}::class] = fn() => [{$a}
136 ];
137PHP;
138 }
139 return $seek;
140 }
142 protected function renderPull(array $component_lookup, OfComponent $component): string
143 {
144 $pull = "";
145 foreach ($component->getInDependenciesOf(InType::PULL) as $in) {
146 $r = $in->getResolvedBy()[0];
147 $o = $component_lookup[$r->getComponent()->getComponentName()];
148 $pull .= "\n" . <<<PHP
149 \$pull[{$in->getName()}::class] = fn() => \$provide[{$o}][{$r->getName()}::class];
150PHP;
151 }
152 return $pull;
153 }
154
155 protected function renderEntryPointsSection(array $component_lookup, OfComponent ...$components): string
156 {
157 $entry_points = "";
158 foreach ($components as $component) {
159 $p = $component_lookup[$component->getComponentName()];
160 $entry_points .= $this->renderEntryPoints($p, $component);
161 }
162 return <<<PHP
163
165 \$entry_points = [{$entry_points}
166 ];
167
168 if (!isset(\$entry_points[\$name])) {
169 throw new \\LogicException("Unknown entry point: \$name.");
170 }
171
172 return \$entry_points[\$name]()->enter();
173}
174
175PHP;
176 }
177
178 protected function renderEntryPoints(int $me, OfComponent $component): string
179 {
180 $entry_points = "";
181 foreach ($component->getOutDependenciesOf(OutType::CONTRIBUTE) as $out) {
182 if ($out->getName() !== \ILIAS\Component\EntryPoint::class) {
183 continue;
184 }
185 $p = $out->aux["position"];
186 $n = str_replace("\"", "\\\"", $out->aux["entry_point_name"]);
187 $entry_points .= "\n" . <<<PHP
188 "$n" => fn() => \$contribute[{$me}][ILIAS\Component\EntryPoint::class . "_{$p}"],
189PHP;
190 }
191
192 return $entry_points;
193 PHP;
194 }
195}
$out
Definition: buildRTE.php:24
$components
An object that looks like a Dependency Injection Container but actually does nothing.
Definition: NullDIC.php:28
A wrapper around another DIC that superficially adds a _# and passes them to an underlying DIC.
Definition: RenamingDIC.php:29
This takes a (hopefully resolved...) dependency tree and renders it in PHP to be used for initialisat...
Definition: Renderer.php:28
renderPull(array $component_lookup, OfComponent $component)
Definition: Renderer.php:128
renderSeek(array $component_lookup, OfComponent $component)
Definition: Renderer.php:104
renderEntryPoints(int $me, OfComponent $component)
Definition: Renderer.php:164
renderUse(array $component_lookup, OfComponent $component)
Definition: Renderer.php:90
render(OfComponent ... $components)
Definition: Renderer.php:29
renderComponent(array $component_lookup, OfComponent $component)
Definition: Renderer.php:67
renderEntryPointsSection(array $component_lookup, OfComponent ... $components)
Definition: Renderer.php:141
$c
Definition: deliver.php:25
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
entry_point(string $name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: result1.php:21