ILIAS  release_8 Revision v8.24
SymbolDecoratorTrait.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;
31
38{
39 private ?Closure $symbol_decorator = null;
40
45 public function addSymbolDecorator(Closure $symbol_decorator): isGlobalScreenItem
46 {
47 if (!$this->checkClosure($symbol_decorator)) {
48 throw new LogicException('first argument and return type of closure must be type-hinted to \ILIAS\UI\Component\Symbol\Symbol');
49 }
50 if ($this->symbol_decorator instanceof Closure) {
51 $existing = $this->symbol_decorator;
52 $this->symbol_decorator = static function (Symbol $c) use ($symbol_decorator, $existing): Symbol {
53 $component = $existing($c);
54
55 return $symbol_decorator($component);
56 };
57 } else {
58 $this->symbol_decorator = $symbol_decorator;
59 }
60
61 return $this;
62 }
63
67 public function getSymbolDecorator(): ?Closure
68 {
69 return $this->symbol_decorator;
70 }
71
72 private function checkClosure(Closure $c): bool
73 {
74 try {
75 $r = new ReflectionFunction($c);
76 if (count($r->getParameters()) !== 1) {
77 return false;
78 }
79 $first_param_type = $r->getParameters()[0]->getType();
80 if ($first_param_type instanceof ReflectionType && $first_param_type->getName() !== Symbol::class) {
81 return false;
82 }
83 $return_type = $r->getReturnType();
84 if (!$return_type instanceof \ReflectionType) {
85 return false;
86 }
87 return $return_type->getName() === Symbol::class;
88 } catch (Throwable $i) {
89 return false;
90 }
91 }
92}
$c
Definition: cli.php:38
This describes a symbol.
Definition: Symbol.php:30
$i
Definition: metadata.php:41
addSymbolDecorator(Closure $symbol_decorator)