每天laravel-20160813| Container -16

   /**
    * Get the contextual concrete binding for the given abstract.
    *
    * @param  string  $abstract
    * @return string|null
    */
   protected function getContextualConcrete($abstract)
   {
       if (isset($this->contextual[end($this->buildStack)][$abstract])) {
           return $this->contextual[end($this->buildStack)][$abstract];
       }
   }//has it then return it,
// this array is too complex

   /**
    * Normalize the given class name by removing leading slashes.
    *
    * @param  mixed  $service
    * @return mixed
    */
   protected function normalize($service)
   {
       return is_string($service) ? ltrim($service, ‘\\‘) : $service;// back the normal string like namespace
   }// Normalize the given class name by removing leading slashes

   /**
    * Get the extender callbacks for a given type.
    *
    * @param  string  $abstract
    * @return array
    */
   protected function getExtenders($abstract)
   {
       if (isset($this->extenders[$abstract])) {// has then return
           return $this->extenders[$abstract];
       }

       return [];
   }// Get the extender callbacks for a given type.

   /**
    * Instantiate a concrete instance of the given type.
    *
    * @param  string  $concrete
    * @param  array   $parameters
    * @return mixed
    *
    * @throws \Illuminate\Contracts\Container\BindingResolutionException
    */
// make a concrete instance of the given type live.
   public function build($concrete, array $parameters = [])// like to make a floor
   {
       // If the concrete type is actually a Closure, we will just execute it and
       // hand back the results of the functions, which allows functions to be
       // used as resolvers for more fine-tuned resolution of these objects.
       if ($concrete instanceof Closure) {
           return $concrete($this, $parameters);
       }// if it a concrete as Closure.

       $reflector = new ReflectionClass($concrete);// else throw the concrete function to get the class name

       // If the type is not instantiable, the developer is attempting to resolve
       // an abstract type such as an Interface of Abstract Class and there is
       // no binding registered for the abstractions so we need to bail out.
       if (! $reflector->isInstantiable()) {// if it a abstract class so can not be instance
           if (! empty($this->buildStack)) {// if has the buildStack
               $previous = implode(‘, ‘, $this->buildStack);//implode the buildStack

               $message = "Target [$concrete] is not instantiable while building [$previous].";// it is a trace
           } else {
               $message = "Target [$concrete] is not instantiable.";
           }

           throw new BindingResolutionException($message);// throw a Exception
       }

       $this->buildStack[] = $concrete;// add the concrete to the concrete

       $constructor = $reflector->getConstructor();// get the Constructor

       // If there are no constructors, that means there are no dependencies then
       // we can just resolve the instances of the objects right away, without
       // resolving any other types or dependencies out of these containers.
       if (is_null($constructor)) {
           array_pop($this->buildStack);// if no has the array_pop  delete the concrete

           return new $concrete;// return new concrete
       }

       $dependencies = $constructor->getParameters();//get parameters

       // Once we have all the constructor‘s parameters we can create each of the
       // dependency instances and then use the reflection instances to make a
       // new instance of this class, injecting the created dependencies in.
       $parameters = $this->keyParametersByArgument(
           $dependencies, $parameters
       );// get

       $instances = $this->getDependencies(
           $dependencies, $parameters
       );

       array_pop($this->buildStack);

       return $reflector->newInstanceArgs($instances);
   }// build function is so powerful, can use a instance and a parameters to new a new instance.
时间: 2024-08-03 09:51:38

每天laravel-20160813| Container -16的相关文章

laravel容器container 阅读记录

今天抽时间又仔细看了一下laravel的container,记录一下. 所谓容器,听名字就知道,是一个仓库,装东西用的,所以,container所有的功能,都围绕一个主题:管理装. 类名称:Illuminate\Container\Container 首先,生成一个数组绑定列表,用自定义名称作为主键,然后键值是闭包(输入的可能是闭包或者实体类,但是,在存储的时候,都统一转化成了闭包存储). 其次,根据绑定列表,生成对应的类的实例,供用户使用,调用的时候,发现如果已经生成,不需要重新生成使用,实际

