[BUGFIX] Fix PHP Notices in cached ext_localconf.php
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / LoadTcaService.php
1 <?php
2 namespace TYPO3\CMS\Install\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Category\CategoryRegistry;
18 use TYPO3\CMS\Core\Package\PackageManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Service for loading the TCA
23 */
24 class LoadTcaService
25 {
26 /**
27 * Load TCA
28 * Mostly a copy of ExtensionManagementUtility to include TCA without migrations.
29 * To be used in install tool only.
30 *
31 * This will set up $GLOBALS['TCA']
32 *
33 * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::buildBaseTcaFromSingleFiles
34 */
35 public function loadExtensionTablesWithoutMigration()
36 {
37 $GLOBALS['TCA'] = [];
38
39 $activePackages = GeneralUtility::makeInstance(PackageManager::class)->getActivePackages();
40
41 // First load "full table" files from Configuration/TCA
42 foreach ($activePackages as $package) {
43 $tcaConfigurationDirectory = $package->getPackagePath() . 'Configuration/TCA';
44 if (is_dir($tcaConfigurationDirectory)) {
45 $files = scandir($tcaConfigurationDirectory);
46 foreach ($files as $file) {
47 if (is_file($tcaConfigurationDirectory . '/' . $file)
48 && ($file !== '.')
49 && ($file !== '..')
50 && (substr($file, -4, 4) === '.php')
51 ) {
52 $tcaOfTable = require $tcaConfigurationDirectory . '/' . $file;
53 if (is_array($tcaOfTable)) {
54 // TCA table name is filename without .php suffix, eg 'sys_notes', not 'sys_notes.php'
55 $tcaTableName = substr($file, 0, -4);
56 $GLOBALS['TCA'][$tcaTableName] = $tcaOfTable;
57 }
58 }
59 }
60 }
61 }
62
63 // Apply category stuff
64 CategoryRegistry::getInstance()->applyTcaForPreRegisteredTables();
65
66 // Execute override files from Configuration/TCA/Overrides
67 foreach ($activePackages as $package) {
68 $tcaOverridesPathForPackage = $package->getPackagePath() . 'Configuration/TCA/Overrides';
69 if (is_dir($tcaOverridesPathForPackage)) {
70 $files = scandir($tcaOverridesPathForPackage);
71 foreach ($files as $file) {
72 if (is_file($tcaOverridesPathForPackage . '/' . $file)
73 && ($file !== '.')
74 && ($file !== '..')
75 && (substr($file, -4, 4) === '.php')
76 ) {
77 require $tcaOverridesPathForPackage . '/' . $file;
78 }
79 }
80 }
81 }
82 }
83
84 /**
85 * Load ext_tables.php of a single extension
86 *
87 * @param string $extensionKey The extension to load a ext_tables.php file from.
88 */
89 public function loadSingleExtTablesFile(string $extensionKey)
90 {
91 global $T3_SERVICES, $T3_VAR, $TYPO3_CONF_VARS;
92 global $TBE_MODULES, $TBE_MODULES_EXT, $TCA;
93 global $PAGES_TYPES, $TBE_STYLES;
94 global $_EXTKEY;
95
96 if (!isset($GLOBALS['TYPO3_LOADED_EXT'][$extensionKey])) {
97 throw new \RuntimeException(
98 'Extension ' . $extensionKey . ' does not exist in TYPO3_LOADED_EXT',
99 1477217619
100 );
101 }
102
103 $extensionInformation = $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey];
104 $_EXTKEY = $extensionKey;
105 // Load each ext_tables.php file of loaded extensions
106 if ((is_array($extensionInformation) || $extensionInformation instanceof \ArrayAccess)
107 && $extensionInformation['ext_tables.php']
108 ) {
109 // $_EXTKEY and $_EXTCONF are available in ext_tables.php
110 // and are explicitly set in cached file as well
111 $_EXTCONF = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY] ?? null;
112 require $extensionInformation['ext_tables.php'];
113 }
114 }
115 }