Skip to content

Commit 8a8c701

Browse files
author
Oliver
committed
feat: Add support for specifying if it should sign
This adds a new parameter to specify if a closure should be signed or not.
1 parent 975c823 commit 8a8c701

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/SerializableClosure.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,29 @@ class SerializableClosure
1717
*/
1818
protected $serializable;
1919

20+
/**
21+
* Checks if the Closure should be signed.
22+
* If set to `null` it will be based on if a signer exists.
23+
*
24+
* @var bool|null
25+
*/
26+
protected $shouldSign;
27+
2028
/**
2129
* Creates a new serializable closure instance.
2230
*
2331
* @param \Closure $closure
2432
* @return void
2533
*/
26-
public function __construct(Closure $closure)
34+
public function __construct(Closure $closure, ?bool $shouldSign = null)
2735
{
36+
$this->shouldSign = $shouldSign;
37+
2838
if (\PHP_VERSION_ID < 70400) {
2939
throw new PhpVersionNotSupportedException();
3040
}
3141

32-
$this->serializable = Serializers\Signed::$signer
42+
$this->serializable = Serializers\Signed::$signer && $this->shouldSign !== false
3343
? new Serializers\Signed($closure)
3444
: new Serializers\Native($closure);
3545
}
@@ -106,6 +116,7 @@ public function __serialize()
106116
{
107117
return [
108118
'serializable' => $this->serializable,
119+
'shouldSign' => $this->shouldSign,
109120
];
110121
}
111122

@@ -119,7 +130,7 @@ public function __serialize()
119130
*/
120131
public function __unserialize($data)
121132
{
122-
if (Signed::$signer && ! $data['serializable'] instanceof Signed) {
133+
if (Signed::$signer && ($data['shouldSign'] ?? null) !== false && ! $data['serializable'] instanceof Signed) {
123134
throw new InvalidSignatureException();
124135
}
125136

0 commit comments

Comments
 (0)