Skip to content

Commit ac33b07

Browse files
author
Oliver
committed
feat: Add new UnsignedSerializableClosure class
Adds a new class for serializing closures without signing the closure.
1 parent 8a8c701 commit ac33b07

File tree

2 files changed

+85
-14
lines changed

2 files changed

+85
-14
lines changed

src/SerializableClosure.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,19 @@ 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-
2820
/**
2921
* Creates a new serializable closure instance.
3022
*
3123
* @param \Closure $closure
3224
* @return void
3325
*/
34-
public function __construct(Closure $closure, ?bool $shouldSign = null)
26+
public function __construct(Closure $closure)
3527
{
36-
$this->shouldSign = $shouldSign;
37-
3828
if (\PHP_VERSION_ID < 70400) {
3929
throw new PhpVersionNotSupportedException();
4030
}
4131

42-
$this->serializable = Serializers\Signed::$signer && $this->shouldSign !== false
32+
$this->serializable = Serializers\Signed::$signer
4333
? new Serializers\Signed($closure)
4434
: new Serializers\Native($closure);
4535
}
@@ -116,7 +106,6 @@ public function __serialize()
116106
{
117107
return [
118108
'serializable' => $this->serializable,
119-
'shouldSign' => $this->shouldSign,
120109
];
121110
}
122111

@@ -130,7 +119,7 @@ public function __serialize()
130119
*/
131120
public function __unserialize($data)
132121
{
133-
if (Signed::$signer && ($data['shouldSign'] ?? null) !== false && ! $data['serializable'] instanceof Signed) {
122+
if (Signed::$signer && ! $data['serializable'] instanceof Signed) {
134123
throw new InvalidSignatureException();
135124
}
136125

src/UnsignedSerializableClosure.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Laravel\SerializableClosure;
4+
5+
use Closure;
6+
use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException;
7+
8+
class UnsignedSerializableClosure
9+
{
10+
/**
11+
* The closure's serializable.
12+
*
13+
* @var \Laravel\SerializableClosure\Contracts\Serializable
14+
*/
15+
protected $serializable;
16+
17+
/**
18+
* Creates a new serializable closure instance.
19+
*
20+
* @param \Closure $closure
21+
* @return void
22+
*/
23+
public function __construct(Closure $closure)
24+
{
25+
if (\PHP_VERSION_ID < 70400) {
26+
throw new PhpVersionNotSupportedException();
27+
}
28+
29+
$this->serializable = new Serializers\Native($closure);
30+
}
31+
32+
/**
33+
* Resolve the closure with the given arguments.
34+
*
35+
* @return mixed
36+
*/
37+
public function __invoke()
38+
{
39+
if (\PHP_VERSION_ID < 70400) {
40+
throw new PhpVersionNotSupportedException();
41+
}
42+
43+
return call_user_func_array($this->serializable, func_get_args());
44+
}
45+
46+
/**
47+
* Gets the closure.
48+
*
49+
* @return \Closure
50+
*/
51+
public function getClosure()
52+
{
53+
if (\PHP_VERSION_ID < 70400) {
54+
throw new PhpVersionNotSupportedException();
55+
}
56+
57+
return $this->serializable->getClosure();
58+
}
59+
60+
/**
61+
* Get the serializable representation of the closure.
62+
*
63+
* @return array
64+
*/
65+
public function __serialize()
66+
{
67+
return [
68+
'serializable' => $this->serializable,
69+
];
70+
}
71+
72+
/**
73+
* Restore the closure after serialization.
74+
*
75+
* @param array $data
76+
* @return void
77+
*/
78+
public function __unserialize($data)
79+
{
80+
$this->serializable = $data['serializable'];
81+
}
82+
}

0 commit comments

Comments
 (0)