今天就跟大家聊聊有關Laravel中Macroable宏指令的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
首先完整的源碼
<?php
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];
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

比如Illuminate\Filesystem\Filesystem::class,我們想為這個類增加一個方法,但不會動到里面的代碼。
1. 我們只需要到App\Providers\AppServiceProvider::register方法增加宏指令(你也可以專門新建一個服務提供者專門處理)

2. 然后增加一條測試路由,測試我們新增加的方法

看完上述內容,你們對Laravel中Macroable宏指令的作用是什么有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。