‪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 ‪hasAspect(string $name): bool
64  {
65  return match ($name) {
66  'date', 'visibility', 'backend.user', 'frontend.user', 'workspace', 'language' => true,
67  default => isset($this->aspects[$name]),
68  };
69  }
70 
76  public function ‪getAspect(string $name): ‪AspectInterface
77  {
78  if (!isset($this->aspects[$name])) {
79  // Ensure the default aspects are available, this is mostly necessary for tests to not set up everything
80  switch ($name) {
81  case 'date':
82  $this->‪setAspect(
83  'date',
85  (new \DateTimeImmutable())->setTimestamp(‪$GLOBALS['EXEC_TIME'])
86  )
87  );
88  break;
89  case 'visibility':
90  $this->‪setAspect('visibility', new ‪VisibilityAspect());
91  break;
92  case 'backend.user':
93  $this->‪setAspect('backend.user', new ‪UserAspect());
94  break;
95  case 'frontend.user':
96  $this->‪setAspect('frontend.user', new ‪UserAspect());
97  break;
98  case 'workspace':
99  $this->‪setAspect('workspace', new ‪WorkspaceAspect());
100  break;
101  case 'language':
102  $this->‪setAspect('language', new ‪LanguageAspect());
103  break;
104  default:
105  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777641);
106  }
107  }
108  return $this->aspects[$name];
109  }
110 
116  public function ‪getPropertyFromAspect(string $name, string $property, mixed $default = null): mixed
117  {
118  if (!$this->‪hasAspect($name)) {
119  throw new ‪AspectNotFoundException('No aspect named "' . $name . '" found.', 1527777868);
120  }
121  try {
122  return $this->‪getAspect($name)->get($property);
124  return $default;
125  }
126  }
127 
131  public function ‪setAspect(string $name, ‪AspectInterface $aspect): void
132  {
133  $this->aspects[$name] = $aspect;
134  }
135 
143  public function ‪unsetAspect(string $name): void
144  {
145  unset($this->aspects[$name]);
146  }
147 }
‪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:76
‪TYPO3\CMS\Core\Context\Context\unsetAspect
‪unsetAspect(string $name)
Definition: Context.php:143
‪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:131
‪TYPO3\CMS\Core\Context\Context\getPropertyFromAspect
‪getPropertyFromAspect(string $name, string $property, mixed $default=null)
Definition: Context.php:116
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\Context\hasAspect
‪hasAspect(string $name)
Definition: Context.php:63
‪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