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\Support\Traits;
4
 
5
use Closure;
6
use Illuminate\Support\Reflector;
7
use ReflectionFunction;
8
use RuntimeException;
9
 
10
trait ReflectsClosures
11
{
12
    /**
13
     * Get the class name of the first parameter of the given Closure.
14
     *
15
     * @param  \Closure  $closure
16
     * @return string
17
     *
18
     * @throws \ReflectionException
19
     * @throws \RuntimeException
20
     */
21
    protected function firstClosureParameterType(Closure $closure)
22
    {
23
        $types = array_values($this->closureParameterTypes($closure));
24
 
25
        if (! $types) {
26
            throw new RuntimeException('The given Closure has no parameters.');
27
        }
28
 
29
        if ($types[0] === null) {
30
            throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
31
        }
32
 
33
        return $types[0];
34
    }
35
 
36
    /**
37
     * Get the class names of the first parameter of the given Closure, including union types.
38
     *
39
     * @param  \Closure  $closure
40
     * @return array
41
     *
42
     * @throws \ReflectionException
43
     * @throws \RuntimeException
44
     */
45
    protected function firstClosureParameterTypes(Closure $closure)
46
    {
47
        $reflection = new ReflectionFunction($closure);
48
 
49
        $types = collect($reflection->getParameters())->mapWithKeys(function ($parameter) {
50
            if ($parameter->isVariadic()) {
51
                return [$parameter->getName() => null];
52
            }
53
 
54
            return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
55
        })->filter()->values()->all();
56
 
57
        if (empty($types)) {
58
            throw new RuntimeException('The given Closure has no parameters.');
59
        }
60
 
61
        if (isset($types[0]) && empty($types[0])) {
62
            throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
63
        }
64
 
65
        return $types[0];
66
    }
67
 
68
    /**
69
     * Get the class names / types of the parameters of the given Closure.
70
     *
71
     * @param  \Closure  $closure
72
     * @return array
73
     *
74
     * @throws \ReflectionException
75
     */
76
    protected function closureParameterTypes(Closure $closure)
77
    {
78
        $reflection = new ReflectionFunction($closure);
79
 
80
        return collect($reflection->getParameters())->mapWithKeys(function ($parameter) {
81
            if ($parameter->isVariadic()) {
82
                return [$parameter->getName() => null];
83
            }
84
 
85
            return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
86
        })->all();
87
    }
88
}