ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
JavaScriptBindable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
24use ReflectionFunction;
25use InvalidArgumentException;
26
32{
33 private ?Closure $on_load_code_binder = null;
34
38 public function withOnLoadCode(Closure $binder)
39 {
40 $this->checkBinder($binder);
41 $clone = clone $this;
42 $clone->on_load_code_binder = $binder;
43 return $clone;
44 }
45
49 public function withAdditionalOnLoadCode(Closure $binder)
50 {
51 $current_binder = $this->getOnLoadCode();
52 if ($current_binder === null) {
53 return $this->withOnLoadCode($binder);
54 }
55
56 $this->checkBinder($binder);
57 return $this->withOnLoadCode(fn ($id) => $current_binder($id) . "\n" . $binder($id));
58 }
59
63 public function getOnLoadCode(): ?Closure
64 {
65 return $this->on_load_code_binder;
66 }
67
71 private function checkBinder(Closure $binder): void
72 {
73 $refl = new ReflectionFunction($binder);
74 $args = array_map(fn ($arg) => $arg->name, $refl->getParameters());
75 if (array("id") !== $args) {
76 throw new InvalidArgumentException('Expected closure "$binder" to have exactly one argument "$id".');
77 }
78 }
79}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Interface to be extended by components that have the possibility to bind to Javascript.