ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
SymbolDecoratorTrait.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\GlobalScreen\Scope;
22 
23 use Closure;
25 use LogicException;
27 use ReflectionType;
28 use 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 === null) {
85  return false;
86  }
87  if ($return_type->getName() !== Symbol::class) {
88  return false;
89  }
90 
91  return true;
92  } catch (Throwable $i) {
93  return false;
94  }
95  }
96 }
Interface isGlobalScreenItem.
This describes a symbol.
Definition: Symbol.php:29
addSymbolDecorator(Closure $symbol_decorator)
$r