Skip to content

Commit

Permalink
Merge branch 'collection-macro-classes' of https://github.com/sebasti…
Browse files Browse the repository at this point in the history
…aanluca/framework into sebastiaanluca-collection-macro-classes
  • Loading branch information
taylorotwell committed Jun 27, 2017
2 parents 824c29b + 7908979 commit 3b91f32
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Contracts/Support/Macro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Contracts\Support;

interface Macro
{
/**
* Return the callable macro.
*
* @return callable
*/
public function handle();
}
20 changes: 14 additions & 6 deletions src/Illuminate/Support/Traits/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use BadMethodCallException;
use Illuminate\Contracts\Support\Macro;

trait Macroable
{
Expand All @@ -17,11 +18,12 @@ trait Macroable
/**
* Register a custom macro.
*
* @param string $name
* @param callable $macro
* @param string $name
* @param \Illuminate\Contracts\Support\Macro|callable $macro
*
* @return void
*/
public static function macro($name, callable $macro)
public static function macro($name, $macro)
{
static::$macros[$name] = $macro;
}
Expand Down Expand Up @@ -74,10 +76,16 @@ public function __call($method, $parameters)
throw new BadMethodCallException("Method {$method} does not exist.");
}

if (static::$macros[$method] instanceof Closure) {
return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
$macro = static::$macros[$method];

if (is_string($macro) && is_a($macro, Macro::class, true)) {
$macro = (new $macro)->handle();
}

return call_user_func_array(static::$macros[$method], $parameters);
if ($macro instanceof Closure) {
return call_user_func_array($macro->bindTo($this, static::class), $parameters);
}

return call_user_func_array($macro, $parameters);
}
}

0 comments on commit 3b91f32

Please sign in to comment.