Laravel中Macroable宏指令的作用是什么
创始人
2024-12-07 14:23:17
0次

首先完整的源码

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];
 
 if ($macro instanceof Closure) {
  return call_user_func_array($macro->bindTo($this, static::class), $parameters);
 }
 
 return call_user_func_array($macro, $parameters);
 }
}

Macroable::macro方法

public static function macro($name, $macro)
{
 static::$macros[$name] = $macro;
}

很简单的代码,根据参数的注释,$macro可以传一个闭包或者对象,之所以可以传对象,多亏了PHP中的魔术方法

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();

Macroable::mixin方法

这个方法是把一个对象的方法的返回结果注入到原对象中

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可以将一个类的方法绑定到宏类中。需要注意的就是,方法必须是返回一个闭包类型。

* Macroable::hasMacro方法

public static function hasMacro($name)
{
 return isset(static::$macros[$name]);
}

这个方法就比较简单没什么复杂可言,就判断是否存在宏指令。通常是使用宏指令之前判断一下。

* Macroable::__call和Macroable::__callStatic方法

正是由于这两个方法,我们才能进行宏操作,两个方法除了执行方式不同,代码大同小异。这里讲一下__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方法。

官网对Closure::bindTo的解释:复制当前闭包对象,绑定指定的$this对象和类作用域。

Laravel 中对类增加宏指令

Laravel中很多类都使用了宏这个trait

Laravel中Macroable宏指令的作用是什么

比如Illuminate\Filesystem\Filesystem::class,我们想为这个类增加一个方法,但不会动到里面的代码。

1. 我们只需要到App\Providers\AppServiceProvider::register方法增加宏指令(你也可以专门新建一个服务提供者专门处理)

Laravel中Macroable宏指令的作用是什么

2. 然后增加一条测试路由,测试我们新增加的方法

Laravel中Macroable宏指令的作用是什么

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...