2016-01-09 08:39:02 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06: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-09 08:39:02 -06:00
|
|
|
|
2016-01-15 06:08:25 -06:00
|
|
|
/**
|
|
|
|
* Class Binder
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Middleware
|
|
|
|
*/
|
2016-01-09 08:39:02 -06:00
|
|
|
class Binder
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|