From 755674f3ea2e9318685913e90e6a2afe56f1445b Mon Sep 17 00:00:00 2001 From: Robin Valk Date: Wed, 31 Aug 2016 21:27:08 +0200 Subject: [PATCH] Add support for guards in Laravel Auth extention Via this you can check for different guards without having to use the `auth_guard` helper all the time. --- src/Extension/Laravel/Auth.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Extension/Laravel/Auth.php b/src/Extension/Laravel/Auth.php index fffa0783..1b2a96c3 100644 --- a/src/Extension/Laravel/Auth.php +++ b/src/Extension/Laravel/Auth.php @@ -49,10 +49,27 @@ public function getName() public function getFunctions() { return [ - new Twig_SimpleFunction('auth_check', [$this->auth, 'check']), - new Twig_SimpleFunction('auth_guest', [$this->auth, 'guest']), - new Twig_SimpleFunction('auth_user', [$this->auth, 'user']), + new Twig_SimpleFunction('auth_check', $this->generateCallable('check')), + new Twig_SimpleFunction('auth_guest', $this->generateCallable('guest')), + new Twig_SimpleFunction('auth_user', $this->generateCallable('user')), new Twig_SimpleFunction('auth_guard', [$this->auth, 'guard']), ]; } + + /** + * Generates a callable using a guard for the AuthManager + * + * @param $methodName + * + * @return \Closure + */ + private function generateCallable($methodName) + { + return function ($guard = 'web') use ($methodName) { + $params = func_get_args(); + $guard = array_shift($params); + + return $this->auth->guard($guard)->$methodName($params); + }; + } }