Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Harden class loading in non composer mode #687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 0 additions & 26 deletions Classes/Console/Core/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ public function __construct(\Composer\Autoload\ClassLoader $classLoader)
$this->bootstrap->initializeClassLoader($classLoader);
// Initialize basic annotation loader until TYPO3 does so as well
AnnotationRegistry::registerLoader('class_exists');

// We need to be sure all classes can be loaded in non composer mode as early as possible
$this->initializeNonComposerClassLoading();
$this->runLevel = new RunLevel($this->bootstrap);
}

Expand All @@ -83,29 +80,6 @@ private function ensureRequiredEnvironment()
}
}

/**
* Register auto loading for our own classes in case we cannot rely on composer class loading.
*/
private function initializeNonComposerClassLoading()
{
if (Bootstrap::usesComposerClassLoading()) {
return;
}
$extensionBaseDir = dirname(__DIR__, 3) . '/';
$autoloadDefinition = json_decode(file_get_contents($extensionBaseDir . 'composer.json'), true)['autoload']['psr-4'];
foreach ($autoloadDefinition as $prefix => $paths) {
$paths = array_map(
function ($path) use ($extensionBaseDir) {
return $extensionBaseDir . $path;
},
(array)$paths
);
$this->classLoader->addPsr4($prefix, $paths);
}
$pharFile = __DIR__ . '/../../../Libraries/symfony-process.phar';
require 'phar://' . $pharFile . '/vendor/autoload.php';
}

/**
* If detected TYPO3 version does not match the main supported version,
* overlay compatibility classes for the detected branch, by registering
Expand Down
26 changes: 26 additions & 0 deletions Resources/Private/ExtensionArtifacts/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
return (function () {
static $classLoader;
if ($classLoader) {
return $classLoader;
}

$typo3AutoLoadFile = realpath(($rootPath = dirname(__DIR__, 6)) . '/typo3') . '/../vendor/autoload.php';
putenv('TYPO3_PATH_ROOT=' . $rootPath);
$classLoader = require $typo3AutoLoadFile;

$extensionBaseDir = dirname(__DIR__, 3) . '/';
$autoloadDefinition = json_decode(file_get_contents($extensionBaseDir . 'composer.json'), true)['autoload']['psr-4'];
foreach ($autoloadDefinition as $prefix => $paths) {
$paths = array_map(
function ($path) use ($extensionBaseDir) {
return $extensionBaseDir . $path;
},
(array)$paths
);
$classLoader->addPsr4($prefix, $paths);
}
$pharFile = __DIR__ . '/../../../Libraries/symfony-process.phar';
require 'phar://' . $pharFile . '/vendor/autoload.php';
return $classLoader;
})();
2 changes: 2 additions & 0 deletions Resources/Private/ExtensionArtifacts/ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
'suggests' => [
],
],
'autoload' => [
],
];
1 change: 1 addition & 0 deletions Resources/Private/ExtensionArtifacts/ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

(function () {
if ((TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) || (TYPO3_MODE === 'BE' && isset($_GET['M']) && 'tools_ExtensionmanagerExtensionmanager' === $_GET['M'])) {
require \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('typo3_console') . 'Resources/Private/ExtensionArtifacts/autoload.php';
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
$signalSlotDispatcher->connect(
\TYPO3\CMS\Extensionmanager\Utility\InstallUtility::class,
Expand Down
10 changes: 2 additions & 8 deletions Scripts/typo3-console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
} elseif (file_exists($vendorAutoLoadFile = dirname(dirname(dirname(__DIR__))) . '/autoload.php')) {
// Console is a dependency, thus located in vendor/helhum/typo3-console
$classLoader = require $vendorAutoLoadFile;
} elseif (file_exists($typo3AutoLoadFile = realpath(($rootPath = dirname(dirname(dirname(dirname(__DIR__))))) . '/typo3') . '/../vendor/autoload.php')) {
} elseif (file_exists($typo3AutoLoadFile = __DIR__ . '/../Resources/Private/ExtensionArtifacts/autoload.php')) {
// Console is extension
putenv('TYPO3_PATH_ROOT=' . $rootPath);
$classLoader = require $typo3AutoLoadFile;
} else {
echo 'Could not find autoload.php file. Is TYPO3_PATH_ROOT specified correctly?' . PHP_EOL;
echo 'Could not find autoload.php file. TYPO3 Console needs to be installed with composer' . PHP_EOL;
exit(1);
}

if (!class_exists(\Helhum\Typo3Console\Core\Kernel::class)) {
// This require is needed so that the console works in non Composer mode,
// where requiring the main autoload.php is not enough to load extension classes
require __DIR__ . '/../Classes/Core/Kernel.php';
}
$kernel = new \Helhum\Typo3Console\Core\Kernel($classLoader);
$exitCode = $kernel->handle(new \Helhum\Typo3Console\Mvc\Cli\Symfony\Input\ArgvInput());
$kernel->terminate($exitCode);
Expand Down