From badf20104117ed0681edb91b849fcd02217f12e9 Mon Sep 17 00:00:00 2001
From: Helmut Hummel <info@helhum.io>
Date: Tue, 2 Aug 2016 21:28:56 +0200
Subject: [PATCH] [BUGFIX] Allow array of paths in psr-4 autoload definition

When defining a namespace prefix for psr-4 autoload in composer.json,
it is possible to specify multiple paths as array.

Thus, we must handle the array case properly and not falsely assume
that there is only a path string.

Resolves: #77365
Releases: 7.6, master
Change-Id: If96f4ce9eb3f4466e8cc620025b1848681b56011
Reviewed-on: https://review.typo3.org/49331
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
---
 .../Core/ClassLoadingInformationGenerator.php | 28 ++++++++++---------
 .../ClassLoadingInformationGeneratorTest.php  | 13 +++++++++
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php b/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php
index b892bc44e150..b1a1d83cbda1 100644
--- a/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php
+++ b/typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php
@@ -81,17 +81,19 @@ class ClassLoadingInformationGenerator
             $autoloadPsr4 = $this->getAutoloadSectionFromManifest($manifest, 'psr-4');
             if (!empty($autoloadPsr4)) {
                 $classLoaderPrefixesPsr4 = $this->classLoader->getPrefixesPsr4();
-                foreach ($autoloadPsr4 as $namespacePrefix => $path) {
-                    $namespacePath = $packagePath . $path;
-                    if ($useRelativePaths) {
-                        $psr4[$namespacePrefix] = $this->makePathRelative($namespacePath, realpath($namespacePath));
-                    } else {
-                        $psr4[$namespacePrefix] = $namespacePath;
-                    }
-                    if (!empty($classLoaderPrefixesPsr4[$namespacePrefix])) {
-                        // The namespace prefix has been registered already, which means there also might be
-                        // a class map which we need to override
-                        $classMap = array_merge($classMap, $this->createClassMap($namespacePath, $useRelativePaths, false, $namespacePrefix));
+                foreach ($autoloadPsr4 as $namespacePrefix => $paths) {
+                    foreach ((array)$paths as $path) {
+                        $namespacePath = $packagePath . $path;
+                        if ($useRelativePaths) {
+                            $psr4[$namespacePrefix][] = $this->makePathRelative($namespacePath, realpath($namespacePath));
+                        } else {
+                            $psr4[$namespacePrefix][] = $namespacePath;
+                        }
+                        if (!empty($classLoaderPrefixesPsr4[$namespacePrefix])) {
+                            // The namespace prefix has been registered already, which means there also might be
+                            // a class map which we need to override
+                            $classMap = array_merge($classMap, $this->createClassMap($namespacePath, $useRelativePaths, false, $namespacePrefix));
+                        }
                     }
                 }
             }
@@ -233,8 +235,8 @@ EOF;
         }
         $classMapFile .= ");\n";
 
-        foreach ($psr4 as $prefix => $relativePath) {
-            $psr4File .= sprintf('    %s => array(%s),', var_export($prefix, true), $this->getPathCode($relativePath)) . LF;
+        foreach ($psr4 as $prefix => $relativePaths) {
+            $psr4File .= sprintf('    %s => array(%s),', var_export($prefix, true), implode(',', array_map([$this, 'getPathCode'], $relativePaths))) . LF;
         }
         $psr4File .= ");\n";
 
diff --git a/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php b/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php
index 5c03bb1de429..9ecac23e5817 100644
--- a/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php
+++ b/typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php
@@ -152,6 +152,19 @@ class ClassLoadingInformationGeneratorTest extends UnitTestCase
                 ],
                 [],
             ],
+            'Psr-4 section with array' => [
+                [
+                    'autoload' => [
+                        'psr-4' => [
+                            'TYPO3\\CMS\\TestExtension\\' => ['Classes/', 'Resources/PHP/'],
+                        ],
+                    ],
+                ],
+                [
+                    '\'TYPO3\\\\CMS\\\\TestExtension\\\\\' => array($typo3InstallDir . \'/Fixtures/test_extension/Classes\',$typo3InstallDir . \'/Fixtures/test_extension/Resources/PHP\')',
+                ],
+                [],
+            ],
             'Psr-4 section without trailing slash' => [
                 [
                     'autoload' => [
-- 
GitLab