/** * Returns a fresh or existing instance of the object specified by $objectName. * * Important: * * If possible, instances of Prototype objects should always be created with the * Object Factory's create() method and Singleton objects should rather be * injected by some type of Dependency Injection. * * @param string $objectName The name of the object to return an instance of * @return object The object instance * @author Robert Lemke <*****@*****.**> * @throws \F3\FLOW3\Object\Exception\UnknownObjectException if an object with the given name does not exist * @api */ public function getObject($objectName) { if (!$this->isObjectRegistered($objectName)) { throw new \F3\FLOW3\Object\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1166550023); } switch ($this->objectConfigurations[$objectName]->getScope()) { case 'prototype': $object = call_user_func_array(array($this->objectFactory, 'create'), func_get_args()); break; case 'singleton': if ($this->singletonObjectsRegistry->objectExists($objectName)) { $object = $this->singletonObjectsRegistry->getObject($objectName); } else { $arguments = array_slice(func_get_args(), 1); $overridingArguments = $this->getOverridingArguments($arguments); $object = $this->objectBuilder->createObject($objectName, $this->objectConfigurations[$objectName], $overridingArguments); $this->singletonObjectsRegistry->putObject($objectName, $object); $this->registerShutdownObject($object, $this->objectConfigurations[$objectName]->getLifecycleShutdownMethodName()); } break; case 'session': if ($this->sessionObjectsRegistry === NULL) { throw new \F3\FLOW3\Object\Exception('The session objects registry has not been injected correctly into the object manager.', 1247211113); } if ($this->sessionObjectsRegistry->objectExists($objectName)) { $object = $this->sessionObjectsRegistry->getObject($objectName); } else { $arguments = array_slice(func_get_args(), 1); $overridingArguments = $this->getOverridingArguments($arguments); $object = $this->objectBuilder->createObject($objectName, $this->objectConfigurations[$objectName], $overridingArguments); $this->sessionObjectsRegistry->putObject($objectName, $object); $this->registerShutdownObject($object, $this->objectConfigurations[$objectName]->getLifecycleShutdownMethodName()); } break; } return $object; }
/** * Checks if getObject() returns the object we have put into the cache previously * * @test * @author Robert Lemke <*****@*****.**> */ public function getObjectReturnsSameObjectWhichHasBeenStoredByPutObject() { $originalObject = new \F3\FLOW3\Fixture\DummyClass(); $this->objectRegistry->putObject('DummyObject', $originalObject); $this->assertSame($originalObject, $this->objectRegistry->getObject('DummyObject'), 'getObject() did not return the object we stored in the object registry previously.'); }