[BUGFIX] Ensure LoadTcaService loads ext_tables.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 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
24 */
25 class LoadTcaService
26 {
27 /**
28 * Load TCA
29 * Mostly a copy of ExtensionManagementUtility to include TCA without migrations.
30 * To be used in install tool only.
31 *
32 * This will set up $GLOBALS['TCA']
33 *
34 * @see \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::buildBaseTcaFromSingleFiles
35 */
36 public function loadExtensionTablesWithoutMigration()
37 {
38 $GLOBALS['TCA'] = [];
39
40 $activePackages = GeneralUtility::makeInstance(PackageManager::class)->getActivePackages();
41
42 // First load "full table" files from Configuration/TCA
43 foreach ($activePackages as $package) {
44 $tcaConfigurationDirectory = $package->getPackagePath() . 'Configuration/TCA';
45 if (is_dir($tcaConfigurationDirectory)) {
46 $files = scandir($tcaConfigurationDirectory);
47 foreach ($files as $file) {
48 if (is_file($tcaConfigurationDirectory . '/' . $file)
49 && ($file !== '.')
50 && ($file !== '..')
51 && (substr($file, -4, 4) === '.php')
52 ) {
53 $tcaOfTable = require $tcaConfigurationDirectory . '/' . $file;
54 if (is_array($tcaOfTable)) {
55 // TCA table name is filename without .php suffix, eg 'sys_notes', not 'sys_notes.php'
56 $tcaTableName = substr($file, 0, -4);
57 $GLOBALS['TCA'][$tcaTableName] = $tcaOfTable;
58 }
59 }
60 }
61 }
62 }
63
64 // Apply category stuff
65 CategoryRegistry::getInstance()->applyTcaForPreRegisteredTables();
66
67 // Execute override files from Configuration/TCA/Overrides
68 foreach ($activePackages as $package) {
69 $tcaOverridesPathForPackage = $package->getPackagePath() . 'Configuration/TCA/Overrides';
70 if (is_dir($tcaOverridesPathForPackage)) {
71 $files = scandir($tcaOverridesPathForPackage);
72 foreach ($files as $file) {
73 if (is_file($tcaOverridesPathForPackage . '/' . $file)
74 && ($file !== '.')
75 && ($file !== '..')
76 && (substr($file, -4, 4) === '.php')
77 ) {
78 require $tcaOverridesPathForPackage . '/' . $file;
79 }
80 }
81 }
82 }
83 }
84
85 /**
86 * Load ext_tables.php of a single extension
87 *
88 * @param string $extensionKey The extension to load a ext_tables.php file from.
89 */
90 public function loadSingleExtTablesFile(string $extensionKey)
91 {
92 global $T3_SERVICES, $T3_VAR, $TYPO3_CONF_VARS;
93 global $TBE_MODULES, $TBE_MODULES_EXT, $TCA;
94 global $PAGES_TYPES, $TBE_STYLES;
95 global $_EXTKEY;
96
97 $packageManager = GeneralUtility::makeInstance(PackageManager::class);
98 try {
99 $package = $packageManager->getPackage($extensionKey);
100 } catch (\TYPO3\CMS\Core\Package\Exception\UnknownPackageException $e) {
101 throw new \RuntimeException(
102 'Extension ' . $extensionKey . ' is not active',
103 1477217619
104 );
105 }
106
107 $extTablesPath = $package->getPackagePath() . 'ext_tables.php';
108 // Load ext_tables.php file of the extension
109 if (@file_exists($extTablesPath)) {
110 // $_EXTKEY and $_EXTCONF are available in ext_tables.php
111 // and are explicitly set in cached file as well
112 $_EXTKEY = $extensionKey;
113 $_EXTCONF = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY] ?? null;
114 require $extTablesPath;
115 }
116 }
117 }