[TASK] Streamline expressionLanguage usage in core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / ProviderConfigurationLoader.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\ExpressionLanguage;
5
6 use TYPO3\CMS\Core\Cache\CacheManager;
7 use TYPO3\CMS\Core\Package\PackageManager;
8 use TYPO3\CMS\Core\Service\DependencyOrderingService;
9 use TYPO3\CMS\Core\Utility\GeneralUtility;
10
11 /**
12 * Class ProviderConfigurationLoader
13 * This class resolves the expression language provider configuration and store in a cache.
14 */
15 class ProviderConfigurationLoader
16 {
17 protected $cacheIdentifier = 'expressionLanguageProviders';
18
19 /**
20 * @return array
21 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
22 */
23 public function getExpressionLanguageProviders(): array
24 {
25 $packageManager = GeneralUtility::makeInstance(
26 PackageManager::class,
27 GeneralUtility::makeInstance(DependencyOrderingService::class)
28 );
29 $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_core');
30
31 if ($cache->has($this->cacheIdentifier)) {
32 /** @noinspection PhpUndefinedMethodInspection the method require() will be added to the interface in v10 */
33 return $cache->require($this->cacheIdentifier);
34 }
35
36 $packages = $packageManager->getActivePackages();
37 $providers = [];
38 foreach ($packages as $package) {
39 $packageConfiguration = $package->getPackagePath() . 'Configuration/ExpressionLanguage.php';
40 if (file_exists($packageConfiguration)) {
41 $providersInPackage = require $packageConfiguration;
42 if (is_array($providersInPackage)) {
43 $providers[] = $providersInPackage;
44 }
45 }
46 }
47 $providers = count($providers) > 0 ? array_merge_recursive(...$providers) : $providers;
48 $cache->set($this->cacheIdentifier, 'return ' . var_export($providers, true) . ';');
49 return $providers ?? [];
50 }
51 }