protected function produceDependency($alias, $fallback = '_michaels_no_fallback')
    {
        /* Get the registered factory (string, closure, object, container, NoItemFoundMessage) */
        $factory = $this->getIfExists("$alias");

        /* Manage not founds and fallback */
        if ($factory instanceof NoItemFoundMessage) {
            if ($fallback !== '_michaels_no_fallback') {
                return $fallback;
            } elseif (class_exists($alias)) {
                return new $alias;
            } else {
                throw new ItemNotFoundException("$alias not found");
            }
        }

        /* Get any declared dependencies */
        $declared = $this->getIfExists("_declarations.$alias");
        $dependencies = [];

        // Now setup those dependencies into an array
        if (!$declared instanceof NoItemFoundMessage) {
            $dependencies = array_map(function(&$value) use ($alias) {
                if (is_string($value) && $this->exists("$alias")) {
                    return $this->get($value);
                }
                return $value;
            }, $declared);
        }

        /* Produce the object itself */
        if ($factory instanceof IocContainerInterface) {
            $object = $factory->get($alias);

        } elseif (is_string($factory)) {
            $class = new \ReflectionClass($factory);
            $object = $class->newInstanceArgs($dependencies);

        } elseif (is_callable($factory)) {
            array_unshift($dependencies, $this);
            $object = call_user_func_array($factory, $dependencies);

        } elseif (is_object($factory)) {
            $object = $factory;

            if (method_exists($object, "needs")) {
                call_user_func_array([$object, 'needs'], $dependencies);
            }

        } else {
            throw new \Exception("`get()` can only return from strings, callables, or objects");
        }

        /* Run the object through the pipeline, if desired */
        $pipeline = $this->getIfExists("_pipelines.$alias");

        if (!$pipeline instanceof NoItemFoundMessage) {
            /** @var \Closure $pipeline */
            $object = $pipeline($object, $this);
        }

        /* Return the final object */
        return $object;
    }
protected function produceDependency($alias, $fallback = '_michaels_no_fallback')
    {
        /* Get the registered factory (string, closure, object, container, NoItemFoundMessage) */
        $factory = $this->getIfExists("$alias");

        /* Manage not founds and fallback */
        if ($factory instanceof NoItemFoundMessage) {
            if ($fallback !== '_michaels_no_fallback') {
                return $fallback;
            } elseif (class_exists($alias)) {
                return new $alias;
            } else {
                throw new ItemNotFoundException("$alias not found");
            }
        }

        /* Get any declared dependencies */
        $declared = $this->getIfExists("_declarations.$alias");
        $dependencies = [];

        // Now setup those dependencies into an array
        if (!$declared instanceof NoItemFoundMessage) {
            $dependencies = array_map(function(&$value) use ($alias) {
                if (is_string($value) && $this->exists("$alias")) {
                    return $this->get($value);
                }
                return $value;
            }, $declared);
        }

        /* Produce the object itself */
        if ($factory instanceof IocContainerInterface) {
            $object = $factory->get($alias);

        } elseif (is_string($factory)) {
            $class = new \ReflectionClass($factory);
            $object = $class->newInstanceArgs($dependencies);

        } elseif (is_callable($factory)) {
            array_unshift($dependencies, $this);
            $object = call_user_func_array($factory, $dependencies);

        } elseif (is_object($factory)) {
            $object = $factory;

            if (method_exists($object, "needs")) {
                call_user_func_array([$object, 'needs'], $dependencies);
            }

        } else {
            throw new \Exception("`get()` can only return from strings, callables, or objects");
        }

        /* Run the object through the pipeline, if desired */
        $pipeline = $this->getIfExists("_pipelines.$alias");

        if (!$pipeline instanceof NoItemFoundMessage) {
            /** @var \Closure $pipeline */
            $object = $pipeline($object, $this);
        }

        /* Return the final object */
        return $object;
    }
protected function produceDependency ( $ alias , $ fallback = '_michaels_no_fallback' ) { /* Get the registered factory (string, closure, object, container, NoItemFoundMessage) */ $ factory = $ this -> getIfExists ( "$alias" ) ; /* Manage not founds and fallback */ if ( $ factory instanceof NoItemFoundMessage ) { if ( $ fallback !== '_michaels_no_fallback' ) { return $ fallback ; } elseif ( class_exists ( $ alias ) ) { return new $ alias ; } else { throw new ItemNotFoundException ( "$alias not found" ) ; } } /* Get any declared dependencies */ $ declared = $ this -> getIfExists ( "_declarations.$alias" ) ; $ dependencies = [ ] ; // Now setup those dependencies into an array if ( ! $ declared instanceof NoItemFoundMessage ) { $ dependencies = array_map ( function ( & $ value ) use ( $ alias ) { if ( is_string ( $ value ) && $ this -> exists ( "$alias" ) ) { return $ this -> get ( $ value ) ; } return $ value ; } , $ declared ) ; } /* Produce the object itself */ if ( $ factory instanceof IocContainerInterface ) { $ object = $ factory -> get ( $ alias ) ; } elseif ( is_string ( $ factory ) ) { $ class = new \ ReflectionClass ( $ factory ) ; $ object = $ class -> newInstanceArgs ( $ dependencies ) ; } elseif ( is_callable ( $ factory ) ) { array_unshift ( $ dependencies , $ this ) ; $ object = call_user_func_array ( $ factory , $ dependencies ) ; } elseif ( is_object ( $ factory ) ) { $ object = $ factory ; if ( method_exists ( $ object , "needs" ) ) { call_user_func_array ( [ $ object , 'needs' ] , $ dependencies ) ; } } else { throw new \ Exception ( "`get()` can only return from strings, callables, or objects" ) ; } /* Run the object through the pipeline, if desired */ $ pipeline = $ this -> getIfExists ( "_pipelines.$alias" ) ; if ( ! $ pipeline instanceof NoItemFoundMessage ) { /** @var \Closure $pipeline */ $ object = $ pipeline ( $ object , $ this ) ; } /* Return the final object */ return $ object ; }
Produces the object from an alias
@param string $alias
@param mixed|string $fallback
@return mixed
@throws ItemNotFoundException
@throws \Exception
Produces the object from an alias