[转载]Laravel Container (容器) 深入理解 (下)

本文大部分翻译自 DAVE JAMES MILLER 的 <Laravel's Dependency Injection Container in Depth> . 上文介绍了 Dependency Injection Containers (容器) 的基本概念,现在接着深入讲解 Laravel 的 Container. Laravel 中实现的 Inversion of Control (IoC) / Dependency Injection (DI) Container 非常强悍,但文档中

Laravel框架怎样使用阿里云ACE缓存服务

Laravel框架怎样使用阿里云ACE缓存服务 之前我写了一篇在 Laravel 4 框架中使用阿里云 OCS 缓存的文章.介绍了怎样通过扩展 Laravel 4 来支持须要 SASL 认证的阿里云 OCS 缓存服务.有网友问我.ACE 的缓存怎么在 Laravel 4 中使用.我本来认为应该能够全然用同样的办法,后来自己尝试的时候才发现,ACE 的缓存区别很大.所以再写一篇,介绍一下怎样在 Laravel 框架中使用阿里云 ACE 的缓存服务. 怎样扩展 Laravel 的缓存驱动 在 Lar

Laravel框架如何使用阿里云ACE缓存服务

Laravel框架如何使用阿里云ACE缓存服务 之前我写了一篇在 Laravel 4 框架中使用阿里云 OCS 缓存的文章,介绍了如何通过扩展 Laravel 4 来支持需要 SASL 认证的阿里云 OCS 缓存服务.有网友问我,ACE 的缓存怎么在 Laravel 4 中使用.我本来觉得应该可以完全用相同的办法,后来自己尝试的时候才发现,ACE 的缓存差别非常大.所以再写一篇,介绍一下如何在 Laravel 框架中使用阿里云 ACE 的缓存服务. 如何扩展 Laravel 的缓存驱动 在 La

laravel cookie加密解密原理

通过控制台的 cookie 信息我们会发现,每次请求之后,关键的 cookie,如PHPSESSID.XSRF-TOKEN 都会发生变化,并且都是很长的一串字符串. 其实这是一个 json 数组,其中包含了 iv,value,mac 三个字段: 这些字段都是在框架加密解密的时候使用的,加密方法是 openssl_encrypt: 对 openssl 不太了解的可以看下下面的例子: $data = 'laravel'; $iv = random_bytes(16); $key = 'this is

echarts学习之——电力迁徙图(一)

今天主要就是在搞echarts,我们都知道他为我们提供了丰富的api方法,使我们能够迅速的搭建图标.同时他里面还有许多的案例, 其中就有这么一个国内航线模拟迁徙的地图,如下所示: 而我们通常因为各种需求原因,不想要全国地图,而单单想要一个省,或一个市的地图,进行类似模拟迁徙的效果,就比如我接下来要做的这个效果图: 首先第一个案例中的demo代码我就不说了,可以到http://gallery.echartsjs.com/editor.html?c=xSyn_h87Sg 进行下载 接下来的步骤为:

浮动法布局

1.效果图 2.代码 2.1 jsp 1 <%-- 2 Created by IntelliJ IDEA. 3 User: wcy 4 Date: 2016/11/15 5 Time: 21:16 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="ja

asp.net core 依赖注入问题

最近.net core可以跨平台了,这是一个伟大的事情,为了可以赶上两年以后的跨平台部署大潮,我也加入到了学习之列.今天研究的是依赖注入,但是我发现一个问题,困扰我很久,现在我贴出来,希望可以有人帮忙解决或回复一下. 背景:我测试.net自带的依赖注入生命周期,一共三个:Transient.Scope.Single三种,通过一个GUID在界面展示,但是我发现scope和single的每次都是相同的,并且single实例的guid值每次都会改变. 通过截图可以看到scope和Single每次浏览器

DOM+Javascript一些实例

1.内容+遮罩层+悬浮对话框 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <style> 7 .hide{ 8 display: none; 9 } 10 .c1{ 11 background: rgba(0,0,0,.5); 12 po