ILIAS  trunk Revision v11.0_alpha-1861-g09f3d197f78
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 class Renderer
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),
42  $components
43  )) .
44  $this->renderEntryPointsSection($component_lookup, ...$components);
45  }
46 
47  protected function renderHeader(): string
48  {
49  return <<<PHP
50 <?php
51 
68 require_once(__DIR__ . "/../vendor/composer/vendor/autoload.php");
69 
70 function 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 
78 PHP;
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()}();
90 
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 
101 PHP;
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}"];
113 PHP;
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}"],
131 PHP;
132  }
133  $u = join(", ", array_unique($u));
134  $seek .= "\n" . <<<PHP
135  \$seek[{$in->getName()}::class] = fn() => [{$a}
136  ];
137 PHP;
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];
150 PHP;
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 
175 PHP;
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}"],
189 PHP;
190  }
191 
192  return $entry_points;
193  PHP;
194  }
195 }
renderSeek(array $component_lookup, OfComponent $component)
Definition: Renderer.php:104
renderComponent(array $component_lookup, OfComponent $component)
Definition: Renderer.php:67
render(OfComponent ... $components)
Definition: Renderer.php:29
renderEntryPoints(int $me, OfComponent $component)
Definition: Renderer.php:164
$c
Definition: deliver.php:25
$components
An object that looks like a Dependency Injection Container but actually does nothing.
Definition: NullDIC.php:27
renderPull(array $component_lookup, OfComponent $component)
Definition: Renderer.php:128
$out
Definition: buildRTE.php:24
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
renderUse(array $component_lookup, OfComponent $component)
Definition: Renderer.php:90
renderEntryPointsSection(array $component_lookup, OfComponent ... $components)
Definition: Renderer.php:141
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
$r