Skip to content

Commit

Permalink
Add support for Laravel\SerializableClosure
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Feb 11, 2022
1 parent df24555 commit 77fd46e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .idea/gretel.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require": {
"illuminate/support": "^8.0|^9.0|dev-master",
"illuminate/routing": "^8.0|^9.0|dev-master",
"illuminate/view": "^8.0|^9.0|dev-master"
"illuminate/view": "^8.0|^9.0|dev-master",
"laravel/serializable-closure": "^1.0"
},
"require-dev": {
"orchestra/testbench": "^6.21|^7.0|dev-master",
Expand Down
25 changes: 21 additions & 4 deletions src/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Glhd\Gretel\Support\BindsClosures;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Opis\Closure\SerializableClosure;
use Laravel\SerializableClosure\SerializableClosure;
use Opis\Closure\SerializableClosure as OpisSerializableClosure;

class Resolver
{
Expand Down Expand Up @@ -70,7 +71,7 @@ public function getClosure(): Closure
{
if (null !== $this->serialized) {
$callback = unserialize($this->serialized, ['allow_classes' => true]);
if ($callback instanceof SerializableClosure) {
if ($this->isSerializableClosure($callback)) {
$this->callback = $callback->getClosure();
$this->serialized = null;
}
Expand All @@ -91,10 +92,26 @@ public function getSerializedClosure(): string
return $this->serialized;
}

protected function isSerializableClosure($callback): bool
{
if ($callback instanceof SerializableClosure) {
return true;
}

if (class_exists(OpisSerializableClosure::class) && $callback instanceof OpisSerializableClosure) {
return true;
}

return false;
}

protected function isSerializedClosure($value): bool
{
$fragment = 'C:'.strlen(SerializableClosure::class).':"'.SerializableClosure::class;
$needles = [
'O:'.strlen(SerializableClosure::class).':"'.SerializableClosure::class,
'C:'.strlen(OpisSerializableClosure::class).':"'.OpisSerializableClosure::class,
];

return is_string($value) && Str::startsWith($value, $fragment);
return is_string($value) && Str::startsWith($value, $needles);
}
}

0 comments on commit 77fd46e

Please sign in to comment.