/** * Get service instance * * @param ServiceLocatorInterface $serviceLocator Service Locator instance * @return mixed */ public function getServiceInstance(ServiceLocatorInterface $serviceLocator) { if ($this->instance === null) { /** * Instance must be created using factory */ if ($this->factory instanceof \Closure) { $instance = call_user_func($this->factory, $serviceLocator); } else { $instance = $this->factory->factory($serviceLocator); } if (!$this->isShared()) { return $instance; // new instance must be returned } else { $this->instance = $instance; // cache instance return $this->instance; // same instance must be returned } } else { /** * Instance either was passed directly or has been created earlier */ if (!$this->isShared()) { return $this->instance; // new instance must be returned } else { return clone $this->instance; // same instance must be returned } } }