Support php's array callable format for macro's.

This allows you to define methods like this as well: Str::macro('name', [$this, 'method']);
This commit is contained in:
Ezra Pool
2018-02-03 21:10:48 +01:00
parent 2b1273c45e
commit 8ab907f219
2 changed files with 24 additions and 5 deletions
+16 -2
View File
@@ -319,10 +319,9 @@ class Alias
$properties = $reflection->getStaticProperties(); $properties = $reflection->getStaticProperties();
$macros = isset($properties['macros']) ? $properties['macros'] : []; $macros = isset($properties['macros']) ? $properties['macros'] : [];
foreach ($macros as $macro_name => $macro_func) { foreach ($macros as $macro_name => $macro_func) {
$function = new \ReflectionFunction($macro_func);
// Add macros // Add macros
$this->methods[] = new Macro( $this->methods[] = new Macro(
$function, $this->getMacroFunction($macro_func),
$this->alias, $this->alias,
$reflection, $reflection,
$macro_name, $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. * Output an error.
* *
+8 -3
View File
@@ -10,14 +10,19 @@ class Macro extends Method
/** /**
* Macro constructor. * Macro constructor.
* *
* @param \ReflectionFunction $method * @param \ReflectionFunctionAbstract $method
* @param string $alias * @param string $alias
* @param \ReflectionClass $class * @param \ReflectionClass $class
* @param null $methodName * @param null $methodName
* @param array $interfaces * @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->method = $method;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
$this->name = $methodName ?: $method->name; $this->name = $methodName ?: $method->name;