Skip to content

Commit

Permalink
Adding macroable capabilities to the Guard (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian O'Sullivan authored and tymondesigns committed Jun 19, 2018
1 parent 392f34d commit c945cfb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/JWTGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Support\Traits\Macroable;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Contracts\Auth\UserProvider;
use Tymon\JWTAuth\Exceptions\UserNotDefinedException;

class JWTGuard implements Guard
{
use GuardHelpers;
use GuardHelpers, Macroable {
__call as macroCall;
}

/**
* The user we last attempted to retrieve.
Expand Down Expand Up @@ -435,6 +438,10 @@ public function __call($method, $parameters)
return call_user_func_array([$this->jwt, $method], $parameters);
}

if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}

throw new BadMethodCallException("Method [$method] does not exist.");
}
}
13 changes: 13 additions & 0 deletions tests/JWTGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,17 @@ public function it_should_get_the_payload()
$this->jwt->shouldReceive('getPayload')->once()->andReturn(Mockery::mock(Payload::class));
$this->assertInstanceOf(Payload::class, $this->guard->payload());
}

/**
* @test
* @group laravel-5.2
*/
public function it_should_be_macroable()
{
$this->guard->macro('foo', function () {
return 'bar';
});

$this->assertEquals('bar', $this->guard->foo());
}
}

0 comments on commit c945cfb

Please sign in to comment.