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

API Use new serialize API #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Expand All @@ -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",
Expand All @@ -32,11 +35,6 @@
"Opis\\Closure\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.6.x-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
Expand Down
4 changes: 2 additions & 2 deletions src/ReflectionClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 15 additions & 3 deletions src/SerializableClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Provides a wrapper for serialization of closures
*/
class SerializableClosure implements Serializable
class SerializableClosure
{
/**
* @var Closure Wrapped closure
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down