‪TYPO3CMS  ‪main
ExtLocalconfFactory.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 
22 use TYPO3\CMS\Core\Package\PackageManager;
23 
28 {
29  public function ‪__construct(
30  private readonly PackageManager $packageManager,
31  private readonly ‪PhpFrontend $codeCache,
32  ) {}
33 
38  public function ‪load(): void
39  {
40  $cacheIdentifier = $this->‪getExtLocalconfCacheIdentifier();
41  $hasCache = $this->codeCache->require($cacheIdentifier) !== false;
42  if (!$hasCache) {
44  $this->‪createCacheEntry();
45  }
46  }
47 
48  public function ‪loadUncached(): void
49  {
51  }
52 
56  public function ‪createCacheEntry(): void
57  {
58  $phpCodeToCache = [];
59  // Set same globals as in loadSingleExtLocalconfFiles()
60  $phpCodeToCache[] = '';
63  // Iterate through loaded extensions and add ext_localconf content
64  foreach ($this->packageManager->getActivePackages() as $package) {
65  $extensionKey = $package->getPackageKey();
66  $extLocalconfPath = $package->getPackagePath() . 'ext_localconf.php';
67  if (@file_exists($extLocalconfPath)) {
68  // Include a header per extension to make the cache file more readable
69  $phpCodeToCache[] = '';
73  // Add ext_localconf.php content of extension
74  $phpCodeToCache[] = 'namespace {';
75  $phpCodeToCache[] = trim((string)file_get_contents($extLocalconfPath));
76  $phpCodeToCache[] = '}';
77  $phpCodeToCache[] = '';
78  $phpCodeToCache[] = '';
79  }
80  }
81  $phpCodeToCache = implode(LF, $phpCodeToCache);
82  // Remove all start and ending php tags from content, and remove strict_types=1 declaration.
83  $phpCodeToCache = preg_replace('/<\\?php|\\?>/is', '', $phpCodeToCache);
84  $phpCodeToCache = preg_replace('/declare\\s?+\\(\\s?+strict_types\\s?+=\\s?+1\\s?+\\);/is', '', (string)$phpCodeToCache);
85  $this->codeCache->set($this->‪getExtLocalconfCacheIdentifier(), $phpCodeToCache);
86  }
87 
91  private function ‪loadSingleExtLocalconfFiles(): void
92  {
93  foreach ($this->packageManager->getActivePackages() as $package) {
94  $extLocalconfPath = $package->getPackagePath() . 'ext_localconf.php';
95  if (file_exists($extLocalconfPath)) {
96  require $extLocalconfPath;
97  }
98  }
99  }
100 
104  private function ‪getExtLocalconfCacheIdentifier(): string
105  {
106  return (new ‪PackageDependentCacheIdentifier($this->packageManager))->withPrefix('ext_localconf')->toString();
107  }
108 }
‪TYPO3\CMS\Core\Configuration\Extension
Definition: ExtLocalconfFactory.php:18
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\getExtLocalconfCacheIdentifier
‪getExtLocalconfCacheIdentifier()
Definition: ExtLocalconfFactory.php:104
‪TYPO3\CMS\Core\Cache\Frontend\PhpFrontend
Definition: PhpFrontend.php:25
‪TYPO3\CMS\Core\Package\Cache\PackageDependentCacheIdentifier
Definition: PackageDependentCacheIdentifier.php:30
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\__construct
‪__construct(private readonly PackageManager $packageManager, private readonly PhpFrontend $codeCache,)
Definition: ExtLocalconfFactory.php:29
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\loadSingleExtLocalconfFiles
‪loadSingleExtLocalconfFiles()
Definition: ExtLocalconfFactory.php:91
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\loadUncached
‪loadUncached()
Definition: ExtLocalconfFactory.php:48
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory
Definition: ExtLocalconfFactory.php:28
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\createCacheEntry
‪createCacheEntry()
Definition: ExtLocalconfFactory.php:56
‪TYPO3\CMS\Core\Configuration\Extension\ExtLocalconfFactory\load
‪load()
Definition: ExtLocalconfFactory.php:38