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