ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
CalledContexts.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use LogicException;
26 
31 final class CalledContexts extends ContextCollection
32 {
36  private $call_locations = [];
37 
38  public function current() : ScreenContext
39  {
40  return $this->getLast();
41  }
42 
43  public function push(ScreenContext $context) : void
44  {
45  $this->claim(
46  $context,
47  $context->getUniqueContextIdentifier() === 'external'
48  ); // external can be claimed multiple times
49  }
50 
51  public function external() : ContextCollection
52  {
53  $this->claim($this->repo->external(), true);
54 
55  return $this;
56  }
57 
58  public function clear() : void
59  {
60  $this->call_locations = [];
61  $this->stack = [];
62  }
63 
64  protected function claim(ScreenContext $context, bool $silent = false) : void
65  {
66  $this->checkCallLocation($context, $silent);
67 
68  if (!$silent && in_array($context, $this->stack)) {
69  throw new LogicException("A context can only be claimed once");
70  }
71  if (end($this->stack) instanceof ScreenContext) {
72  $context = $context->withAdditionalData($this->getLast()->getAdditionalData());
73  }
74 
75  parent::push($context);
76  }
77 
78  private function checkCallLocation(ScreenContext $context, bool $silent = false) : void
79  {
80  $called_classes = array_filter(
81  debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
82  function ($item) : bool {
83  if (!isset($item['class'])) {
84  return false;
85  }
86 
87  return (!in_array($item['class'], [CalledContexts::class, ContextCollection::class]));
88  }
89  );
90  array_walk(
91  $called_classes,
92  function (&$item) : void {
93  $item = ($item['class'] ?? '') . ":" . ($item['line'] ?? '');
94  }
95  );
96 
97  $call_location = reset($called_classes);
98 
99  if (!$silent && isset($this->call_locations[$context->getUniqueContextIdentifier()])) {
100  $first_location = $this->call_locations[$context->getUniqueContextIdentifier()];
101  throw new LogicException("context '{$context->getUniqueContextIdentifier()}' already claimed in $first_location, second try in $call_location");
102  }
103  $this->call_locations[$context->getUniqueContextIdentifier()] = $call_location;
104  }
105 }
$context
Definition: webdav.php:26
checkCallLocation(ScreenContext $context, bool $silent=false)
claim(ScreenContext $context, bool $silent=false)