2016-01-09 08:39:02 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
2018-02-09 08:01:22 -06:00
|
|
|
* Binder.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
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;
|
2020-03-17 09:02:57 -05:00
|
|
|
use Illuminate\Http\Request;
|
2018-02-07 04:13:04 -06:00
|
|
|
use Illuminate\Routing\Route;
|
2016-01-28 14:33:45 -06:00
|
|
|
|
2016-01-15 06:08:25 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Class Binder
|
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.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @var Auth
|
2018-02-09 07:47:37 -06:00
|
|
|
*/
|
|
|
|
protected $auth;
|
2017-12-17 07:30:53 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* The binders.
|
|
|
|
*
|
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
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param Auth $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.
|
|
|
|
*
|
2020-03-17 09:02:57 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2016-01-09 08:39:02 -06:00
|
|
|
*
|
|
|
|
* @return mixed
|
2018-02-09 07:47:37 -06:00
|
|
|
*
|
2016-01-09 08:39:02 -06:00
|
|
|
*/
|
2018-07-17 15:21:03 -05:00
|
|
|
public function handle($request, Closure $next)
|
2016-01-09 08:39:02 -06:00
|
|
|
{
|
|
|
|
foreach ($request->route()->parameters() as $key => $value) {
|
2021-04-07 00:28:43 -05:00
|
|
|
if (array_key_exists($key, $this->binders)) {
|
2018-02-09 12:23:31 -06:00
|
|
|
$boundObject = $this->performBinding($key, $value, $request->route());
|
2016-01-09 08:39:02 -06:00
|
|
|
$request->route()->setParameter($key, $boundObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Do the binding.
|
|
|
|
*
|
2021-04-07 00:53:05 -05:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
2021-09-18 03:26:12 -05:00
|
|
|
* @param Route $route
|
2016-01-09 08:39:02 -06:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-02-09 12:23:31 -06:00
|
|
|
private function performBinding(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-09 12:23:31 -06:00
|
|
|
|
|
|
|
return $class::routeBinder($value, $route);
|
2016-01-09 08:39:02 -06:00
|
|
|
}
|
|
|
|
}
|