1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | /** * Register a binding with the container. * * @param string $abstract * @param \Closure|string|null $concrete * @param bool $shared * @return void */ public function bind( $abstract , $concrete = null, $shared = false) { $this ->dropStaleInstances( $abstract ); // 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. 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. if (! $concrete instanceof Closure) { $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 ); } } /** * Drop all of the stale instances and aliases. * * @param string $abstract * @return void */ protected function dropStaleInstances( $abstract ) { unset( $this ->instances[ $abstract ], $this ->aliases[ $abstract ]); } /** * Get the Closure to be used when building a type. * * @param string $abstract * @param string $concrete * @return \Closure */ protected function getClosure( $abstract , $concrete ) { return function ( $container , $parameters = []) use ( $abstract , $concrete ) { if ( $abstract == $concrete ) { return $container ->build( $concrete ); } return $container ->resolve( $concrete , $parameters , $raiseEvents = false ); }; } /** * Determine if the given abstract type has been resolved. * * @param string $abstract * @return bool */ public function resolved( $abstract ) { if ( $this ->isAlias( $abstract )) { $abstract = $this ->getAlias( $abstract ); } return isset( $this ->resolved[ $abstract ]) || isset( $this ->instances[ $abstract ]); } /** * Fire the "rebound" callbacks for the given abstract type. * * @param string $abstract * @return void */ protected function rebound( $abstract ) { $instance = $this ->make( $abstract ); foreach ( $this ->getReboundCallbacks( $abstract ) as $callback ) { call_user_func( $callback , $this , $instance ); } } |
bind第一步是销毁了之前绑定的instances和aliases,如果$concrete是空,那么就绑定$abstract自己
如果concrete不是闭包 就转换成闭包
1 | $this ->getClosure( $abstract , $concrete ); |
绑定自身的话可以看到返回的是
return $container->build($concrete);
大概可以理解成
1 2 3 4 5 6 7 8 9 10 11 12 | $bindings = [ 'HelpSpot\API' => [ //闭包绑定 'concrete' => function ( $app , $paramters = []) { return $app ->build( 'HelpSpot\API' ); }, 'shared' => false //如果是singleton绑定,这个值为true ] 'Illuminate\Tests\Container\IContainerContractStub' => [ //接口实现绑定 'concrete' => 'Illuminate\Tests\Container\ContainerImplementationStub' , 'shared' => false ] ] |