[BUGFIX] Reuse entities of overridden classes in persistence session
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / Session.php
index 421bef1..0779b31 100644 (file)
@@ -23,6 +23,11 @@ use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
 class Session implements \TYPO3\CMS\Core\SingletonInterface
 {
     /**
+     * @var \TYPO3\CMS\Extbase\Object\Container\Container
+     */
+    protected $objectContainer;
+
+    /**
      * Reconstituted objects
      *
      * @var ObjectStorage
@@ -42,8 +47,9 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
     /**
      * Constructs a new Session
      */
-    public function __construct()
+    public function __construct(\TYPO3\CMS\Extbase\Object\Container\Container $container)
     {
+        $this->objectContainer = $container;
         $this->reconstitutedEntities = new ObjectStorage();
         $this->objectMap = new ObjectStorage();
     }
@@ -128,7 +134,7 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
      */
     public function hasIdentifier($identifier, $className)
     {
-        return isset($this->identifierMap[strtolower($className)][$identifier]);
+        return isset($this->identifierMap[$this->getClassIdentifier($className)][$identifier]);
     }
 
     /**
@@ -140,7 +146,7 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
      */
     public function getObjectByIdentifier($identifier, $className)
     {
-        return $this->identifierMap[strtolower($className)][$identifier];
+        return $this->identifierMap[$this->getClassIdentifier($className)][$identifier];
     }
 
     /**
@@ -168,7 +174,7 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
     public function registerObject($object, $identifier)
     {
         $this->objectMap[$object] = $identifier;
-        $this->identifierMap[strtolower(get_class($object))][$identifier] = $object;
+        $this->identifierMap[$this->getClassIdentifier(get_class($object))][$identifier] = $object;
     }
 
     /**
@@ -178,7 +184,7 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
      */
     public function unregisterObject($object)
     {
-        unset($this->identifierMap[strtolower(get_class($object))][$this->objectMap[$object]]);
+        unset($this->identifierMap[$this->getClassIdentifier(get_class($object))][$this->objectMap[$object]]);
         $this->objectMap->detach($object);
     }
 
@@ -192,4 +198,16 @@ class Session implements \TYPO3\CMS\Core\SingletonInterface
         $this->objectMap = new ObjectStorage();
         $this->reconstitutedEntities = new ObjectStorage();
     }
+
+    /**
+     * Objects are stored in the cache with their implementation class name
+     * to allow reusing instances of different classes that point to the same implementation
+     *
+     * @param string $className
+     * @return string a unique class identifier respecting configured implementation class names
+     */
+    protected function getClassIdentifier($className): string
+    {
+        return strtolower($this->objectContainer->getImplementationClassName($className));
+    }
 }