Elgg  Version 6.1
Context.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
27 final class Context {
28 
29  private $stack = [];
30 
36  public function __construct(HttpRequest $request) {
37  // don't do this for *_handler.php, etc.
38  if (basename($request->server->get('SCRIPT_FILENAME') ?? '') === 'index.php') {
39  $context = $request->getFirstUrlSegment();
40  if (!$context) {
41  $context = 'main';
42  }
43 
44  $this->stack = [$context];
45  }
46  }
47 
53  public function peek(): ?string {
54  $last = end($this->stack);
55  return ($last === false) ? null : $last;
56  }
57 
64  public function push(string $context): void {
65  $this->stack[] = "$context";
66  }
67 
73  public function pop(): ?string {
74  return array_pop($this->stack);
75  }
76 
83  public function set(string $context): bool {
85 
86  if (empty($context)) {
87  return false;
88  }
89 
90  $context = strtolower($context);
91 
92  $this->pop();
93  $this->push($context);
94 
95  return true;
96  }
97 
109  public function contains(string $context): bool {
110  return in_array($context, $this->stack);
111  }
112 
118  public function toArray(): array {
119  return $this->stack;
120  }
121 
128  public function fromArray(array $stack): void {
129  $this->stack = array_map('strval', $stack);
130  }
131 }
peek()
Get the most recently pushed context value.
Definition: Context.php:53
$context
Definition: add.php:8
pop()
Removes and returns the top context string from the stack.
Definition: Context.php:73
$request
Definition: livesearch.php:12
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
toArray()
Get the entire context stack as an array (e.g.
Definition: Context.php:118
contains(string $context)
Check if this context exists anywhere in the stack.
Definition: Context.php:109
push(string $context)
Push a context onto the top of the stack.
Definition: Context.php:64
fromArray(array $stack)
Overwrite the entire context stack from an array of strings.
Definition: Context.php:128
__construct(HttpRequest $request)
Initialize the context from the request.
Definition: Context.php:36
Manages a global stack of strings for sharing information about the current execution context...
Definition: Context.php:27