2016-01-09 08:39:02 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Binder.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
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/>.
|
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;
|
2016-01-28 14:33:45 -06:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2016-01-15 06:08:25 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class Binder.
|
2016-01-15 06:08:25 -06:00
|
|
|
*/
|
2016-01-09 08:39:02 -06:00
|
|
|
class Binder
|
|
|
|
{
|
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.
|
|
|
|
*/
|
2016-01-09 08:39:02 -06:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->binders = Domain::getBindables();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2017-11-15 05:25:49 -06:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2016-01-09 08:39:02 -06:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-01-28 14:33:45 -06:00
|
|
|
public function handle(Request $request, Closure $next)
|
2016-01-09 08:39:02 -06:00
|
|
|
{
|
|
|
|
foreach ($request->route()->parameters() as $key => $value) {
|
|
|
|
if (isset($this->binders[$key])) {
|
|
|
|
$boundObject = $this->performBinding($key, $value, $request->route());
|
|
|
|
$request->route()->setParameter($key, $boundObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
* @param $route
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function performBinding($key, $value, $route)
|
|
|
|
{
|
2016-01-09 09:10:12 -06:00
|
|
|
$class = $this->binders[$key];
|
2016-01-15 16:12:52 -06:00
|
|
|
|
2016-01-09 09:10:12 -06:00
|
|
|
return $class::routeBinder($value, $route);
|
2016-01-09 08:39:02 -06:00
|
|
|
}
|
|
|
|
}
|