‪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', array $sets = []): 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  $siteWriter = GeneralUtility::makeInstance(SiteWriter::class);
50  ‪$siteIdentifier = 'styleguide-demo-' . $topPageUid;
51  $siteWriter->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  'dependencies' => $sets,
141  ];
142  $siteWriter->write(‪$siteIdentifier, $configuration);
143  }
144 
152  protected function ‪getUidOfLastTopLevelPage(): int
153  {
154  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
155  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
156  $lastPage = $queryBuilder->select('uid')
157  ->from('pages')
158  ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)))
159  ->orderBy('sorting', 'DESC')
160  ->executeQuery()
161  ->fetchOne();
162  ‪$uid = 0;
163  if (‪MathUtility::canBeInterpretedAsInteger($lastPage) && $lastPage > 0) {
164  ‪$uid = (int)$lastPage;
165  }
166  return ‪$uid;
167  }
168 
176  protected function ‪addToFal(array $files, string $from, string $to): void
177  {
178  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
179  $storage = $storageRepository->findByUid(1);
180  $folder = $storage->getRootLevelFolder();
181 
182  try {
183  $folder->createFolder($to);
184  $folder = $folder->getSubfolder($to);
185  foreach ($files as $fileName) {
186  $sourceLocation = GeneralUtility::getFileAbsFileName($from . $fileName);
187  $storage->addFile($sourceLocation, $folder, $fileName, DuplicationBehavior::RENAME, false);
188  }
189  } catch (‪ExistingTargetFolderException $e) {
190  // No op if folder exists. This code assumes file exist, too.
191  }
192  }
193 
199  protected function ‪deleteFalFolder(string $path): void
200  {
202  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
203  $storage = $storageRepository->findByUid(1);
204  $folder = $storage->getRootLevelFolder();
205  try {
206  $folder = $folder->getSubfolder($path);
207  $folder->delete(true);
208  } catch (\InvalidArgumentException $e) {
209  // No op if folder does not exist
210  }
211  }
212 
213  protected function ‪executeDataHandler(array $data = [], array $commands = []): void
214  {
215  if (!empty($data) || !empty($commands)) {
216  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
217  $dataHandler->enableLogging = false;
218  $dataHandler->bypassAccessCheckForRecords = true;
219  $dataHandler->bypassWorkspaceRestrictions = true;
220  $dataHandler->start($data, $commands);
221  if (‪Environment::isCli()) {
222  $dataHandler->clear_cacheCmd('all');
223  }
224 
225  empty($data) ?: $dataHandler->process_datamap();
226  empty($commands) ?: $dataHandler->process_cmdmap();
227 
228  // Update signal only if not running in cli mode
229  if (!‪Environment::isCli()) {
230  BackendUtility::setUpdateSignal('updatePageTree');
231  }
232  }
233  }
234 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:104
‪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:176
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\getUidOfLastTopLevelPage
‪int getUidOfLastTopLevelPage()
Definition: AbstractGenerator.php:152
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior
‪DuplicationBehavior
Definition: DuplicationBehavior.php:28
‪TYPO3\CMS\Core\Configuration\SiteWriter
Definition: SiteWriter.php:39
‪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\Styleguide\TcaDataGenerator\AbstractGenerator\createSiteConfiguration
‪createSiteConfiguration(int $topPageUid, string $base='http://localhost/', string $title='styleguide demo', array $sets=[])
Definition: AbstractGenerator.php:44
‪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:213
‪TYPO3\CMS\Styleguide\TcaDataGenerator\AbstractGenerator\deleteFalFolder
‪deleteFalFolder(string $path)
Definition: AbstractGenerator.php:199