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

97 lines
2.4 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
* Copyright (c) 2019 thegrumpydictator@gmail.com
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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
*
* 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
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -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/>.
*/
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;
2018-02-07 04:13:04 -06:00
use Illuminate\Routing\Route;
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.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
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
*
* @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
}
2018-07-09 12:24:08 -05:00
/** @noinspection PhpUnusedParameterInspection */
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
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) {
if (isset($this->binders[$key])) {
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.
*
2016-01-09 08:39:02 -06:00
* @param $key
* @param $value
* @param $route
*
* @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
}
}