百度百科的定義: 計算機科學裏的宏(Macro),是一種批量處理的稱謂。通常說來,宏是一種規則或模式,或稱語法替換 ,用於說明某一特定輸入(一般是字符串)如何根據預約義的規則轉換成對應的輸出(一般也是字符串)。這種替換在預編譯時進行,稱做宏展開。php
<?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]);
}
複製代碼
這個方法就比較簡單沒什麼複雜可言,就判斷是否存在宏指令。一般是使用宏指令以前判斷一下。ide
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對象和類做用域。this
Illuminate\Filesystem\Filesystem::class
,咱們想爲這個類增長一個方法,但不會動到裏面的代碼。
App\Providers\AppServiceProvider::register
方法增長宏指令(你也能夠專門新建一個服務提供者專門處理)
原文地址code