Skip to content

Commit

Permalink
extract implicit binding
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 21, 2016
1 parent b208a4f commit 21de409
Showing 1 changed file with 13 additions and 76 deletions.
89 changes: 13 additions & 76 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,24 +633,7 @@ public function substituteBindings($route)
*/
public function substituteImplicitBindings($route)
{
$parameters = $route->parameters();

foreach ($route->signatureParameters(Model::class) as $parameter) {
$class = $parameter->getClass();

if (array_key_exists($parameter->name, $parameters) &&
! $route->parameter($parameter->name) instanceof Model) {
$method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail';

$model = $this->container->make($class->name);

$route->setParameter(
$parameter->name, $model->where(
$model->getRouteKeyName(), $parameters[$parameter->name]
)->{$method}()
);
}
}
ImplicitRouteBinding::resolveForRoute($this->container, $route);
}

/**
Expand Down Expand Up @@ -751,43 +734,6 @@ public function pushMiddlewareToGroup($group, $middleware)
return $this;
}

/**
* Register a model binder for a wildcard.
*
* @param string $key
* @param string $class
* @param \Closure|null $callback
* @return void
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function model($key, $class, Closure $callback = null)
{
$this->bind($key, function ($value) use ($class, $callback) {
if (is_null($value)) {
return;
}

// For model binders, we will attempt to retrieve the models using the first
// method on the model instance. If we cannot retrieve the models we'll
// throw a not found exception otherwise we will return the instance.
$instance = $this->container->make($class);

if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
return $model;
}

// If a callback was supplied to the method we will call that to determine
// what we should do when the model is not found. This just gives these
// developer a little greater flexibility to decide what will happen.
if ($callback instanceof Closure) {
return call_user_func($callback, $value);
}

throw (new ModelNotFoundException)->setModel($class);
});
}

/**
* Add a new route parameter binder.
*
Expand All @@ -797,33 +743,24 @@ public function model($key, $class, Closure $callback = null)
*/
public function bind($key, $binder)
{
if (is_string($binder)) {
$binder = $this->createClassBinding($binder);
}

$this->binders[str_replace('-', '_', $key)] = $binder;
$this->binders[str_replace('-', '_', $key)] = RouteBinding::forCallback(
$this->container, $binder
);
}

/**
* Create a class based binding using the IoC container.
* Register a model binder for a wildcard.
*
* @param string $binding
* @return \Closure
* @param string $key
* @param string $class
* @param \Closure|null $callback
* @return void
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function createClassBinding($binding)
public function model($key, $class, Closure $callback = null)
{
return function ($value, $route) use ($binding) {
// If the binding has an @ sign, we will assume it's being used to delimit
// the class name from the bind method name. This allows for bindings
// to run multiple bind methods in a single class for convenience.
$segments = explode('@', $binding);

$method = count($segments) == 2 ? $segments[1] : 'bind';

$callable = [$this->container->make($segments[0]), $method];

return call_user_func($callable, $value, $route);
};
$this->bind($key, RouteBinding::forModel($this->container, $class, $callback));
}

/**
Expand Down

0 comments on commit 21de409

Please sign in to comment.