ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CalledContexts.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use LogicException;
25
30{
31 private array $call_locations = [];
32
33 public function current(): ScreenContext
34 {
35 return $this->getLast();
36 }
37
38 #[\Override]
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 #[\Override]
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 parent::push($context);
69 }
70
71 private function checkCallLocation(ScreenContext $context, bool $silent = false): void
72 {
73 $called_classes = array_filter(
74 debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
75 function ($item): bool {
76 if (!isset($item['class'])) {
77 return false;
78 }
79
80 return (!in_array($item['class'], [CalledContexts::class, ContextCollection::class]));
81 }
82 );
83 array_walk(
84 $called_classes,
85 function (& $item): void {
86 $item = ($item['class'] ?? '') . ":" . ($item['line'] ?? '');
87 }
88 );
89
90 $call_location = reset($called_classes);
91
92 if (!$silent && isset($this->call_locations[$context->getUniqueContextIdentifier()])) {
93 $first_location = $this->call_locations[$context->getUniqueContextIdentifier()];
94 throw new LogicException("context '{$context->getUniqueContextIdentifier()}' already claimed in $first_location, second try in $call_location");
95 }
96 $this->call_locations[$context->getUniqueContextIdentifier()] = $call_location;
97 }
98}
claim(ScreenContext $context, bool $silent=false)
checkCallLocation(ScreenContext $context, bool $silent=false)
$context
Definition: webdav.php:31