‪TYPO3CMS  9.5
Context.php
Go to the documentation of this file.
1 <?php
2 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 
21 
49 {
53  protected ‪$aspects = [];
54 
60  public function ‪__construct(array $defaultAspects = [])
61  {
62  foreach ($defaultAspects as $name => $defaultAspect) {
63  if ($defaultAspect instanceof ‪AspectInterface) {
64  $this->aspects[$name] = $defaultAspect;
65  }
66  }
67  // Ensure the default aspects are set, this is mostly necessary for tests to not set up everything
68  if (!$this->‪hasAspect('date')) {
69  $time = ‪$GLOBALS['EXEC_TIME'] ?? time();
70  $this->‪setAspect('date', new ‪DateTimeAspect(new \DateTimeImmutable('@' . $time)));
71  }
72  if (!$this->‪hasAspect('visibility')) {
73  $this->‪setAspect('visibility', new ‪VisibilityAspect());
74  }
75  if (!$this->‪hasAspect('backend.user')) {
76  $this->‪setAspect('backend.user', new ‪UserAspect());
77  }
78  if (!$this->‪hasAspect('frontend.user')) {
79  $this->‪setAspect('frontend.user', new ‪UserAspect());
80  }
81  if (!$this->‪hasAspect('workspace')) {
82  $this->‪setAspect('workspace', new ‪WorkspaceAspect());
83  }
84  if (!$this->‪hasAspect('language')) {
85  $this->‪setAspect('language', new ‪LanguageAspect());
86  }
87  }
88 
95  public function ‪hasAspect(string $name): bool
96  {
97  return isset($this->aspects[$name]);
98  }
99 
107  public function ‪getAspect(string $name): ‪AspectInterface
108  {
109  if (!$this->‪hasAspect($name)) {
110  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777641);
111  }
112  return $this->aspects[$name];
113  }
114 
124  public function ‪getPropertyFromAspect(string $name, string $property, $default = null)
125  {
126  if (!$this->‪hasAspect($name)) {
127  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777868);
128  }
129  try {
130  return $this->aspects[$name]->get($property);
132  return $default;
133  }
134  }
135 
142  public function ‪setAspect(string $name, ‪AspectInterface $aspect): void
143  {
144  $this->aspects[$name] = $aspect;
145  }
146 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:29
‪TYPO3\CMS\Core\Context\Context\getPropertyFromAspect
‪mixed null getPropertyFromAspect(string $name, string $property, $default=null)
Definition: Context.php:123
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:29
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:3
‪TYPO3\CMS\Core\Context\Context\$aspects
‪AspectInterface[] $aspects
Definition: Context.php:52
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Core\Context\Context\setAspect
‪setAspect(string $name, AspectInterface $aspect)
Definition: Context.php:141
‪TYPO3\CMS\Core\Context\Context\__construct
‪__construct(array $defaultAspects=[])
Definition: Context.php:59
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:25
‪TYPO3\CMS\Core\Context\Context\hasAspect
‪bool hasAspect(string $name)
Definition: Context.php:94
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:55
‪TYPO3\CMS\Core\Context\Context\getAspect
‪AspectInterface getAspect(string $name)
Definition: Context.php:106
‪TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
Definition: AspectNotFoundException.php:24
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:33
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:24