2016-01-09 15:39:02 +01:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
2018-02-09 15:01:22 +01:00
|
|
|
* Binder.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2016-01-09 15:39:02 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Middleware;
|
|
|
|
|
|
|
|
use FireflyIII\Support\Domain;
|
2018-02-09 14:47:37 +01:00
|
|
|
use Illuminate\Contracts\Auth\Factory as Auth;
|
2020-03-17 15:02:57 +01:00
|
|
|
use Illuminate\Http\Request;
|
2018-02-07 11:13:04 +01:00
|
|
|
use Illuminate\Routing\Route;
|
2016-01-28 21:33:45 +01:00
|
|
|
|
2016-01-15 13:08:25 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Class Binder
|
2016-01-15 13:08:25 +01:00
|
|
|
*/
|
2018-02-09 15:01:22 +01:00
|
|
|
class Binder
|
2016-01-09 15:39:02 +01:00
|
|
|
{
|
2018-02-09 14:47:37 +01:00
|
|
|
/**
|
|
|
|
* The authentication factory instance.
|
|
|
|
*
|
2020-03-17 15:02:57 +01:00
|
|
|
* @var Auth
|
2018-02-09 14:47:37 +01:00
|
|
|
*/
|
|
|
|
protected $auth;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2017-12-17 14:30:53 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* The binders.
|
|
|
|
*
|
2017-12-17 14:30:53 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-01-09 15:39:02 +01:00
|
|
|
protected $binders = [];
|
|
|
|
|
2016-01-09 15:53:11 +01:00
|
|
|
/**
|
|
|
|
* Binder constructor.
|
|
|
|
*/
|
2018-02-09 14:47:37 +01:00
|
|
|
public function __construct(Auth $auth)
|
2016-01-09 15:39:02 +01:00
|
|
|
{
|
|
|
|
$this->binders = Domain::getBindables();
|
2018-02-09 14:47:37 +01:00
|
|
|
$this->auth = $auth;
|
2016-01-09 15:39:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2023-06-21 12:34:58 +02:00
|
|
|
* @param Request $request
|
2016-01-09 15:39:02 +01:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2023-12-20 19:35:52 +01:00
|
|
|
public function handle($request, \Closure $next)
|
2016-01-09 15:39:02 +01:00
|
|
|
{
|
|
|
|
foreach ($request->route()->parameters() as $key => $value) {
|
2021-04-07 07:28:43 +02:00
|
|
|
if (array_key_exists($key, $this->binders)) {
|
2018-02-09 19:23:31 +01:00
|
|
|
$boundObject = $this->performBinding($key, $value, $request->route());
|
2016-01-09 15:39:02 +01:00
|
|
|
$request->route()->setParameter($key, $boundObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Do the binding.
|
|
|
|
*
|
2016-01-09 15:39:02 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-02-09 19:23:31 +01:00
|
|
|
private function performBinding(string $key, string $value, Route $route)
|
2016-01-09 15:39:02 +01:00
|
|
|
{
|
2016-01-09 16:10:12 +01:00
|
|
|
$class = $this->binders[$key];
|
2018-02-09 19:23:31 +01:00
|
|
|
|
|
|
|
return $class::routeBinder($value, $route);
|
2016-01-09 15:39:02 +01:00
|
|
|
}
|
|
|
|
}
|