├── src ├── .gitkeep ├── helpers.php └── App.php ├── .editorconfig ├── composer.json ├── Zh-README.md └── README.md /src/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | register($instance); 14 | } 15 | } -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = false 10 | 11 | [*.{vue,js,scss}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | end_of_line = lf 16 | insert_final_newline = true 17 | trim_trailing_whitespace = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iidestiny/dependency-injection", 3 | "description": "Enable your methods to use dependency injection", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "iidestiny", 8 | "email": "iidestiny@vip.qq.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=7.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Iidestiny\\DependencyInjection\\": "src" 17 | }, 18 | "files": [ 19 | "src/helpers.php" 20 | ] 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^6.5" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Zh-README.md: -------------------------------------------------------------------------------- 1 |
让你自定义的方法也可以使用依赖注入.
4 | 5 | ## 文档 6 | 7 | - [English document](https://github.com/iiDestiny/dependency-injection/blob/master/README.md) 8 | 9 | ## 要求 10 | 11 | - PHP >= 7.0 12 | 13 | ## 安装 14 | 15 | ```bash 16 | composer require iidestiny/dependency-injection -vvv 17 | ``` 18 | 19 | ## 使用 20 | 21 | 使用辅助方法 22 | 23 | ```php 24 | // 注册你的自定义类 25 | di_register(Tools::class) 26 | 27 | // 调用你类中的方法 28 | di_register(Tools::class)->generate($param, $param, $param) 29 | 30 | // 类所有方法都可以调用 31 | di_register(Tools::class)->foo($bar) 32 | ``` 33 | 34 | ## 实例 35 | 36 | 例如有时候我们自定义的 Service 服务层可能也需要依赖注入其他工具类,但是我们控制器中已经依赖注入了 Service,调用 Service 中方法的时候就不能轻易的注入其他工具类,使用这个扩展包可以轻易解决这个问题,看下面例子。 37 | 38 | ```php 39 | placeOrder($user, $goods, $address); 90 | } 91 | ``` 92 | 93 | 或者 94 | 95 | ```php 96 | /** 97 | * store 98 | */ 99 | public function store() 100 | { 101 | $orderService = di_register(OrderService::class); 102 | 103 | $orderService->placeOrder($user, $goods, $address); 104 | $orderService->pay($order); 105 | } 106 | ``` 107 | 108 | ## License 109 | 110 | MIT -------------------------------------------------------------------------------- /src/App.php: -------------------------------------------------------------------------------- 1 | 13 | */ 14 | class App 15 | { 16 | /** 17 | * The instance 18 | * 19 | * @var 20 | */ 21 | protected $instance; 22 | 23 | /** 24 | * Instance registered 25 | * 26 | * @param $instance 27 | * 28 | * @return string 29 | */ 30 | public function register($instance) 31 | { 32 | if (!is_object($instance)) { 33 | $this->instance = new $instance(); 34 | } else { 35 | $this->instance = $instance; 36 | } 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * Dependency injector 43 | * 44 | * Note that there are type parameters when using and need to pass in the pre-parameters 45 | * similar to optional parameters and non-optional parameter positions. 46 | * 47 | * @param $method 48 | * @param $parameters 49 | * 50 | * @return mixed 51 | * @throws \ReflectionException 52 | */ 53 | public function __call($method, $parameters) 54 | { 55 | if (!method_exists($this->instance, $method)) { 56 | $instance = get_class($this->instance); 57 | 58 | throw new InvalidArgumentException("Instance [{$instance}] does not exist for [{$method}] method"); 59 | } 60 | 61 | return $this->make($method, ...$parameters); 62 | } 63 | 64 | /** 65 | * make method 66 | * 67 | * @param $method 68 | * @param mixed ...$parameters 69 | * 70 | * @return mixed 71 | * @throws \ReflectionException 72 | */ 73 | public function make($method, ...$parameters) 74 | { 75 | $reflector = new ReflectionMethod($this->instance, $method); 76 | 77 | foreach ($reflector->getParameters() as $key => $parameter) { 78 | 79 | $class = $parameter->getClass(); 80 | 81 | if ($class) { 82 | $param = $parameters[$key] ?? null; 83 | 84 | if (is_object($param) && get_class($param) == $class->name) { 85 | continue; 86 | } 87 | 88 | array_splice($parameters, $key, 0, [ 89 | new $class->name(), 90 | ]); 91 | } 92 | } 93 | 94 | return call_user_func_array([$this->instance, $method], $parameters); 95 | } 96 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |Let your custom class methods also enjoy dependency injection.
11 | 12 |
13 | 感谢关注「GitHub 热门」公众号,带你了解技术圈内热门新鲜事!
14 |
15 |
16 |