‪TYPO3CMS  ‪main
AbstractGenerator.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 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
33 
39 abstract class ‪AbstractGenerator
40 {
44  protected function ‪createSiteConfiguration(int $topPageUid, string $base = 'http://localhost/', string $title = 'styleguide demo'): void
45  {
46  $recordFinder = GeneralUtility::makeInstance(RecordFinder::class);
47  // When the DataHandler created the page tree, a default site configuration has been added. Fetch, rename, update.
48  $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByRootPageId($topPageUid);
49  $siteConfiguration = GeneralUtility::makeInstance(SiteConfiguration::class);
50  ‪$siteIdentifier = 'styleguide-demo-' . $topPageUid;
51  $siteConfiguration->rename($site->getIdentifier(), ‪$siteIdentifier);
52  $highestLanguageId = $recordFinder->findHighestLanguageId();
53  $configuration = [
54  'base' => $base . 'styleguide-demo-' . $topPageUid,
55  'rootPageId' => $topPageUid,
56  'routes' => [],
57  'websiteTitle' => $title . ' ' . $topPageUid,
58  'baseVariants' => [],
59  'errorHandling' => [],
60  'languages' => [
61  [
62  'title' => 'English',
63  'enabled' => true,
64  'languageId' => 0,
65  'base' => '/',
66  'typo3Language' => 'default',
67  'locale' => 'en_US.UTF-8',
68  'iso-639-1' => 'en',
69  'navigationTitle' => 'English',
70  'hreflang' => 'en-us',
71  'direction' => 'ltr',
72  'flag' => 'us',
73  'websiteTitle' => '',
74  ],
75  [
76  'title' => 'styleguide demo language danish',
77  'enabled' => true,
78  'base' => '/da/',
79  'typo3Language' => 'da',
80  'locale' => 'da_DK.UTF-8',
81  'iso-639-1' => 'da',
82  'websiteTitle' => '',
83  'navigationTitle' => '',
84  'hreflang' => 'da-dk',
85  'direction' => '',
86  'fallbackType' => 'strict',
87  'fallbacks' => '',
88  'flag' => 'dk',
89  'languageId' => $highestLanguageId + 1,
90  ],
91  [
92  'title' => 'styleguide demo language german',
93  'enabled' => true,
94  'base' => '/de/',
95  'typo3Language' => 'de',
96  'locale' => 'de_DE.UTF-8',
97  'iso-639-1' => 'de',
98  'websiteTitle' => '',
99  'navigationTitle' => '',
100  'hreflang' => 'de-de',
101  'direction' => '',
102  'fallbackType' => 'strict',
103  'fallbacks' => '',
104  'flag' => 'de',
105  'languageId' => $highestLanguageId + 2,
106  ],
107  [
108  'title' => 'styleguide demo language french',
109  'enabled' => true,
110  'base' => '/fr/',
111  'typo3Language' => 'fr',
112  'locale' => 'fr_FR.UTF-8',
113  'iso-639-1' => 'fr',
114  'websiteTitle' => '',
115  'navigationTitle' => '',
116  'hreflang' => 'fr-fr',
117  'direction' => '',
118  'fallbackType' => 'strict',
119  'fallbacks' => '',
120  'flag' => 'fr',
121  'languageId' => $highestLanguageId + 3,
122  ],
123  [
124  'title' => 'styleguide demo language spanish',
125  'enabled' => true,
126  'base' => '/es/',
127  'typo3Language' => 'es',
128  'locale' => 'es_ES.UTF-8',
129  'iso-639-1' => 'es',
130  'websiteTitle' => '',
131  'navigationTitle' => '',
132  'hreflang' => 'es-es',
133  'direction' => '',
134  'fallbackType' => 'strict',
135  'fallbacks' => '',
136  'flag' => 'es',
137  'languageId' => $highestLanguageId + 4,
138  ],
139  ],
140  ];
141  $siteConfiguration->write(‪$siteIdentifier, $configuration);
142  }
143 
151  protected function ‪getUidOfLastTopLevelPage(): int
152  {
153  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
154  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
155  $lastPage = $queryBuilder->select('uid')
156  ->from('pages')
157  ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)))
158  ->orderBy('sorting', 'DESC')
159  ->executeQuery()
160  ->fetchOne();
161  ‪$uid = 0;
162  if (‪MathUtility::canBeInterpretedAsInteger($lastPage) && $lastPage > 0) {
163  ‪$uid = (int)$lastPage;
164  }
165  return ‪$uid;
166  }
167 
175  protected function ‪addToFal(array $files, string $from, string $to): void
176  {
177  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
178  $storage = $storageRepository->findByUid(1);
179  $folder = $storage->getRootLevelFolder();
180 
181  try {
182  $folder->createFolder($to);
183  $folder = $folder->getSubfolder($to);
184  foreach ($files as $fileName) {
185  $sourceLocation = GeneralUtility::getFileAbsFileName($from . $fileName);
186  $storage->addFile($sourceLocation, $folder, $fileName, DuplicationBehavior::RENAME, false);
187  }
188  } catch (‪ExistingTargetFolderException $e) {
189  // No op if folder exists. This code assumes file exist, too.
190  }
191  }
192 
198  protected function ‪deleteFalFolder(string $path): void
199  {
201  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
202  $storage = $storageRepository->findByUid(1);
203  $folder = $storage->getRootLevelFolder();
204  try {
205  $folder = $folder->getSubfolder($path);
206  $folder->delete(true);
207  } catch (\InvalidArgumentException $e) {
208  // No op if folder does not exist
209  }
210  }
211 
212  protected function ‪executeDataHandler(array $data = [], array $commands = []): void
213  {
214  if (!empty($data) || !empty($commands)) {
215  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
216  $dataHandler->enableLogging = false;
217  $dataHandler->bypassAccessCheckForRecords = true;
218  $dataHandler->bypassWorkspaceRestrictions = true;
219  $dataHandler->start($data, $commands);
220  if (‪Environment::isCli()) {
221  $dataHandler->clear_cacheCmd('all');
222  }
223 
224  empty($data) ?: $dataHandler->process_datamap();
225  empty($commands) ?: $dataHandler->process_cmdmap();
226 
227  // Update signal only if not running in cli mode
228  if (!‪Environment::isCli()) {
229  BackendUtility::setUpdateSignal('updatePageTree');
230  }
231  }
232  }
233 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator
Definition: AbstractGenerator.php:40
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException
Definition: ExistingTargetFolderException.php:23
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\addToFal
‪addToFal(array $files, string $from, string $to)
Definition: AbstractGenerator.php:175
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\createSiteConfiguration
‪createSiteConfiguration(int $topPageUid, string $base='http://localhost/', string $title='styleguide demo')
Definition: AbstractGenerator.php:44
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\getUidOfLastTopLevelPage
‪int getUidOfLastTopLevelPage()
Definition: AbstractGenerator.php:151
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior
‪DuplicationBehavior
Definition: DuplicationBehavior.php:28
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Styleguide\TcaDataGenerator
Definition: AbstractGenerator.php:18
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Webhooks\Message\$siteIdentifier
‪identifier readonly int readonly array readonly string readonly string $siteIdentifier
Definition: PageModificationMessage.php:38
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\executeDataHandler
‪executeDataHandler(array $data=[], array $commands=[])
Definition: AbstractGenerator.php:212
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\deleteFalFolder
‪deleteFalFolder(string $path)
Definition: AbstractGenerator.php:198