Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Laravel\SerializableClosure;
4
 
5
use Closure;
6
use Laravel\SerializableClosure\Exceptions\InvalidSignatureException;
7
use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException;
8
use Laravel\SerializableClosure\Serializers\Signed;
9
use Laravel\SerializableClosure\Signers\Hmac;
10
 
11
class SerializableClosure
12
{
13
    /**
14
     * The closure's serializable.
15
     *
16
     * @var \Laravel\SerializableClosure\Contracts\Serializable
17
     */
18
    protected $serializable;
19
 
20
    /**
21
     * Creates a new serializable closure instance.
22
     *
23
     * @param  \Closure  $closure
24
     * @return void
25
     */
26
    public function __construct(Closure $closure)
27
    {
28
        if (\PHP_VERSION_ID < 70400) {
29
            throw new PhpVersionNotSupportedException();
30
        }
31
 
32
        $this->serializable = Serializers\Signed::$signer
33
            ? new Serializers\Signed($closure)
34
            : new Serializers\Native($closure);
35
    }
36
 
37
    /**
38
     * Resolve the closure with the given arguments.
39
     *
40
     * @return mixed
41
     */
42
    public function __invoke()
43
    {
44
        if (\PHP_VERSION_ID < 70400) {
45
            throw new PhpVersionNotSupportedException();
46
        }
47
 
48
        return call_user_func_array($this->serializable, func_get_args());
49
    }
50
 
51
    /**
52
     * Gets the closure.
53
     *
54
     * @return \Closure
55
     */
56
    public function getClosure()
57
    {
58
        if (\PHP_VERSION_ID < 70400) {
59
            throw new PhpVersionNotSupportedException();
60
        }
61
 
62
        return $this->serializable->getClosure();
63
    }
64
 
65
    /**
66
     * Sets the serializable closure secret key.
67
     *
68
     * @param  string|null  $secret
69
     * @return void
70
     */
71
    public static function setSecretKey($secret)
72
    {
73
        Serializers\Signed::$signer = $secret
74
            ? new Hmac($secret)
75
            : null;
76
    }
77
 
78
    /**
79
     * Sets the serializable closure secret key.
80
     *
81
     * @param  \Closure|null  $transformer
82
     * @return void
83
     */
84
    public static function transformUseVariablesUsing($transformer)
85
    {
86
        Serializers\Native::$transformUseVariables = $transformer;
87
    }
88
 
89
    /**
90
     * Sets the serializable closure secret key.
91
     *
92
     * @param  \Closure|null  $resolver
93
     * @return void
94
     */
95
    public static function resolveUseVariablesUsing($resolver)
96
    {
97
        Serializers\Native::$resolveUseVariables = $resolver;
98
    }
99
 
100
    /**
101
     * Get the serializable representation of the closure.
102
     *
103
     * @return array
104
     */
105
    public function __serialize()
106
    {
107
        return [
108
            'serializable' => $this->serializable,
109
        ];
110
    }
111
 
112
    /**
113
     * Restore the closure after serialization.
114
     *
115
     * @param  array  $data
116
     * @return void
117
     *
118
     * @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException
119
     */
120
    public function __unserialize($data)
121
    {
122
        if (Signed::$signer && ! $data['serializable'] instanceof Signed) {
123
            throw new InvalidSignatureException();
124
        }
125
 
126
        $this->serializable = $data['serializable'];
127
    }
128
}