‪TYPO3CMS  ‪main
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 
25 
30 {
36  public function ‪createAspects(array $aspects, ‪SiteLanguage $language, ‪Site $site): array
37  {
38  $aspects = array_map(
39  function ($settings) use ($language, $site) {
40  $type = (string)($settings['type'] ?? '');
41  $aspect = $this->‪create($type, $settings);
42  return $this->‪enrich($aspect, $language, $site);
43  },
44  $aspects
45  );
46  uasort($aspects, [$this, 'sortAspects']);
47  return $aspects;
48  }
49 
56  protected function ‪create(string $type, array $settings): ‪AspectInterface
57  {
58  if (empty($type)) {
59  throw new \InvalidArgumentException('Aspect type cannot be empty', 1538079481);
60  }
61  if (!isset(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'][$type])) {
62  throw new \OutOfRangeException(sprintf('No aspect found for %s', $type), 1538079482);
63  }
64  unset($settings['type']);
65  $className = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['aspects'][$type];
66  return GeneralUtility::makeInstance($className, $settings);
67  }
68 
72  protected function ‪enrich(‪AspectInterface $aspect, ‪SiteLanguage $language, ‪Site $site): ‪AspectInterface
73  {
74  if ($aspect instanceof ‪SiteLanguageAwareInterface) {
76  $aspect->setSiteLanguage($language);
77  }
78  if ($aspect instanceof ‪SiteAwareInterface) {
79  $aspect->setSite($site);
80  }
81  return $aspect;
82  }
83 
88  protected function ‪sortAspects(‪AspectInterface $first, ‪AspectInterface $second): int
89  {
90  // when first is persisted, move it to the end (>0)
91  $first = $first instanceof ‪PersistedMappableAspectInterface ? 1 : 0;
92  // when second is persisted, move it to the beginning (<0)
93  $second = $second instanceof ‪PersistedMappableAspectInterface ? -1 : 0;
94  // 0 + 0 = 0 - both are non-persisted
95  // 1 - 1 = 0 - both are persisted
96  // 1 + 0 = 1 - only first is persisted
97  // 0 - 1 = -1 - only second is persisted
98  return $first + $second;
99  }
100 }
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\sortAspects
‪sortAspects(AspectInterface $first, AspectInterface $second)
Definition: AspectFactory.php:88
‪TYPO3\CMS\Core\Routing\Aspect\AspectInterface
Definition: AspectInterface.php:23
‪TYPO3\CMS\Core\Site\SiteLanguageAwareInterface
Definition: SiteLanguageAwareInterface.php:26
‪TYPO3\CMS\Core\Site\SiteAwareInterface
Definition: SiteAwareInterface.php:26
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:27
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\create
‪create(string $type, array $settings)
Definition: AspectFactory.php:56
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\createAspects
‪AspectInterface[] createAspects(array $aspects, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory
Definition: AspectFactory.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Routing\Aspect\AspectFactory\enrich
‪enrich(AspectInterface $aspect, SiteLanguage $language, Site $site)
Definition: AspectFactory.php:72
‪TYPO3\CMS\Core\Routing\Aspect\PersistedMappableAspectInterface
Definition: PersistedMappableAspectInterface.php:24