Elgg  Version 3.0
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() {
54  $last = end($this->stack);
55  return ($last === false) ? null : $last;
56  }
57 
64  public function push($context) {
65  $this->stack[] = "$context";
66  }
67 
73  public function pop() {
74  return array_pop($this->stack);
75  }
76 
83  public function set($context) {
84  $context = trim($context);
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($context) {
110  return in_array($context, $this->stack);
111  }
112 
118  public function toArray() {
119  return $this->stack;
120  }
121 
128  public function fromArray(array $stack) {
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
contains($context)
Check if this context exists anywhere in the stack.
Definition: Context.php:109
push($context)
Push a context onto the top of the stack.
Definition: Context.php:64
$request
Page handler for autocomplete endpoint.
Definition: livesearch.php:9
toArray()
Get the entire context stack as an array (e.g.
Definition: Context.php:118
Configuration exception.
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