‪TYPO3CMS  ‪main
Context.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
54 {
58  protected array ‪$aspects = [];
59 
63  public function ‪__construct(array $defaultAspects = [])
64  {
65  foreach ($defaultAspects as $name => $defaultAspect) {
66  if ($defaultAspect instanceof ‪AspectInterface) {
67  $this->aspects[$name] = $defaultAspect;
68  }
69  }
70  }
71 
75  public function ‪hasAspect(string $name): bool
76  {
77  return match ($name) {
78  'date', 'visibility', 'backend.user', 'frontend.user', 'workspace', 'language' => true,
79  default => isset($this->aspects[$name]),
80  };
81  }
82 
88  public function ‪getAspect(string $name): ‪AspectInterface
89  {
90  if (!isset($this->aspects[$name])) {
91  // Ensure the default aspects are available, this is mostly necessary for tests to not set up everything
92  switch ($name) {
93  case 'date':
94  $this->‪setAspect('date', new ‪DateTimeAspect(new \DateTimeImmutable('@' . ‪$GLOBALS['EXEC_TIME'])));
95  break;
96  case 'visibility':
97  $this->‪setAspect('visibility', new ‪VisibilityAspect());
98  break;
99  case 'backend.user':
100  $this->‪setAspect('backend.user', new ‪UserAspect());
101  break;
102  case 'frontend.user':
103  $this->‪setAspect('frontend.user', new ‪UserAspect());
104  break;
105  case 'workspace':
106  $this->‪setAspect('workspace', new ‪WorkspaceAspect());
107  break;
108  case 'language':
109  $this->‪setAspect('language', new ‪LanguageAspect());
110  break;
111  default:
112  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777641);
113  }
114  }
115  return $this->aspects[$name];
116  }
117 
123  public function ‪getPropertyFromAspect(string $name, string $property, mixed $default = null): mixed
124  {
125  if (!$this->‪hasAspect($name)) {
126  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777868);
127  }
128  try {
129  return $this->‪getAspect($name)->get($property);
131  return $default;
132  }
133  }
134 
138  public function ‪setAspect(string $name, ‪AspectInterface $aspect): void
139  {
140  $this->aspects[$name] = $aspect;
141  }
142 
150  public function ‪unsetAspect(string $name): void
151  {
152  unset($this->aspects[$name]);
153  }
154 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Context\Context\getAspect
‪getAspect(string $name)
Definition: Context.php:88
‪TYPO3\CMS\Core\Context\Context\unsetAspect
‪unsetAspect(string $name)
Definition: Context.php:150
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:18
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Context\Context\setAspect
‪setAspect(string $name, AspectInterface $aspect)
Definition: Context.php:138
‪TYPO3\CMS\Core\Context\Context\getPropertyFromAspect
‪getPropertyFromAspect(string $name, string $property, mixed $default=null)
Definition: Context.php:123
‪TYPO3\CMS\Core\Context\Context\__construct
‪__construct(array $defaultAspects=[])
Definition: Context.php:63
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\Context\hasAspect
‪hasAspect(string $name)
Definition: Context.php:75
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
Definition: AspectNotFoundException.php:25
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Context\Context\$aspects
‪array $aspects
Definition: Context.php:58
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:25