AppserverIo\Appserver\DependencyInjectionContainer\Provider::injectDependencies PHP Метод

injectDependencies() публичный Метод

Injects the dependencies of the passed instance.
public injectDependencies ( object $instance, string | null $sessionId = null ) : void
$instance object The instance to inject the dependencies for
$sessionId string | null The session-ID, necessary to inject stateful session beans (SFBs)
Результат void
    public function injectDependencies($instance, $sessionId = null)
    {
        // load the object manager instance
        /** @var \AppserverIo\Appserver\DependencyInjectionContainer\Interfaces\ObjectManagerInterface $objectManager */
        $objectManager = $this->getNamingDirectory()->search(sprintf('php:global/%s/ObjectManagerInterface', $this->getApplication()->getUniqueName()));
        // load the object descriptor for the instance from the the object manager
        if ($objectManager->hasObjectDescriptor($className = get_class($instance))) {
            // load the object descriptor
            $objectDescriptor = $objectManager->getObjectDescriptor($className);
            // check if a reflection class instance has been passed or is already available
            $reflectionClass = $this->getReflectionClassForObject($instance);
            // check for declared EPB and resource references
            foreach ($objectDescriptor->getReferences() as $reference) {
                // check if we've a reflection target defined
                if ($injectionTarget = $reference->getInjectionTarget()) {
                    // load the instance to inject by lookup the initial context
                    $toInject = $this->getNamingDirectory()->search(sprintf('php:global/%s/%s', $this->getApplication()->getUniqueName(), $reference->getName()), array($sessionId));
                    // query for method injection
                    if (method_exists($instance, $targetName = $injectionTarget->getTargetMethod())) {
                        // inject the target by invoking the method
                        $instance->{$targetName}($toInject);
                        // query if we've a reflection property with the target name - this is the faster method!
                    } elseif (property_exists($instance, $targetName = $injectionTarget->getTargetProperty())) {
                        // load the reflection property
                        $reflectionProperty = $reflectionClass->getProperty($targetName);
                        // load the PHP ReflectionProperty instance to inject the bean instance
                        $phpReflectionProperty = $reflectionProperty->toPhpReflectionProperty();
                        $phpReflectionProperty->setAccessible(true);
                        $phpReflectionProperty->setValue($instance, $toInject);
                    } else {
                        // throw an exception
                        throw new DependencyInjectionException(sprintf('Can\'t find property or method %s in class %s', $targetName, $className));
                    }
                }
            }
        }
    }