firefly-iii/app/Http/Middleware/Binder.php

101 lines
2.6 KiB
PHP
Raw Normal View History

2016-01-09 08:39:02 -06:00
<?php
/**
2018-02-09 08:01:22 -06:00
* Binder.php
2018-02-07 03:49:06 -06:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-01-09 08:39:02 -06:00
namespace FireflyIII\Http\Middleware;
use Closure;
use FireflyIII\Support\Domain;
2018-02-09 07:47:37 -06:00
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request;
2018-02-07 04:13:04 -06:00
use Illuminate\Routing\Route;
2016-01-15 06:08:25 -06:00
/**
2018-02-07 03:49:06 -06:00
* Class HttpBinder
2016-01-15 06:08:25 -06:00
*/
2018-02-09 08:01:22 -06:00
class Binder
2016-01-09 08:39:02 -06:00
{
2018-02-09 07:47:37 -06:00
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
2017-12-17 07:30:53 -06:00
/**
* @var array
*/
2016-01-09 08:39:02 -06:00
protected $binders = [];
2016-01-09 08:53:11 -06:00
/**
* Binder constructor.
2018-02-09 07:47:37 -06:00
*
* @param \Illuminate\Contracts\Auth\Factory $auth
2016-01-09 08:53:11 -06:00
*/
2018-02-09 07:47:37 -06:00
public function __construct(Auth $auth)
2016-01-09 08:39:02 -06:00
{
$this->binders = Domain::getBindables();
2018-02-09 07:47:37 -06:00
$this->auth = $auth;
2016-01-09 08:39:02 -06:00
}
/**
* Handle an incoming request.
*
2018-02-09 07:47:37 -06:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
2016-01-09 08:39:02 -06:00
*
* @return mixed
2018-02-09 07:47:37 -06:00
*
* @throws \Illuminate\Auth\AuthenticationException
2016-01-09 08:39:02 -06:00
*/
2018-02-09 07:47:37 -06:00
public function handle($request, Closure $next, ...$guards)
2016-01-09 08:39:02 -06:00
{
2018-02-07 04:13:04 -06:00
$middleware = $request->route()->middleware();
$guard = 'web';
if(in_array('auth:api', $middleware)) {
$guard = 'api';
}
$guard = auth()->guard($guard);
2016-01-09 08:39:02 -06:00
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {
2018-02-07 04:13:04 -06:00
$boundObject = $this->performBinding($guard, $key, $value, $request->route());
2016-01-09 08:39:02 -06:00
$request->route()->setParameter($key, $boundObject);
}
}
return $next($request);
}
/**
* @param $key
* @param $value
* @param $route
*
* @return mixed
*/
2018-02-07 04:13:04 -06:00
private function performBinding($guard, string $key, string $value, Route $route)
2016-01-09 08:39:02 -06:00
{
2016-01-09 09:10:12 -06:00
$class = $this->binders[$key];
2018-02-07 04:13:04 -06:00
return $class::routeBinder($guard, $value, $route);
2016-01-09 08:39:02 -06:00
}
}