计算机科学里的宏(Macro),是一种批量处理的称谓。通常说来,宏是一种规则或模式,或称语法替换 ,用于说明某一特定输入(一般是字符串)如何根据预约义的规则转换成对应的输出(一般也是字符串)。这种替换在预编译时进行,称做宏展开。闭包
完整的源码以下:函数
namespace Illuminate\Support\Traits; use Closure; use ReflectionClass; use ReflectionMethod; use BadMethodCallException; trait Macroable { /** * The registered string macros. * * @var array */ protected static $macros = []; /** * Register a custom macro. * * @param string $name * @param object|callable $macro * * @return void */ public static function macro($name, $macro) { static::$macros[$name] = $macro; } /** * Mix another object into the class. * * @param object $mixin * @return void */ public static function mixin($mixin) { $methods = (new ReflectionClass($mixin))->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED ); foreach ($methods as $method) { $method->setAccessible(true); static::macro($method->name, $method->invoke($mixin)); } } /** * Checks if macro is registered. * * @param string $name * @return bool */ public static function hasMacro($name) { return isset(static::$macros[$name]); } /** * Dynamically handle calls to the class. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public static function __callStatic($method, $parameters) { if (! static::hasMacro($method)) { throw new BadMethodCallException("Method {$method} does not exist."); } if (static::$macros[$method] instanceof Closure) { return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); } return call_user_func_array(static::$macros[$method], $parameters); } /** * Dynamically handle calls to the class. * * @param string $method * @param array $parameters * @return mixed * * @throws \BadMethodCallException */ public function __call($method, $parameters) { if (! static::hasMacro($method)) { throw new BadMethodCallException("Method {$method} does not exist."); } $macro = static::$macros[$method]; // $macro 是闭包函数时 if ($macro instanceof Closure) { return call_user_func_array($macro->bindTo($this, static::class), $parameters); } //$macro 是对象时,此时就会执行 `对象()`,从而触发魔术函数`__invoke()`。这就是为何注册指令时,macro 能够传闭包或者对象的缘由 return call_user_func_array($macro, $parameters); } }
用户注册宏指令, 动态添加方法到类this
public static function macro($name, $macro) { static::$macros[$name] = $macro; }
参数的注释,$macro能够传一个闭包或者对象,之因此能够传对象,多亏了PHP中的魔术方法spa
class Father { // 经过增长魔术方法**__invoke**咱们就能够把对象当作闭包来使用了。 public function __invoke() { echo __CLASS__; } } class Child { use \Illuminate\Support\Traits\Macroable; } // 增长了宏指令以后,咱们就能调用 Child 对象中不存在的方法了 Child::macro('show', new Father); // 输出:Father (new Child)->show();
经过增长魔术方法**__invoke**咱们就能够把对象当作闭包来使用了,怎么理解? 原理:先理解闭包函数的回调, call_user_func_array(function() {}, $params), 而咱们使用宏指令时,Child::macro('show', new Father);
先注册show
指令,对应的macro是一个对象,咱们执行(new Child)->show();
时,Child
对象没有找到show
方法,此时会调用魔术方法 __call
,进而会执行 __call
中的code
return call_user_func_array($macro, $parameters);
此刻的 $macro 非闭包函数,而是 new Father 对象,至关于回调执行的是 call_user_func_array(对象(), $parameters);
,这种把对象当函数执行(对象()
)时,就会触发魔术方法 __invoke()
对象
经过增长魔术方法__invoke就能够把对象当作闭包来使用字符串
这个方法是把一个对象的方法的返回结果注入到原对象中get
public static function mixin($mixin) { // 经过反射获取该对象中全部公开和受保护的方法 $methods = (new ReflectionClass($mixin))->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED ); foreach ($methods as $method) { // 设置方法可访问,由于受保护的不能在外部调用 $method->setAccessible(true); // 调用 macro 方法批量建立宏指令 static::macro($method->name, $method->invoke($mixin)); } } // 实际使用 class Father { public function say() { return function () { echo 'say'; }; } public function show() { return function () { echo 'show'; }; } protected function eat() { return function () { echo 'eat'; }; } } class Child { use \Illuminate\Support\Traits\Macroable; } // 批量绑定宏指令 Child::mixin(new Father); $child = new Child; // 输出:say $child->say(); // 输出:show $child->show(); // 输出:eat $child->eat();
在上面的代码能够看出mixin能够将一个类的方法绑定到宏类中。须要注意的就是,方法必须是返回一个闭包类型。源码
public static function hasMacro($name) { return isset(static::$macros[$name]); }
这个方法就比较简单没什么复杂可言,就判断是否存在宏指令。一般是使用宏指令以前判断一下。string
正是因为这两个方法,咱们才能进行宏操做,两个方法除了执行方式不一样,代码大同小异。这里讲一下__call
public function __call($method, $parameters) { // 若是不存在这个宏指令,直接抛出异常 if (! static::hasMacro($method)) { throw new BadMethodCallException("Method {$method} does not exist."); } // 获得存储的宏指令 $macro = static::$macros[$method]; // 闭包作一点点特殊的处理 if ($macro instanceof Closure) { return call_user_func_array($macro->bindTo($this, static::class), $parameters); } // 不是闭包,好比对象的时候,直接经过这种方法运行,可是要确保对象有`__invoke`方法 return call_user_func_array($macro, $parameters); } class Child { use \Illuminate\Support\Traits\Macroable; protected $name = 'father'; } // 闭包的特殊处理,须要作的就是绑定 $this, 如 Child::macro('show', function () { echo $this->name; }); // 输出:father (new Child)->show();
在上面的操做中咱们绑定宏时,在闭包中能够经过$this来调用Child的属性,是由于在__call方法中咱们使用Closure::bindTo方法。
使用宏指令
时,指令是对应的闭包函数时,执行的就时闭包函数;指令对应的时对象时,执行的就对象()
,也就是把对象函数执行,进而触发魔术方法__invoke
,最后也就是说指令间接执行的是__invoke
方法中的代码。