ILIAS  release_8 Revision v8.24
ComponentDecoratorTrait.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
25use LogicException;
26use ReflectionFunction;
27use ReflectionType;
28use Throwable;
30
37{
38 private ?Closure $component_decorator = null;
39
44 public function addComponentDecorator(Closure $component_decorator): isGlobalScreenItem
45 {
46 if (!$this->checkClosure($component_decorator)) {
47 throw new LogicException('first argument and return value of closure must be type-hinted to \ILIAS\UI\Component\Component');
48 }
49 if ($this->component_decorator instanceof Closure) {
50 $existing = $this->component_decorator;
51 $this->component_decorator = static function (Component $c) use ($component_decorator, $existing): Component {
52 $component = $existing($c);
53
54 return $component_decorator($component);
55 };
56 } else {
57 $this->component_decorator = $component_decorator;
58 }
59
60 return $this;
61 }
62
66 public function getComponentDecorator(): ?Closure
67 {
68 return $this->component_decorator;
69 }
70
71 private function checkClosure(Closure $c): bool
72 {
73 try {
74 $r = new ReflectionFunction($c);
75 if (count($r->getParameters()) !== 1) {
76 return false;
77 }
78 $first_param_type = $r->getParameters()[0]->getType();
79 if ($first_param_type instanceof ReflectionType && $first_param_type->getName() !== Component::class) {
80 return false;
81 }
82 $return_type = $r->getReturnType();
83 if ($return_type === null) {
84 return false;
85 }
86 if ($return_type->getName() !== Component::class) {
87 return false;
88 }
89
90 return true;
91 } catch (Throwable $i) {
92 return false;
93 }
94 }
95}
$c
Definition: cli.php:38
A component is the most general form of an entity in the UI.
Definition: Component.php:28
$i
Definition: metadata.php:41
addComponentDecorator(Closure $component_decorator)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...