|
1
|
<?php |
|
2
|
|
|
3
|
declare(strict_types=1); |
|
4
|
|
|
5
|
namespace Serializor; |
|
6
|
|
|
7
|
use Closure; |
|
8
|
|
|
9
|
/** |
|
10
|
* Implementation of SerializableClosure to be compatible with |
|
11
|
* Opis/Closure and Laravel/SerializableClosure. |
|
12
|
* |
|
13
|
* @package Serializor |
|
14
|
*/ |
|
15
|
class SerializableClosure |
|
16
|
{ |
|
17
|
|
|
18
|
private Closure $closure; |
|
19
|
|
|
20
|
public function __construct(Closure $closure) |
|
21
|
{ |
|
22
|
$this->closure = $closure; |
|
23
|
} |
|
24
|
|
|
25
|
/** |
|
26
|
* Resolve the closure with the given arguments. |
|
27
|
* |
|
28
|
* @return mixed |
|
29
|
*/ |
|
30
|
public function __invoke() |
|
31
|
{ |
|
32
|
return call_user_func_array($this->closure, func_get_args()); |
|
33
|
} |
|
34
|
|
|
35
|
/** |
|
36
|
* Get the Closure object. |
|
37
|
* |
|
38
|
* @return Closure |
|
39
|
*/ |
|
40
|
public function getClosure(): Closure |
|
41
|
{ |
|
42
|
return $this->closure; |
|
43
|
} |
|
44
|
|
|
45
|
/** |
|
46
|
* Create a new unsigned serializable closure instance. |
|
47
|
* |
|
48
|
* @param Closure $closure |
|
49
|
* @return UnsignedSerializableClosure |
|
50
|
*/ |
|
51
|
public static function unsigned(Closure $closure) |
|
52
|
{ |
|
53
|
return new UnsignedSerializableClosure($closure); |
|
54
|
} |
|
55
|
|
|
56
|
/** |
|
57
|
* Hook into PHP serialization |
|
58
|
* |
|
59
|
* @return array |
|
60
|
*/ |
|
61
|
public function __serialize() |
|
62
|
{ |
|
63
|
return ['_' => Codec::getInstance()->_serialize($this->closure)]; |
|
64
|
} |
|
65
|
|
|
66
|
/** |
|
67
|
* Hook into PHP unserialization |
|
68
|
* |
|
69
|
* @param array $data |
|
70
|
* @return void |
|
71
|
*/ |
|
72
|
public function __unserialize(array $data): void |
|
73
|
{ |
|
74
|
$this->closure = Codec::getInstance()->_unserialize($data['_']); |
|
75
|
} |
|
76
|
|
|
77
|
public static function resolveUseVariablesUsing(callable $resolver): void |
|
78
|
{ |
|
79
|
Codec::setResolveUseVarsFunc($resolver instanceof Closure ? $resolver : Closure::fromCallable($resolver)); |
|
80
|
} |
|
81
|
|
|
82
|
public static function transformUseVariablesUsing(callable $transformer): void |
|
83
|
{ |
|
84
|
Codec::setTransformUseVarsFunc($transformer instanceof Closure ? $transformer : Closure::fromCallable($transformer)); |
|
85
|
} |
|
86
|
|
|
87
|
/** |
|
88
|
* Sets the serializable closure secret key. |
|
89
|
* |
|
90
|
* @param string|null $secret |
|
91
|
* @return void |
|
92
|
*/ |
|
93
|
public static function setSecretKey($secret): void |
|
94
|
{ |
|
95
|
self::$secret = $secret; |
|
96
|
|
|
97
|
Codec::setDefaultSecret($secret); |
|
98
|
} |
|
99
|
|
|
100
|
private static ?string $secret = null; |
|
101
|
} |
|
102
|
|