‪TYPO3CMS  10.4
AspectFactory.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 
28 
33 {
37  protected ‪$availableAspects;
38 
42  protected ‪$context;
43 
49  public function ‪__construct(‪Context ‪$context = null)
50  {
51  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
52  $this->availableAspects = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'] ?? [];
53  }
54 
63  public function ‪createAspects(array $aspects, ‪SiteLanguage $language, ‪Site $site): array
64  {
65  $aspects = array_map(
66  function ($settings) use ($language, $site) {
67  $type = (string)($settings['type'] ?? '');
68  $aspect = $this->‪create($type, $settings);
69  return $this->‪enrich($aspect, $language, $site);
70  },
71  $aspects
72  );
73  uasort($aspects, [$this, 'sortAspects']);
74  return $aspects;
75  }
76 
86  protected function ‪create(string $type, array $settings): ‪AspectInterface
87  {
88  if (empty($type)) {
89  throw new \InvalidArgumentException(
90  'Aspect type cannot be empty',
91  1538079481
92  );
93  }
94  if (!isset($this->availableAspects[$type])) {
95  throw new \OutOfRangeException(
96  sprintf('No aspect found for %s', $type),
97  1538079482
98  );
99  }
100  unset($settings['type']);
101  $className = $this->availableAspects[$type];
103  $aspect = GeneralUtility::makeInstance($className, $settings);
104  return $aspect;
105  }
106 
115  protected function ‪enrich(‪AspectInterface $aspect, ‪SiteLanguage $language, ‪Site $site): ‪AspectInterface
116  {
117  // the check for the trait is @deprecated and can be removed at any time after TYPO3 v11
118  if ($aspect instanceof ‪SiteLanguageAwareInterface || in_array(SiteLanguageAwareTrait::class, class_uses($aspect), true)) {
120  $aspect->setSiteLanguage($language);
121  }
122  if ($aspect instanceof ‪SiteAwareInterface) {
123  $aspect->setSite($site);
124  }
125  if ($aspect instanceof ‪ContextAwareInterface) {
126  $aspect->setContext($this->context);
127  }
128  return $aspect;
129  }
130 
139  protected function ‪sortAspects(‪AspectInterface $first, ‪AspectInterface $second): int
140  {
141  // when first is persisted, move it to the end (>0)
142  $first = $first instanceof ‪PersistedMappableAspectInterface ? 1 : 0;
143  // when second is persisted, move it to the beginning (<0)
144  $second = $second instanceof ‪PersistedMappableAspectInterface ? -1 : 0;
145  // 0 + 0 = 0 - both are non-persisted
146  // 1 - 1 = 0 - both are persisted
147  // 1 + 0 = 1 - only first is persisted
148  // 0 - 1 = -1 - only second is persisted
149  return $first + $second;
150  }
151 }
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\enrich
‪AspectInterface enrich(AspectInterface $aspect, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:113
‪TYPO3\CMS\Core\Routing\Aspect\AspectInterface
Definition: AspectInterface.php:24
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\__construct
‪__construct(Context $context=null)
Definition: AspectFactory.php:47
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:28
‪TYPO3\CMS\Core\Site\SiteLanguageAwareInterface
Definition: SiteLanguageAwareInterface.php:26
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Site\SiteAwareInterface
Definition: SiteAwareInterface.php:26
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\$availableAspects
‪array $availableAspects
Definition: AspectFactory.php:36
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\create
‪AspectInterface create(string $type, array $settings)
Definition: AspectFactory.php:84
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Context\ContextAwareInterface
Definition: ContextAwareInterface.php:21
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\createAspects
‪AspectInterface[] createAspects(array $aspects, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:61
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\sortAspects
‪int sortAspects(AspectInterface $first, AspectInterface $second)
Definition: AspectFactory.php:137
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory
Definition: AspectFactory.php:33
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\$context
‪Context $context
Definition: AspectFactory.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface
Definition: PersistedMappableAspectInterface.php:25