首页 > 其他 > 详细

每天laravel-20160815| Container -18

时间:2016-05-26 11:39:33      阅读:267      评论:0      收藏:0      [点我收藏+]
    /**
     * Resolve a non-class hinted dependency.
     *
     * @param  \ReflectionParameter  $parameter
     * @return mixed
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    protected function resolveNonClass(ReflectionParameter $parameter)
    {// the parameter is a RefectionParameter $parameter
        if (! is_null($concrete = $this->getContextualConcrete(‘$‘.$parameter->name))) {
           // if can get the concrete
            if ($concrete instanceof Closure) {// if the concrete is a instance of Closure
                return call_user_func($concrete, $this);// return the call_user_func
            } else {
                return $concrete;// return concrete
            }
        }

        if ($parameter->isDefaultValueAvailable()) {// if has
            return $parameter->getDefaultValue();// return it
        }
// other ,set the bad message and return it
       // some time the message is very good use double " ,yeah
        $message = "Unresolvable dependency resolving [$parameter] in class {$parameter->getDeclaringClass()->getName()}";

        throw new BindingResolutionException($message);// throw bad message
    }

    /**
     * Resolve a class based dependency from the container.
     *
     * @param  \ReflectionParameter  $parameter
     * @return mixed
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    protected function resolveClass(ReflectionParameter $parameter)
    {// the class is based dependency from the container.
        try {
            return $this->make($parameter->getClass()->name);
        }// try check this class name whether can be make

        // If we can not resolve the class instance, we will check to see if the value
        // is optional, and if it is we will return the optional parameter value as
        // the value of the dependency, similarly to how we do this with scalars.
        catch (BindingResolutionException $e) {// catch the exception
            if ($parameter->isOptional()) {// if we has the Optional
                return $parameter->getDefaultValue();
            }

            throw $e;// throw $e
        }
    }


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1783327

每天laravel-20160815| Container -18

原文:http://jingshanls.blog.51cto.com/3357095/1783327

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!