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

97 lines
2.4 KiB
PHP
Raw Normal View History

2016-01-09 15:39:02 +01:00
<?php
/**
2018-02-09 15:01:22 +01:00
* Binder.php
2018-02-07 10:49:06 +01:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-01-09 15:39:02 +01:00
namespace FireflyIII\Http\Middleware;
use Closure;
use FireflyIII\Support\Domain;
2018-02-09 14:47:37 +01:00
use Illuminate\Contracts\Auth\Factory as Auth;
2018-02-07 11:13:04 +01:00
use Illuminate\Routing\Route;
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.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
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
*
* @param \Illuminate\Contracts\Auth\Factory $auth
2016-01-09 15:53:11 +01:00
*/
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
}
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUnusedParameterInspection */
2016-01-09 15:39:02 +01:00
/**
* Handle an incoming request.
*
2018-02-09 14:47:37 +01:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
2016-01-09 15:39:02 +01:00
*
* @return mixed
2018-02-09 14:47:37 +01:00
*
2016-01-09 15:39:02 +01:00
*/
2018-07-17 22:21:03 +02:00
public function handle($request, Closure $next)
2016-01-09 15:39:02 +01:00
{
foreach ($request->route()->parameters() as $key => $value) {
if (isset($this->binders[$key])) {
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
* @param $key
* @param $value
* @param $route
*
* @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
}
}