3 namespace Illuminate\Support\Traits
;
8 use BadMethodCallException
;
13 * The registered string macros.
17 protected static $macros = [];
20 * Register a custom macro.
23 * @param object|callable $macro
27 public static function macro($name, $macro)
29 static::$macros[$name] = $macro;
33 * Mix another object into the class.
35 * @param object $mixin
38 public static function mixin($mixin)
40 $methods = (new ReflectionClass($mixin))->getMethods(
41 ReflectionMethod
::IS_PUBLIC | ReflectionMethod
::IS_PROTECTED
44 foreach ($methods as $method) {
45 $method->setAccessible(true);
47 static::macro($method->name
, $method->invoke($mixin));
52 * Checks if macro is registered.
57 public static function hasMacro($name)
59 return isset(static::$macros[$name]);
63 * Dynamically handle calls to the class.
65 * @param string $method
66 * @param array $parameters
69 * @throws \BadMethodCallException
71 public static function __callStatic($method, $parameters)
73 if (! static::hasMacro($method)) {
74 throw new BadMethodCallException("Method {$method} does not exist.");
77 if (static::$macros[$method] instanceof Closure
) {
78 return call_user_func_array(Closure
::bind(static::$macros[$method], null, static::class), $parameters);
81 return call_user_func_array(static::$macros[$method], $parameters);
85 * Dynamically handle calls to the class.
87 * @param string $method
88 * @param array $parameters
91 * @throws \BadMethodCallException
93 public function __call($method, $parameters)
95 if (! static::hasMacro($method)) {
96 throw new BadMethodCallException("Method {$method} does not exist.");
99 $macro = static::$macros[$method];
101 if ($macro instanceof Closure
) {
102 return call_user_func_array($macro->bindTo($this, static::class), $parameters);
105 return call_user_func_array($macro, $parameters);