[BUGFIX] Avoid makeInstance calls starting with a backslash 36/35836/2
authorBenjamin Mack <benni@typo3.org>
Tue, 6 Jan 2015 16:12:34 +0000 (17:12 +0100)
committerBenjamin Mack <benni@typo3.org>
Tue, 6 Jan 2015 18:33:29 +0000 (19:33 +0100)
When using singletons and calling makeInstance
with a beginning backslash in the class name
(see forge example), the singleton is not
correctly used.

Like inside the class loader, makeInstance and
the Extbase object manager should check for
a beginning backslash in the string of the
class name, and throw an exception, to give developers
a hint about broken code.

Change-Id: Ie7e0a6436fa8c5d983cd7e61b6ba96184440c657
Resolves: #60174
Releases: 6.2
Reviewed-on: http://review.typo3.org/35836
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/extbase/Classes/Object/Container/Container.php

index dd4c5ea..9018bf6 100755 (executable)
@@ -4392,6 +4392,10 @@ Connection: close
                if (!is_string($className) || empty($className)) {
                        throw new \InvalidArgumentException('$className must be a non empty string.', 1288965219);
                }
+               // Never instantiate with a beginning backslash, otherwise things like singletons won't work.
+               if ($className[0] === '\\') {
+                       $className = substr($className, 1);
+               }
                if (isset(static::$finalClassNameCache[$className])) {
                        $finalClassName = static::$finalClassNameCache[$className];
                } else {
index 432ecb0..cef2c75 100644 (file)
@@ -133,6 +133,10 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
         * @return object the built object
         */
        protected function getInstanceInternal($className, $givenConstructorArguments = array()) {
+               // Never instantiate with a beginning backslash, otherwise things like singletons won't work.
+               if ($className[0] === '\\') {
+                       $className = substr($className, 1);
+               }
                $className = $this->getImplementationClassName($className);
                if ($className === 'TYPO3\\CMS\\Extbase\\Object\\Container\\Container') {
                        return $this;