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