‪TYPO3CMS  ‪main
Scope.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 
24 
29 final class ‪Scope implements \Stringable, \JsonSerializable
30 {
34  private static array ‪$singletons = [];
35 
39  public readonly ?‪Site ‪$site;
40 
41  public static function ‪backend(): self
42  {
43  return ‪self::asSingleton(new self(ApplicationType::BACKEND));
44  }
45 
46  public static function ‪frontend(): self
47  {
48  return ‪self::asSingleton(new self(ApplicationType::FRONTEND));
49  }
50 
54  public static function ‪frontendSite(?‪SiteInterface ‪$site): self
55  {
56  // PHPStan fails, see https://github.com/phpstan/phpstan/issues/8464
57  // @phpstan-ignore-next-line
58  if (!‪$site instanceof ‪Site || is_subclass_of(‪$site, Site::class)) {
59  return ‪self::frontend();
60  }
61  return ‪self::asSingleton(new self(ApplicationType::FRONTEND, ‪$site->‪getIdentifier(), ‪$site));
62  }
63 
64  public static function ‪frontendSiteIdentifier(string ‪$siteIdentifier): self
65  {
66  return ‪self::asSingleton(new self(ApplicationType::FRONTEND, ‪$siteIdentifier));
67  }
68 
69  public static function ‪from(string $value): self
70  {
71  $parts = ‪GeneralUtility::trimExplode('.', $value, true);
72  $type = ApplicationType::tryFrom($parts[0] ?? '');
73  ‪$siteIdentifier = $parts[1] ?? null;
74  if ($type === null) {
75  throw new \LogicException(
76  sprintf('Could not resolve application type from "%s"', $value),
77  1677424928
78  );
79  }
80  return ‪self::asSingleton(new self($type, ‪$siteIdentifier));
81  }
82 
83  public static function ‪reset(): void
84  {
85  self::$singletons = [];
86  }
87 
88  public static function ‪tryFrom(string $value): ?self
89  {
90  try {
91  return ‪self::from($value);
92  } catch (\LogicException) {
93  return null;
94  }
95  }
96 
97  private static function ‪asSingleton(self $self): self
98  {
99  $id = (string)$self;
100  if (!isset(self::$singletons[$id])) {
101  self::$singletons[$id] = $self;
102  }
103  return self::$singletons[$id];
104  }
105 
109  private function ‪__construct(
110  public readonly ‪ApplicationType $type,
111  public readonly ?string ‪$siteIdentifier = null,
112  ?‪Site ‪$site = null,
113  ) {
114  $this->site = ‪$site;
115  }
116 
117  public function ‪__toString(): string
118  {
119  $value = $this->type->value;
120  if ($this->siteIdentifier !== null) {
121  $value .= '.' . ‪$this->siteIdentifier;
122  }
123  return $value;
124  }
125 
126  public function ‪isFrontendSite(): bool
127  {
128  return $this->siteIdentifier !== null && $this->type->isFrontend();
129  }
130 
131  public function ‪jsonSerialize(): string
132  {
133  return (string)$this;
134  }
135 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\asSingleton
‪static asSingleton(self $self)
Definition: Scope.php:97
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\backend
‪static backend()
Definition: Scope.php:41
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\$singletons
‪static array $singletons
Definition: Scope.php:34
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontend
‪static frontend()
Definition: Scope.php:46
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\$site
‪readonly Site $site
Definition: Scope.php:39
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\reset
‪static reset()
Definition: Scope.php:83
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\tryFrom
‪static tryFrom(string $value)
Definition: Scope.php:88
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope
Definition: Scope.php:30
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy
Definition: ConsumableNonce.php:18
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\__construct
‪__construct(public readonly ApplicationType $type, public readonly ?string $siteIdentifier=null, ?Site $site=null,)
Definition: Scope.php:109
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontendSite
‪static frontendSite(?SiteInterface $site)
Definition: Scope.php:54
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\jsonSerialize
‪jsonSerialize()
Definition: Scope.php:131
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\isFrontendSite
‪isFrontendSite()
Definition: Scope.php:126
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontendSiteIdentifier
‪static frontendSiteIdentifier(string $siteIdentifier)
Definition: Scope.php:64
‪TYPO3\CMS\Webhooks\Message\$siteIdentifier
‪identifier readonly int readonly array readonly string readonly string $siteIdentifier
Definition: PageModificationMessage.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\__toString
‪__toString()
Definition: Scope.php:117
‪TYPO3\CMS\Core\Site\Entity\Site\getIdentifier
‪getIdentifier()
Definition: Site.php:185
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\from
‪static from(string $value)
Definition: Scope.php:69
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:55