Elgg  Version master
Context.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\Http\Request as HttpRequest;
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 {
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(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 }
$context
Definition: add.php:8
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
Manages a global stack of strings for sharing information about the current execution context.
Definition: Context.php:27
contains(string $context)
Check if this context exists anywhere in the stack.
Definition: Context.php:109
__construct(HttpRequest $request)
Initialize the context from the request.
Definition: Context.php:36
push(string $context)
Push a context onto the top of the stack.
Definition: Context.php:64
toArray()
Get the entire context stack as an array (e.g.
Definition: Context.php:118
peek()
Get the most recently pushed context value.
Definition: Context.php:53
pop()
Removes and returns the top context string from the stack.
Definition: Context.php:73
fromArray(array $stack)
Overwrite the entire context stack from an array of strings.
Definition: Context.php:128
Elgg HTTP request.
Definition: Request.php:17
$request
Definition: livesearch.php:12