Application

  • .env 配置文件APP_ENV=local参数
# APP_ENV 判断当前环境,好处理不同的逻辑问题
// 获取当前环境参数 local 本地
app()->environment();          //> local
// 判断当前环境 是否是 local 返回值 bool(布尔值) true | false
app()->environment('local');   //> true
app()->environment('local1');  //> false
# environment() 源码
    /**
     * Get or check the current application environment.
     *
     * @return string|bool
     */
    public function environment()
    {
        // func_num_args() 获取传递参数
        if (func_num_args() > 0) {
            $patterns = is_array(func_get_arg(0)) ? 
                        func_get_arg(0) : func_get_args();

            foreach ($patterns as $pattern) {
                if (Str::is($pattern, $this['env'])) {
                    return true;
                }
            }

            return false;
        }

        return $this['env'];
    }

bound($abstract)

# 判断当前是否绑定
    /**
     * Determine if the given abstract type has been bound.
     *
     * (Overriding Container::bound)
     *
     * @param  string  $abstract
     * @return bool
     */
    public function bound($abstract)
    {
        return isset($this->deferredServices[$abstract]) || parent::bound($abstract);
    }
# 示例
app()->bound('config');   //> bool 判断config是否绑定了

bind($abstract, $concrete = null, $shared = false)

    # 绑定实例到服务器容器中 bindings[] 容器
    /**
     * Register a binding with the container.
     *
     * 容器 键
     * @param  string  $abstract
     * 容器 绑定值 (bind对象 需要对象实例)
     * @param  \Closure|string|null  $concrete
     * 是否贡献实例(单例模式 true) (默认 false 非单例模式)
     * @param  bool  $shared
     * @return void
     */
    public function bind($abstract, $concrete = null, $shared = false)
    {
        // If no concrete type was given, we will simply set the concrete type to the
        // abstract type. After that, the concrete type to be registered as shared
        // without being forced to state their classes in both of the parameters.
        // 删除其他形式注册的服务容器
        // instances容器、aliases别名容器 之前注册的
        $this->dropStaleInstances($abstract);

        if (is_null($concrete)) {
            $concrete = $abstract;
        }

        // If the factory is not a Closure, it means it is just a class name which is
        // bound into this container to the abstract type and we will just wrap it
        // up inside its own Closure to give us more convenience when extending.
        // 如果 $concrete 不是函数情况; 以类的形式注册注册
        if (! $concrete instanceof Closure) {
            // 返回一个闭包 函数 $concrete
            $concrete = $this->getClosure($abstract, $concrete);
        }

        $this->bindings[$abstract] = compact('concrete', 'shared');

        // If the abstract type was already resolved in this container we'll fire the
        // rebound listener so that any objects which have already gotten resolved
        // can have their copy of the object updated via the listener callbacks.
        if ($this->resolved($abstract)) {
            $this->rebound($abstract);
        }
    }

你可能感兴趣的:(Application)