| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorDeps\Laravel\SerializableClosure\Serializers; |
| 4 |
|
| 5 |
use ElementorDeps\Laravel\SerializableClosure\Contracts\Serializable; |
| 6 |
use ElementorDeps\Laravel\SerializableClosure\Exceptions\InvalidSignatureException; |
| 7 |
use ElementorDeps\Laravel\SerializableClosure\Exceptions\MissingSecretKeyException; |
| 8 |
class Signed implements Serializable |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The signer that will sign and verify the closure's signature. |
| 12 |
* |
| 13 |
* @var \Laravel\SerializableClosure\Contracts\Signer|null |
| 14 |
*/ |
| 15 |
public static $signer; |
| 16 |
/** |
| 17 |
* The closure to be serialized/unserialized. |
| 18 |
* |
| 19 |
* @var \Closure |
| 20 |
*/ |
| 21 |
protected $closure; |
| 22 |
/** |
| 23 |
* Creates a new serializable closure instance. |
| 24 |
* |
| 25 |
* @param \Closure $closure |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public function __construct($closure) |
| 29 |
{ |
| 30 |
$this->closure = $closure; |
| 31 |
} |
| 32 |
/** |
| 33 |
* Resolve the closure with the given arguments. |
| 34 |
* |
| 35 |
* @return mixed |
| 36 |
*/ |
| 37 |
public function __invoke() |
| 38 |
{ |
| 39 |
return \call_user_func_array($this->closure, \func_get_args()); |
| 40 |
} |
| 41 |
/** |
| 42 |
* Gets the closure. |
| 43 |
* |
| 44 |
* @return \Closure |
| 45 |
*/ |
| 46 |
public function getClosure() |
| 47 |
{ |
| 48 |
return $this->closure; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Get the serializable representation of the closure. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
public function __serialize() |
| 56 |
{ |
| 57 |
if (!static::$signer) { |
| 58 |
throw new MissingSecretKeyException(); |
| 59 |
} |
| 60 |
return static::$signer->sign(\serialize(new Native($this->closure))); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Restore the closure after serialization. |
| 64 |
* |
| 65 |
* @param array $signature |
| 66 |
* @return void |
| 67 |
* |
| 68 |
* @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException |
| 69 |
*/ |
| 70 |
public function __unserialize($signature) |
| 71 |
{ |
| 72 |
if (static::$signer && !static::$signer->verify($signature)) { |
| 73 |
throw new InvalidSignatureException(); |
| 74 |
} |
| 75 |
/** @var \Laravel\SerializableClosure\Contracts\Serializable $serializable */ |
| 76 |
$serializable = \unserialize($signature['serializable']); |
| 77 |
$this->closure = $serializable->getClosure(); |
| 78 |
} |
| 79 |
} |
| 80 |
|