‪TYPO3CMS  9.5
AspectFactory.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
27 
32 {
36  protected ‪$availableAspects;
37 
41  protected ‪$context;
42 
48  public function ‪__construct(‪Context ‪$context = null)
49  {
50  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
51  $this->availableAspects = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'] ?? [];
52  }
53 
62  public function ‪createAspects(array $aspects, ‪SiteLanguage $language, ‪Site $site): array
63  {
64  $aspects = array_map(
65  function ($settings) use ($language, $site) {
66  $type = (string)($settings['type'] ?? '');
67  $aspect = $this->‪create($type, $settings);
68  return $this->‪enrich($aspect, $language, $site);
69  },
70  $aspects
71  );
72  uasort($aspects, [$this, 'sortAspects']);
73  return $aspects;
74  }
75 
85  protected function ‪create(string $type, array $settings): ‪AspectInterface
86  {
87  if (empty($type)) {
88  throw new \InvalidArgumentException(
89  'Aspect type cannot be empty',
90  1538079481
91  );
92  }
93  if (!isset($this->availableAspects[$type])) {
94  throw new \OutOfRangeException(
95  sprintf('No aspect found for %s', $type),
96  1538079482
97  );
98  }
99  unset($settings['type']);
100  $className = $this->availableAspects[$type];
102  $aspect = GeneralUtility::makeInstance($className, $settings);
103  return $aspect;
104  }
105 
114  protected function ‪enrich(‪AspectInterface $aspect, ‪SiteLanguage $language, ‪Site $site): ‪AspectInterface
115  {
116  // the check for the trait is @deprecated and can be removed at any time after TYPO3 v11
117  if ($aspect instanceof ‪SiteLanguageAwareInterface || in_array(SiteLanguageAwareTrait::class, class_uses($aspect), true)) {
118  $aspect->setSiteLanguage($language);
119  }
120  if ($aspect instanceof ‪SiteAwareInterface) {
121  $aspect->setSite($site);
122  }
123  if ($aspect instanceof ‪ContextAwareInterface) {
124  $aspect->setContext($this->context);
125  }
126  return $aspect;
127  }
128 
137  protected function ‪sortAspects(‪AspectInterface $first, ‪AspectInterface $second): int
138  {
139  // when first is persisted, move it to the end (>0)
140  $first = $first instanceof ‪PersistedMappableAspectInterface ? 1 : 0;
141  // when second is persisted, move it to the beginning (<0)
142  $second = $second instanceof ‪PersistedMappableAspectInterface ? -1 : 0;
143  // 0 + 0 = 0 - both are non-persisted
144  // 1 - 1 = 0 - both are persisted
145  // 1 + 0 = 1 - only first is persisted
146  // 0 - 1 = -1 - only second is persisted
147  return $first + $second;
148  }
149 }
‪TYPO3\CMS\Core\Routing\Aspect\AspectInterface
Definition: AspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\__construct
‪__construct(Context $context=null)
Definition: AspectFactory.php:46
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:25
‪TYPO3\CMS\Core\Site\SiteLanguageAwareInterface
Definition: SiteLanguageAwareInterface.php:23
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Core\Site\SiteAwareInterface
Definition: SiteAwareInterface.php:23
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\enrich
‪AspectInterface mixed enrich(AspectInterface $aspect, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:112
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:25
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\$availableAspects
‪array $availableAspects
Definition: AspectFactory.php:35
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\create
‪AspectInterface create(string $type, array $settings)
Definition: AspectFactory.php:83
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:4
‪TYPO3\CMS\Core\Context\ContextAwareInterface
Definition: ContextAwareInterface.php:19
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\createAspects
‪AspectInterface[] createAspects(array $aspects, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:60
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\sortAspects
‪int sortAspects(AspectInterface $first, AspectInterface $second)
Definition: AspectFactory.php:135
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory
Definition: AspectFactory.php:32
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\$context
‪Context $context
Definition: AspectFactory.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface
Definition: PersistedMappableAspectInterface.php:24