Skip to content

Commit e75730e

Browse files
committed
add files
1 parent 21de409 commit e75730e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Illuminate\Routing;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ImplicitRouteBinding
8+
{
9+
/**
10+
* Resolve the implicit route bindings for the given route.
11+
*
12+
* @param \Illuminate\Container\Container $container
13+
* @param \Illuminate\Routing\Route $route
14+
* @return void
15+
*/
16+
public static function resolveForRoute($container, $route)
17+
{
18+
$parameters = $route->parameters();
19+
20+
foreach ($route->signatureParameters(Model::class) as $parameter) {
21+
$class = $parameter->getClass();
22+
23+
if (array_key_exists($parameter->name, $parameters) &&
24+
! $route->parameter($parameter->name) instanceof Model) {
25+
$method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail';
26+
27+
$model = $container->make($class->name);
28+
29+
$route->setParameter(
30+
$parameter->name, $model->where(
31+
$model->getRouteKeyName(), $parameters[$parameter->name]
32+
)->{$method}()
33+
);
34+
}
35+
}
36+
}
37+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Illuminate\Routing;
4+
5+
use Closure;
6+
use Illuminate\Database\Eloquent\ModelNotFoundException;
7+
8+
class RouteBinding
9+
{
10+
/**
11+
* Create a Route model binding for a given callback.
12+
*
13+
* @param \Illuminate\Container\Container $container
14+
* @param \Closure|string $binder
15+
* @return \Closure
16+
*/
17+
public static function forCallback($container, $binder)
18+
{
19+
if (is_string($binder)) {
20+
return static::createClassBinding($container, $binder);
21+
}
22+
23+
return $binder;
24+
}
25+
26+
/**
27+
* Create a class based binding using the IoC container.
28+
*
29+
* @param string $binding
30+
* @return \Closure
31+
*/
32+
protected static function createClassBinding($container, $binding)
33+
{
34+
return function ($value, $route) use ($container, $binding) {
35+
// If the binding has an @ sign, we will assume it's being used to delimit
36+
// the class name from the bind method name. This allows for bindings
37+
// to run multiple bind methods in a single class for convenience.
38+
$segments = explode('@', $binding);
39+
40+
$method = count($segments) == 2 ? $segments[1] : 'bind';
41+
42+
$callable = [$container->make($segments[0]), $method];
43+
44+
return call_user_func($callable, $value, $route);
45+
};
46+
}
47+
48+
/**
49+
* Create a Route model binding for a model.
50+
*
51+
* @param \Illuminate\Container\Container $container
52+
* @param string $class
53+
* @param \Closure|null $callback
54+
* @return \Closure
55+
*/
56+
public static function forModel($container, $class, $callback = null)
57+
{
58+
return function ($value) use ($container, $class, $callback) {
59+
if (is_null($value)) {
60+
return;
61+
}
62+
63+
// For model binders, we will attempt to retrieve the models using the first
64+
// method on the model instance. If we cannot retrieve the models we'll
65+
// throw a not found exception otherwise we will return the instance.
66+
$instance = $container->make($class);
67+
68+
if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) {
69+
return $model;
70+
}
71+
72+
// If a callback was supplied to the method we will call that to determine
73+
// what we should do when the model is not found. This just gives these
74+
// developer a little greater flexibility to decide what will happen.
75+
if ($callback instanceof Closure) {
76+
return call_user_func($callback, $value);
77+
}
78+
79+
throw (new ModelNotFoundException)->setModel($class);
80+
};
81+
}
82+
}

0 commit comments

Comments
 (0)