Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/3rdparty.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'laravel/serializable-closure/src/SerializableClosure.php')
-rw-r--r--laravel/serializable-closure/src/SerializableClosure.php128
1 files changed, 128 insertions, 0 deletions
diff --git a/laravel/serializable-closure/src/SerializableClosure.php b/laravel/serializable-closure/src/SerializableClosure.php
new file mode 100644
index 00000000..e7f9f0cf
--- /dev/null
+++ b/laravel/serializable-closure/src/SerializableClosure.php
@@ -0,0 +1,128 @@
+<?php
+
+namespace Laravel\SerializableClosure;
+
+use Closure;
+use Laravel\SerializableClosure\Exceptions\InvalidSignatureException;
+use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException;
+use Laravel\SerializableClosure\Serializers\Signed;
+use Laravel\SerializableClosure\Signers\Hmac;
+
+class SerializableClosure
+{
+ /**
+ * The closure's serializable.
+ *
+ * @var \Laravel\SerializableClosure\Contracts\Serializable
+ */
+ protected $serializable;
+
+ /**
+ * Creates a new serializable closure instance.
+ *
+ * @param \Closure $closure
+ * @return void
+ */
+ public function __construct(Closure $closure)
+ {
+ if (\PHP_VERSION_ID < 70400) {
+ throw new PhpVersionNotSupportedException();
+ }
+
+ $this->serializable = Serializers\Signed::$signer
+ ? new Serializers\Signed($closure)
+ : new Serializers\Native($closure);
+ }
+
+ /**
+ * Resolve the closure with the given arguments.
+ *
+ * @return mixed
+ */
+ public function __invoke()
+ {
+ if (\PHP_VERSION_ID < 70400) {
+ throw new PhpVersionNotSupportedException();
+ }
+
+ return call_user_func_array($this->serializable, func_get_args());
+ }
+
+ /**
+ * Gets the closure.
+ *
+ * @return \Closure
+ */
+ public function getClosure()
+ {
+ if (\PHP_VERSION_ID < 70400) {
+ throw new PhpVersionNotSupportedException();
+ }
+
+ return $this->serializable->getClosure();
+ }
+
+ /**
+ * Sets the serializable closure secret key.
+ *
+ * @param string|null $secret
+ * @return void
+ */
+ public static function setSecretKey($secret)
+ {
+ Serializers\Signed::$signer = $secret
+ ? new Hmac($secret)
+ : null;
+ }
+
+ /**
+ * Sets the serializable closure secret key.
+ *
+ * @param \Closure|null $transformer
+ * @return void
+ */
+ public static function transformUseVariablesUsing($transformer)
+ {
+ Serializers\Native::$transformUseVariables = $transformer;
+ }
+
+ /**
+ * Sets the serializable closure secret key.
+ *
+ * @param \Closure|null $resolver
+ * @return void
+ */
+ public static function resolveUseVariablesUsing($resolver)
+ {
+ Serializers\Native::$resolveUseVariables = $resolver;
+ }
+
+ /**
+ * Get the serializable representation of the closure.
+ *
+ * @return array
+ */
+ public function __serialize()
+ {
+ return [
+ 'serializable' => $this->serializable,
+ ];
+ }
+
+ /**
+ * Restore the closure after serialization.
+ *
+ * @param array $data
+ * @return void
+ *
+ * @throws \Laravel\SerializableClosure\Exceptions\InvalidSignatureException
+ */
+ public function __unserialize($data)
+ {
+ if (Signed::$signer && ! $data['serializable'] instanceof Signed) {
+ throw new InvalidSignatureException();
+ }
+
+ $this->serializable = $data['serializable'];
+ }
+}