From 8ab907f219579677f622f30ea0ac21b8d628398e Mon Sep 17 00:00:00 2001 From: Ezra Pool Date: Fri, 2 Feb 2018 10:37:47 +0100 Subject: [PATCH] Support php's array callable format for macro's. This allows you to define methods like this as well: Str::macro('name', [$this, 'method']); --- src/Alias.php | 18 ++++++++++++++++-- src/Macro.php | 11 ++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Alias.php b/src/Alias.php index 4902f80..171f373 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -319,10 +319,9 @@ class Alias $properties = $reflection->getStaticProperties(); $macros = isset($properties['macros']) ? $properties['macros'] : []; foreach ($macros as $macro_name => $macro_func) { - $function = new \ReflectionFunction($macro_func); // Add macros $this->methods[] = new Macro( - $function, + $this->getMacroFunction($macro_func), $this->alias, $reflection, $macro_name, @@ -333,6 +332,21 @@ class Alias } } + /** + * @param $macro_func + * + * @return \ReflectionFunctionAbstract + * @throws \ReflectionException + */ + protected function getMacroFunction($macro_func) + { + if (is_array($macro_func) && is_callable($macro_func)) { + return new \ReflectionMethod($macro_func[0], $macro_func[1]); + } + + return new \ReflectionFunction($macro_func); + } + /** * Output an error. * diff --git a/src/Macro.php b/src/Macro.php index 521a3f0..2d6081e 100644 --- a/src/Macro.php +++ b/src/Macro.php @@ -10,14 +10,19 @@ class Macro extends Method /** * Macro constructor. * - * @param \ReflectionFunction $method + * @param \ReflectionFunctionAbstract $method * @param string $alias * @param \ReflectionClass $class * @param null $methodName * @param array $interfaces */ - public function __construct(\ReflectionFunction $method, $alias, $class, $methodName = null, $interfaces = array()) - { + public function __construct( + \ReflectionFunctionAbstract $method, + $alias, + $class, + $methodName = null, + $interfaces = array() + ) { $this->method = $method; $this->interfaces = $interfaces; $this->name = $methodName ?: $method->name;