8000 Fixes the problem with macros returning Renderable implementations by kirkbushell · Pull Request #7980 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Fixes the problem with macros returning Renderable implementations #7980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Additional support for renderable macros.
  • Loading branch information
kirkbushell committed Mar 12, 2015
commit 01568e210822a883e39a01fde53cc2db59f970d8
38 changes: 23 additions & 15 deletions src/Illuminate/Support/Traits/Macroable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

trait Macroable {

Expand Down Expand Up @@ -44,22 +45,29 @@ public static function hasMacro($name)
*
* @throws \BadMethodCallException
*/
public static function __callStatic($method, $parameters)
{
if (static::hasMacro($method))
{
if (static::$macros[$method] instanceof Closure)
{
return call_user_func_array(Closure::bind(static::$macros[$method], null, get_called_class()), $parameters);
}
else
{
return call_user_func_array(static::$macros[$method], $parameters);
}
}
public static function __callStatic($method, $parameters)
{
if (static::hasMacro($method))
{
if (static::$macros[$method] instanceof Closure)
{
$macro = call_user_func_array(Closure::bind(static::$macros[$method], null, get_called_class()), $parameters);
}
else
{
$macro = call_user_func_array(static::$macros[$method], $parameters);
}

throw new BadMethodCallException("Method {$method} does not exist.");
}
if ($macro instanceof Renderable)
{
return $macro->render();
}

return $macro;
}

throw new BadMethodCallException("Method {$method} does not exist.");
}

/**
* Dynamically handle calls to the class.
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportMacroableTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Mockery as m;

class SupportMacroableTest extends PHPUnit_Framework_TestCase {

private $macroable;
Expand Down Expand Up @@ -45,6 +47,19 @@ public function testWhenCallingMacroClosureIsBoundToObject()
$this->assertEquals('static', $result);
}


public function testCallingRenderableMacro()
{
$renderable = m::mock('Illuminate\Contracts\Support\Renderable');
$renderable->shouldReceive('render')->once()->andReturn('output');

TestMacroable::macro('tryRenderable', function() use ($renderable) {
return $renderable;
});

$this->assertEquals('output', TestMacroable::tryRenderable());
}

}

class TestMacroable {
Expand Down
0