‪TYPO3CMS  11.5
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 
53 {
57  protected ‪$aspects = [];
58 
64  public function ‪__construct(array $defaultAspects = [])
65  {
66  foreach ($defaultAspects as $name => $defaultAspect) {
67  if ($defaultAspect instanceof ‪AspectInterface) {
68  $this->aspects[$name] = $defaultAspect;
69  }
70  }
71  }
72 
79  public function ‪hasAspect(string $name): bool
80  {
81  switch ($name) {
82  // Ensure the default aspects are available, this is mostly necessary for tests to not set up everything
83  case 'date':
84  case 'visibility':
85  case 'backend.user':
86  case 'frontend.user':
87  case 'workspace':
88  case 'language':
89  case 'typoscript':
90  return true;
91  default:
92  return isset($this->aspects[$name]);
93  }
94  }
95 
103  public function ‪getAspect(string $name): ‪AspectInterface
104  {
105  if (!isset($this->aspects[$name])) {
106  // Ensure the default aspects are available, this is mostly necessary for tests to not set up everything
107  switch ($name) {
108  case 'date':
109  $this->‪setAspect(
110  'date',
111  new ‪DateTimeAspect(
112  (new \DateTimeImmutable())->setTimestamp(‪$GLOBALS['EXEC_TIME'])
113  )
114  );
115  break;
116  case 'visibility':
117  $this->‪setAspect('visibility', new ‪VisibilityAspect());
118  break;
119  case 'backend.user':
120  $this->‪setAspect('backend.user', new ‪UserAspect());
121  break;
122  case 'frontend.user':
123  $this->‪setAspect('frontend.user', new ‪UserAspect());
124  break;
125  case 'workspace':
126  $this->‪setAspect('workspace', new ‪WorkspaceAspect());
127  break;
128  case 'language':
129  $this->‪setAspect('language', new ‪LanguageAspect());
130  break;
131  case 'typoscript':
132  $this->‪setAspect('typoscript', new ‪TypoScriptAspect());
133  break;
134  default:
135  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777641);
136  }
137  }
138  return $this->aspects[$name];
139  }
140 
150  public function ‪getPropertyFromAspect(string $name, string $property, $default = null)
151  {
152  if (!$this->‪hasAspect($name)) {
153  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777868);
154  }
155  try {
156  return $this->‪getAspect($name)->‪get($property);
158  return $default;
159  }
160  }
161 
168  public function ‪setAspect(string $name, ‪AspectInterface $aspect): void
169  {
170  $this->aspects[$name] = $aspect;
171  }
172 
180  public function ‪unsetAspect(string $name): void
181  {
182  unset($this->aspects[$name]);
183  }
184 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Context\Context\getPropertyFromAspect
‪mixed null getPropertyFromAspect(string $name, string $property, $default=null)
Definition: Context.php:149
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Context\AspectInterface\get
‪mixed get(string $name)
‪TYPO3\CMS\Core\Context\Context\unsetAspect
‪unsetAspect(string $name)
Definition: Context.php:179
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:18
‪TYPO3\CMS\Core\Context\Context\$aspects
‪AspectInterface[] $aspects
Definition: Context.php:56
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Context\Context\setAspect
‪setAspect(string $name, AspectInterface $aspect)
Definition: Context.php:167
‪TYPO3\CMS\Core\Context\Context\__construct
‪__construct(array $defaultAspects=[])
Definition: Context.php:63
‪TYPO3\CMS\Core\Context\TypoScriptAspect
Definition: TypoScriptAspect.php:29
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\Context\hasAspect
‪bool hasAspect(string $name)
Definition: Context.php:78
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Core\Context\Context\getAspect
‪AspectInterface getAspect(string $name)
Definition: Context.php:102
‪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\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:25