|
| 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