Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from creative-commoners/pulls/master/depr
Browse files Browse the repository at this point in the history
API Use new serialize API
GuySartorelli authored Mar 28, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 3d81e43 + 7b5ac5c commit b898a8d
Showing 3 changed files with 22 additions and 12 deletions.
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "opis/closure",
"name": "silverstripe/closure",
"description": "A library that can be used to serialize closures (anonymous functions) and arbitrary objects.",
"keywords": ["closure", "serialization", "function", "serializable", "serialize", "anonymous functions"],
"homepage": "https://opis.io/closure",
@@ -15,7 +15,10 @@
}
],
"require": {
"php": "^5.4 || ^7.0 || ^8.0"
"php": "^8.1"
},
"replace": {
"opis/closure":"3.*"
},
"require-dev": {
"jeremeamia/superclosure": "^2.0",
@@ -32,11 +35,6 @@
"Opis\\Closure\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.6.x-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
4 changes: 2 additions & 2 deletions src/ReflectionClosure.php
Original file line number Diff line number Diff line change
@@ -46,13 +46,13 @@ public function __construct(Closure $closure, $code = null)
/**
* @return bool
*/
public function isStatic()
public function isStatic(): bool
{
if ($this->isStaticClosure === null) {
$this->isStaticClosure = strtolower(substr($this->getCode(), 0, 6)) === 'static';
}

return $this->isStaticClosure;
return (bool) $this->isStaticClosure;
}

public function isShortClosure()
18 changes: 15 additions & 3 deletions src/SerializableClosure.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
/**
* Provides a wrapper for serialization of closures
*/
class SerializableClosure implements Serializable
class SerializableClosure
{
/**
* @var Closure Wrapped closure
@@ -109,12 +109,19 @@ public function __invoke()
return call_user_func_array($this->closure, func_get_args());
}

public function __serialize(): array
{
return [
'serialized' => $this->serialize()
];
}

/**
* Implementation of Serializable::serialize()
*
* @return string The serialized closure
*/
public function serialize()
private function serialize()
{
if ($this->scope === null) {
$this->scope = new ClosureScope();
@@ -178,13 +185,18 @@ protected function transformUseVariables($data)
return $data;
}

public function __unserialize(array $data): void
{
$this->unserialize($data['serialized']);
}

/**
* Implementation of Serializable::unserialize()
*
* @param string $data Serialized data
* @throws SecurityException
*/
public function unserialize($data)
private function unserialize($data)
{
ClosureStream::register();

0 comments on commit b898a8d

Please sign in to comment.