Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
     * Create a new unsigned serializable closure instance.
67
     *
68
     * @param  Closure  $closure
69
     * @return \Laravel\SerializableClosure\UnsignedSerializableClosure
70
     */
71
    public static function unsigned(Closure $closure)
72
    {
73
        return new UnsignedSerializableClosure($closure);
74
    }
75
 
76
    /**
77
     * Sets the serializable closure secret key.
78
     *
79
     * @param  string|null  $secret
80
     * @return void
81
     */
82
    public static function setSecretKey($secret)
83
    {
84
        Serializers\Signed::$signer = $secret
85
            ? new Hmac($secret)
86
            : null;
87
    }
88
 
89
    /**
90
     * Sets the serializable closure secret key.
91
     *
92
     * @param  \Closure|null  $transformer
93
     * @return void
94
     */
95
    public static function transformUseVariablesUsing($transformer)
96
    {
97
        Serializers\Native::$transformUseVariables = $transformer;
98
    }
99
 
100
    /**
101
     * Sets the serializable closure secret key.
102
     *
103
     * @param  \Closure|null  $resolver
104
     * @return void
105
     */
106
    public static function resolveUseVariablesUsing($resolver)
107
    {
108
        Serializers\Native::$resolveUseVariables = $resolver;
109
    }
110
 
111
    /**
112
     * Get the serializable representation of the closure.
113
     *
114
     * @return array
115
     */
116
    public function __serialize()
117
    {
118
        return [
119
            'serializable' => $this->serializable,
120
        ];
121
    }
122
 
123
    /**
124
     * Restore the closure after serialization.
125
     *
126
     * @param  array  $data
127
     * @return void
128
     *
129
     * @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException
130
     */
131
    public function __unserialize($data)
132
    {
133
        if (Signed::$signer && ! $data['serializable'] instanceof Signed) {
134
            throw new InvalidSignatureException();
135
        }
136
 
137
        $this->serializable = $data['serializable'];
138
    }
139
}