protected function instantiate($recipe, array $arguments = [])
    {
        // Merge arguments with existing?
        $recipe = $this->prepareArguments($recipe, $arguments);

        // Get skeleton for storing different types of injections
        $injections = $this->initInjectionMapSkeleton();

        // Does it have dependencies?
        if (true === isset($recipe['dependencies']) && null !== $recipe['dependencies']) {

            // Iterate over configuration
            foreach ($recipe['dependencies'] as $dependency) {

                /* @var $dependency Doozr_Di_Dependency */
                // Create instance if no instance exists ...
                if (null === $dependency->getInstance()) {

                    // Check if dependency is just linked!
                    if (true === $dependency->hasLink()) {

                        // Look in registry for instance ...
                        if (null === $instance = $this->getRegistry()->get($dependency->getLink())) {
                            // ... alternatively try to build one :)
                            $instance = $this->getRegistry()->getContainer()->build($dependency->getLink());
                        }

                        if (null === $instance) {
                            throw new Doozr_Di_Exception(
                                sprintf(
                                    'Link to "%s" could not be resolved/satisfied by registry instance of type "%s".',
                                    $dependency->getLink(),
                                    get_class($this->getRegistry())
                                )
                            );
                        }

                        $dependency->setInstance($instance);
                    } else {

                        // Check basic requirements :D it's just the className!
                        if (null === $className = $dependency->getClassName()) {
                            throw new Doozr_Di_Exception(
                                sprintf(
                                    'Property "className" not set! If you are not using "link" then you need to '.
                                    'define the class to inject via "className".'
                                )
                            );
                        }

                        // Check if the constructor is known to us ...
                        if (null === $constructor = $dependency->getConstructor()) {
                            $constructor = self::parseConstructor(new \ReflectionClass($className));
                            $dependency->setConstructor($constructor);
                        }

                        // Create instance via this class ;)
                        $instance = $this->instantiate(
                            [
                                'className'   => $className,
                                'constructor' => $constructor,
                                'arguments'   => $dependency->getArguments(),
                            ]
                        );

                        // Store the instance
                        $dependency->setInstance($instance);
                    }
                }

                // Store position for injection if type = configuration
                if (null === $dependency->getPosition()) {
                    $position = (isset($injections[$dependency->getType()]))
                        ? count($injections[$dependency->getType()]) + 1
                        : 1;
                    $dependency->setPosition($position);
                }

                $injections[$dependency->getType()][] = [
                    'instance' => $dependency->getInstance(),
                    'position' => $dependency->getPosition(),
                    'target'   => $dependency->getTarget(),
                ];
            }
        }

        // Process injections, create instance and return it
        return $this->createInstance($recipe['className'], $recipe['constructor'], $arguments, $injections);
    }
protected function instantiate($recipe, array $arguments = [])
    {
        // Merge arguments with existing?
        $recipe = $this->prepareArguments($recipe, $arguments);

        // Get skeleton for storing different types of injections
        $injections = $this->initInjectionMapSkeleton();

        // Does it have dependencies?
        if (true === isset($recipe['dependencies']) && null !== $recipe['dependencies']) {

            // Iterate over configuration
            foreach ($recipe['dependencies'] as $dependency) {

                /* @var $dependency Doozr_Di_Dependency */
                // Create instance if no instance exists ...
                if (null === $dependency->getInstance()) {

                    // Check if dependency is just linked!
                    if (true === $dependency->hasLink()) {

                        // Look in registry for instance ...
                        if (null === $instance = $this->getRegistry()->get($dependency->getLink())) {
                            // ... alternatively try to build one :)
                            $instance = $this->getRegistry()->getContainer()->build($dependency->getLink());
                        }

                        if (null === $instance) {
                            throw new Doozr_Di_Exception(
                                sprintf(
                                    'Link to "%s" could not be resolved/satisfied by registry instance of type "%s".',
                                    $dependency->getLink(),
                                    get_class($this->getRegistry())
                                )
                            );
                        }

                        $dependency->setInstance($instance);
                    } else {

                        // Check basic requirements :D it's just the className!
                        if (null === $className = $dependency->getClassName()) {
                            throw new Doozr_Di_Exception(
                                sprintf(
                                    'Property "className" not set! If you are not using "link" then you need to '.
                                    'define the class to inject via "className".'
                                )
                            );
                        }

                        // Check if the constructor is known to us ...
                        if (null === $constructor = $dependency->getConstructor()) {
                            $constructor = self::parseConstructor(new \ReflectionClass($className));
                            $dependency->setConstructor($constructor);
                        }

                        // Create instance via this class ;)
                        $instance = $this->instantiate(
                            [
                                'className'   => $className,
                                'constructor' => $constructor,
                                'arguments'   => $dependency->getArguments(),
                            ]
                        );

                        // Store the instance
                        $dependency->setInstance($instance);
                    }
                }

                // Store position for injection if type = configuration
                if (null === $dependency->getPosition()) {
                    $position = (isset($injections[$dependency->getType()]))
                        ? count($injections[$dependency->getType()]) + 1
                        : 1;
                    $dependency->setPosition($position);
                }

                $injections[$dependency->getType()][] = [
                    'instance' => $dependency->getInstance(),
                    'position' => $dependency->getPosition(),
                    'target'   => $dependency->getTarget(),
                ];
            }
        }

        // Process injections, create instance and return it
        return $this->createInstance($recipe['className'], $recipe['constructor'], $arguments, $injections);
    }
