Implement multi purpose binder

This commit is contained in:
James Cole 2018-02-07 11:13:04 +01:00
parent 587ad1298d
commit eacc1da157
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 13 additions and 85 deletions

View File

@ -167,7 +167,7 @@ class Kernel extends HttpKernel
'auth:api' => [
'throttle:60,1',
'bindings',
HttpBinder::class,
],
];

View File

@ -1,80 +0,0 @@
<?php
/**
* ApiBinder.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
use Closure;
use FireflyIII\Support\Domain;
use Illuminate\Http\Request;
/**
* Class ApiBinder
*/
class ApiBinder
{
/**
* @var array
*/
protected $binders = [];
/**
* Binder constructor.
*/
public function __construct()
{
$this->binders = Domain::getBindables();
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {
$boundObject = $this->performBinding($key, $value, $request->route());
$request->route()->setParameter($key, $boundObject);
}
}
return $next($request);
}
/**
* @param $key
* @param $value
* @param $route
*
* @return mixed
*/
private function performBinding($key, $value, $route)
{
$class = $this->binders[$key];
return $class::routeBinder($value, $route);
}
}

View File

@ -24,7 +24,9 @@ namespace FireflyIII\Http\Middleware;
use Closure;
use FireflyIII\Support\Domain;
use Illuminate\Auth\SessionGuard;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
/**
* Class HttpBinder
@ -54,9 +56,16 @@ class HttpBinder
*/
public function handle(Request $request, Closure $next)
{
$middleware = $request->route()->middleware();
$guard = 'web';
if(in_array('auth:api', $middleware)) {
$guard = 'api';
}
$guard = auth()->guard($guard);
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {
$boundObject = $this->performBinding($key, $value, $request->route());
$boundObject = $this->performBinding($guard, $key, $value, $request->route());
$request->route()->setParameter($key, $boundObject);
}
}
@ -71,10 +80,9 @@ class HttpBinder
*
* @return mixed
*/
private function performBinding($key, $value, $route)
private function performBinding($guard, string $key, string $value, Route $route)
{
$class = $this->binders[$key];
return $class::routeBinder($value, $route);
return $class::routeBinder($guard, $value, $route);
}
}