Created
March 25, 2025 16:09
-
-
Save mizegit/848bfe40024021c75e0d38feb5be8dc6 to your computer and use it in GitHub Desktop.
laravel框架技术关键解析 代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface Visit | |
| { | |
| public function go(); | |
| } | |
| class Train implements Visit | |
| { | |
| public function go() | |
| { | |
| echo "go to tibet by train"; | |
| } | |
| } | |
| class Container | |
| { | |
| protected $bindings = []; | |
| public function bind($abstract, $concrete = null, $shared = false) | |
| { | |
| if (! $concrete instanceof Closure) { | |
| $concrete = $this->getClosure($abstract, $concrete); | |
| } | |
| $this->bindings[$abstract] = compact('concrete', 'shared'); | |
| } | |
| protected function getClosure($abstract, $concrete) | |
| { | |
| return function($c) use ($abstract, $concrete) { | |
| $method = ($abstract == $concrete) ? 'build' : 'make'; | |
| return $c->$method($concrete); | |
| }; | |
| } | |
| public function make($abstract) | |
| { | |
| $concrete = $this->getConcrete($abstract); | |
| if ($this->isBuildable($concrete, $abstract)) { | |
| $object = $this->build($concrete); | |
| } else { | |
| $object = $this->make($concrete); | |
| } | |
| return $object; | |
| } | |
| protected function isBuildable($concrete, $abstract) | |
| { | |
| return $concrete === $abstract || $concrete instanceof Closure; | |
| } | |
| protected function getConcrete($abstract) | |
| { | |
| if (! isset($this->bindings[$abstract])) { | |
| return $abstract; | |
| } | |
| return $concrete = $this->bindings[$abstract]['concrete']; | |
| } | |
| public function build($concrete) | |
| { | |
| if ($concrete instanceof Closure){ | |
| return $concrete($this); | |
| } | |
| $reflector = new ReflectionClass($concrete); | |
| if (! $reflector->isInstantiable()) { | |
| echo "Target [$concrete] is not instantiable."; | |
| } | |
| $constructor = $reflector->getConstructor(); | |
| if (is_null($constructor)) { | |
| return new $concrete; | |
| } | |
| $dependencies = $constructor->getParameters(); | |
| $instances = $this->getDependencies($dependencies); | |
| return $reflector->newInstanceArgs($instances); | |
| } | |
| protected function getDependencies($parameters) | |
| { | |
| $dependencies = []; | |
| foreach ($parameters as $parameter) | |
| { | |
| $dependency = $parameter->getClass(); | |
| if (is_null($dependency)) { | |
| $dependencies[] = NULL; | |
| } else { | |
| $dependencies[] = $this->resolveClass($parameter); | |
| } | |
| } | |
| return (array) $dependencies; | |
| } | |
| protected function resolveClass(ReflectionParameter $parameter) | |
| { | |
| return $this->make($parameter->getClass()->name); | |
| } | |
| } | |
| class Traveller | |
| { | |
| protected $trafficTool; | |
| public function __construct(Visit $trafficTool) | |
| { | |
| $this->trafficTool = $trafficTool; | |
| } | |
| public function visitTibet() | |
| { | |
| $this->trafficTool->go(); | |
| } | |
| } | |
| // 实例化 IoC 容器 | |
| $app = new Container(); | |
| // 完成容器的填充 | |
| $app->bind("Visit", "Train"); | |
| $app->bind("traveller", "Traveller"); | |
| // 通过容器实现依赖注入,完成类的实例化 | |
| $tra = $app->make("traveller"); | |
| $tra->visitTibet(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deepseek重写了一下,有点意思