‪TYPO3CMS  10.4
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('date', new ‪DateTimeAspect(new \DateTimeImmutable('@' . ‪$GLOBALS['EXEC_TIME'])));
110  break;
111  case 'visibility':
112  $this->‪setAspect('visibility', new ‪VisibilityAspect());
113  break;
114  case 'backend.user':
115  $this->‪setAspect('backend.user', new ‪UserAspect());
116  break;
117  case 'frontend.user':
118  $this->‪setAspect('frontend.user', new ‪UserAspect());
119  break;
120  case 'workspace':
121  $this->‪setAspect('workspace', new ‪WorkspaceAspect());
122  break;
123  case 'language':
124  $this->‪setAspect('language', new ‪LanguageAspect());
125  break;
126  case 'typoscript':
127  $this->‪setAspect('typoscript', new ‪TypoScriptAspect());
128  break;
129  default:
130  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777641);
131  }
132  }
133  return $this->aspects[$name];
134  }
135 
145  public function ‪getPropertyFromAspect(string $name, string $property, $default = null)
146  {
147  if (!$this->‪hasAspect($name)) {
148  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777868);
149  }
150  try {
151  return $this->‪getAspect($name)->‪get($property);
153  return $default;
154  }
155  }
156 
163  public function ‪setAspect(string $name, ‪AspectInterface $aspect): void
164  {
165  $this->aspects[$name] = $aspect;
166  }
167 }
‪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:144
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Context\AspectInterface\get
‪mixed get(string $name)
‪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:162
‪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:26
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:26