Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Routing;
4
 
5
use Closure;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Illuminate\Support\Str;
8
 
9
class RouteBinding
10
{
11
    /**
12
     * Create a Route model binding for a given callback.
13
     *
14
     * @param  \Illuminate\Container\Container  $container
15
     * @param  \Closure|string  $binder
16
     * @return \Closure
17
     */
18
    public static function forCallback($container, $binder)
19
    {
20
        if (is_string($binder)) {
21
            return static::createClassBinding($container, $binder);
22
        }
23
 
24
        return $binder;
25
    }
26
 
27
    /**
28
     * Create a class based binding using the IoC container.
29
     *
30
     * @param  \Illuminate\Container\Container  $container
31
     * @param  string  $binding
32
     * @return \Closure
33
     */
34
    protected static function createClassBinding($container, $binding)
35
    {
36
        return function ($value, $route) use ($container, $binding) {
37
            // If the binding has an @ sign, we will assume it's being used to delimit
38
            // the class name from the bind method name. This allows for bindings
39
            // to run multiple bind methods in a single class for convenience.
40
            [$class, $method] = Str::parseCallback($binding, 'bind');
41
 
42
            $callable = [$container->make($class), $method];
43
 
44
            return $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
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
57
     */
58
    public static function forModel($container, $class, $callback = null)
59
    {
60
        return function ($value) use ($container, $class, $callback) {
61
            if (is_null($value)) {
62
                return;
63
            }
64
 
65
            // For model binders, we will attempt to retrieve the models using the first
66
            // method on the model instance. If we cannot retrieve the models we'll
67
            // throw a not found exception otherwise we will return the instance.
68
            $instance = $container->make($class);
69
 
70
            if ($model = $instance->resolveRouteBinding($value)) {
71
                return $model;
72
            }
73
 
74
            // If a callback was supplied to the method we will call that to determine
75
            // what we should do when the model is not found. This just gives these
76
            // developer a little greater flexibility to decide what will happen.
77
            if ($callback instanceof Closure) {
78
                return $callback($value);
79
            }
80
 
81
            throw (new ModelNotFoundException)->setModel($class);
82
        };
83
    }
84
}