[FEATURE] Add ObjectManager->getScope like in TYPO3.Flow 89/21189/5
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Sun, 2 Jun 2013 10:59:52 +0000 (12:59 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 4 Jun 2013 19:33:06 +0000 (21:33 +0200)
This method detects if a class is of type prototype or
singleton. We need this method as it is a dependency for
further backporting of the Flow validation api.

Releases: 6.2
Resolves: #48766
Change-Id: I4d6b043a4015c31ede0b099eebed4491a38723cd
Reviewed-on: https://review.typo3.org/21189
Tested-by: Wouter Wolters
Reviewed-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Object/Container/Container.php
typo3/sysext/extbase/Classes/Object/ObjectManager.php
typo3/sysext/extbase/Classes/Object/ObjectManagerInterface.php
typo3/sysext/extbase/Tests/Unit/Object/Container/ContainerTest.php

index 17553a0..e30a8e7 100644 (file)
@@ -36,6 +36,9 @@ namespace TYPO3\CMS\Extbase\Object\Container;
  */
 class Container implements \TYPO3\CMS\Core\SingletonInterface {
 
+       const SCOPE_PROTOTYPE = 1;
+       const SCOPE_SINGLETON = 2;
+
        /**
         * internal cache for classinfos
         *
@@ -327,6 +330,24 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
                }
                return $classInfo;
        }
+
+       /**
+        * @param string $className
+        *
+        * @return boolean
+        */
+       public function isSingleton($className) {
+               return $this->getClassInfo($className)->getIsSingleton();
+       }
+
+       /**
+        * @param string $className
+        *
+        * @return boolean
+        */
+       public function isPrototype($className) {
+               return !$this->isSingleton($className);
+       }
 }
 
 ?>
\ No newline at end of file
index 2dbc430..d2e808a 100644 (file)
@@ -27,6 +27,9 @@ namespace TYPO3\CMS\Extbase\Object;
  *
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
+
+use \TYPO3\CMS\Extbase\Object\Container\Container;
+
 /**
  * Implementation of the default Extbase Object Manager
  */
@@ -108,6 +111,21 @@ class ObjectManager implements ObjectManagerInterface {
        }
 
        /**
+        * Returns the scope of the specified object.
+        *
+        * @param string $objectName The object name
+        * @return integer One of the Container::SCOPE_ constants
+        * @throws \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException
+        * @api
+        */
+       public function getScope($objectName) {
+               if (!$this->isRegistered($objectName)) {
+                       throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1265367590);
+               }
+               return $this->objectContainer->isSingleton($objectName) ? Container::SCOPE_SINGLETON : Container::SCOPE_PROTOTYPE;
+       }
+
+       /**
         * Creates a fresh instance of the object specified by $objectName.
         *
         * This factory method can only create objects of the scope prototype.
index d387df8..27c4b50 100644 (file)
@@ -71,6 +71,14 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface {
         * @api
         */
        public function getEmptyObject($className);
+
+       /**
+        * Returns the scope of the specified object.
+        *
+        * @param string $objectName The object name
+        * @return integer One of the Container::SCOPE_ constants
+        */
+       public function getScope($objectName);
 }
 
 ?>
\ No newline at end of file
index 2dc8514..7e38b27 100644 (file)
@@ -310,6 +310,22 @@ class ContainerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $object = $this->container->getInstance('t3lib_object_prototypeNeedsSingletonInConstructor');
                $this->assertInstanceOf('t3lib_object_singleton', $object->dependency);
        }
+
+       /**
+        * @test
+        */
+       public function isSingletonReturnsTrueForSingletonInstancesAndFalseForPrototypes() {
+               $this->assertTrue($this->container->isSingleton('TYPO3\CMS\Extbase\Object\Container\Container'));
+               $this->assertFalse($this->container->isSingleton('TYPO3\CMS\Extbase\Core\Bootstrap'));
+       }
+
+       /**
+        * @test
+        */
+       public function isPrototypeReturnsFalseForSingletonInstancesAndTrueForPrototypes() {
+               $this->assertFalse($this->container->isPrototype('TYPO3\CMS\Extbase\Object\Container\Container'));
+               $this->assertTrue($this->container->isPrototype('TYPO3\CMS\Extbase\Core\Bootstrap'));
+       }
 }
 
 ?>
\ No newline at end of file