[BUGFIX] Allow array of paths in psr-4 autoload definition 31/49331/2
authorHelmut Hummel <info@helhum.io>
Tue, 2 Aug 2016 19:28:56 +0000 (21:28 +0200)
committerHelmut Hummel <typo3@helhum.io>
Tue, 2 Aug 2016 21:00:25 +0000 (23:00 +0200)
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>
typo3/sysext/core/Classes/Core/ClassLoadingInformationGenerator.php
typo3/sysext/core/Tests/Unit/Core/ClassLoadingInformationGeneratorTest.php

index b892bc4..b1a1d83 100644 (file)
@@ -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";
 
index 5c03bb1..9ecac23 100644 (file)
@@ -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' => [