protected function instantiate ( $ recipe , array $ arguments = [ ] ) { // Merge arguments with existing? $ recipe = $ this -> prepareArguments ( $ recipe , $ arguments ) ; // Get skeleton for storing different types of injections $ injections = $ this -> initInjectionMapSkeleton ( ) ; // Does it have dependencies? if ( true === isset ( $ recipe [ 'dependencies' ] ) && null !== $ recipe [ 'dependencies' ] ) { // Iterate over configuration foreach ( $ recipe [ 'dependencies' ] as $ dependency ) { /* @var $dependency Doozr_Di_Dependency */ // Create instance if no instance exists ... if ( null === $ dependency -> getInstance ( ) ) { // Check if dependency is just linked! if ( true === $ dependency -> hasLink ( ) ) { // Look in registry for instance ... if ( null === $ instance = $ this -> getRegistry ( ) -> get ( $ dependency -> getLink ( ) ) ) { // ... alternatively try to build one :) $ instance = $ this -> getRegistry ( ) -> getContainer ( ) -> build ( $ dependency -> getLink ( ) ) ; } if ( null === $ instance ) { throw new Doozr_Di_Exception ( sprintf ( 'Link to "%s" could not be resolved/satisfied by registry instance of type "%s".' , $ dependency -> getLink ( ) , get_class ( $ this -> getRegistry ( ) ) ) ) ; } $ dependency -> setInstance ( $ instance ) ; } else { // Check basic requirements :D it's just the className! if ( null === $ className = $ dependency -> getClassName ( ) ) { throw new Doozr_Di_Exception ( sprintf ( 'Property "className" not set! If you are not using "link" then you need to ' . 'define the class to inject via "className".' ) ) ; } // Check if the constructor is known to us ... if ( null === $ constructor = $ dependency -> getConstructor ( ) ) { $ constructor = self :: parseConstructor ( new \ ReflectionClass ( $ className ) ) ; $ dependency -> setConstructor ( $ constructor ) ; } // Create instance via this class ;) $ instance = $ this -> instantiate ( [ 'className' => $ className , 'constructor' => $ constructor , 'arguments' => $ dependency -> getArguments ( ) , ] ) ; // Store the instance $ dependency -> setInstance ( $ instance ) ; } } // Store position for injection if type = configuration if ( null === $ dependency -> getPosition ( ) ) { $ position = ( isset ( $ injections [ $ dependency -> getType ( ) ] ) ) ? count ( $ injections [ $ dependency -> getType ( ) ] ) + 1 : 1 ; $ dependency -> setPosition ( $ position ) ; } $ injections [ $ dependency -> getType ( ) ] [ ] = [ 'instance' => $ dependency -> getInstance ( ) , 'position' => $ dependency -> getPosition ( ) , 'target' => $ dependency -> getTarget ( ) , ] ; } } // Process injections, create instance and return it return $ this -> createInstance ( $ recipe [ 'className' ] , $ recipe [ 'constructor' ] , $ arguments , $ injections ) ; }
Instantiates a class including it dependencies.

This method is intend to instantiate a class and pass the required dependencies to it.
The dependencies are pre-configured and passed to this method as $recipe. The className is
the name of the class to instantiate and arguments is an (optional) array of arguments
which are passed to the class as additional arguments when instantiating.

@param array $recipe    The recipe for instantiating (contains array of depencies, arguments, ...)
@param array $arguments Arguments to pass to instance.

@author Benjamin Carl <opensource@clickalicious.de>

@return object The new created instance

@throws Doozr_Di_Exception
Instantiates a class including it dependencies .