ILIAS  release_8 Revision v8.23
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 {
33  private array $call_locations = [];
34 
35  public function current(): ScreenContext
36  {
37  return $this->getLast();
38  }
39 
40  public function push(ScreenContext $context): void
41  {
42  $this->claim(
43  $context,
44  $context->getUniqueContextIdentifier() === 'external'
45  ); // external can be claimed multiple times
46  }
47 
48  public function external(): ContextCollection
49  {
50  $this->claim($this->repo->external(), true);
51 
52  return $this;
53  }
54 
55  public function clear(): void
56  {
57  $this->call_locations = [];
58  $this->stack = [];
59  }
60 
61  protected function claim(ScreenContext $context, bool $silent = false): void
62  {
63  $this->checkCallLocation($context, $silent);
64 
65  if (!$silent && in_array($context, $this->stack)) {
66  throw new LogicException("A context can only be claimed once");
67  }
68  if (end($this->stack) instanceof ScreenContext) {
69  $context = $context->withAdditionalData($this->getLast()->getAdditionalData());
70  }
71 
72  parent::push($context);
73  }
74 
75  private function checkCallLocation(ScreenContext $context, bool $silent = false): void
76  {
77  $called_classes = array_filter(
78  debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
79  function ($item): bool {
80  if (!isset($item['class'])) {
81  return false;
82  }
83 
84  return (!in_array($item['class'], [CalledContexts::class, ContextCollection::class]));
85  }
86  );
87  array_walk(
88  $called_classes,
89  function (& $item): void {
90  $item = ($item['class'] ?? '') . ":" . ($item['line'] ?? '');
91  }
92  );
93 
94  $call_location = reset($called_classes);
95 
96  if (!$silent && isset($this->call_locations[$context->getUniqueContextIdentifier()])) {
97  $first_location = $this->call_locations[$context->getUniqueContextIdentifier()];
98  throw new LogicException("context '{$context->getUniqueContextIdentifier()}' already claimed in $first_location, second try in $call_location");
99  }
100  $this->call_locations[$context->getUniqueContextIdentifier()] = $call_location;
101  }
102 }
$context
Definition: webdav.php:29
checkCallLocation(ScreenContext $context, bool $silent=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
claim(ScreenContext $context, bool $silent=false)