1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
<?php
/* ===========================================================================
* Copyright (c) 2019-2021 Zindex Software
*
* Licensed under the MIT License
* =========================================================================== */
namespace Opis\Closure\Test;
use Opis\Closure\ClosureScope;
use Opis\Closure\SerializableClosure;
class JsonSerializableClosure extends SerializableClosure
{
public function __serialize()
{
if ($this->scope === null) {
$this->scope = new ClosureScope();
$this->scope->toserialize++;
}
$this->scope->serializations++;
$scope = $object = null;
$reflector = $this->getReflector();
if($reflector->isBindingRequired()){
$object = $reflector->getClosureThis();
static::wrapClosures($object, $this->scope);
if($scope = $reflector->getClosureScopeClass()){
$scope = $scope->name;
}
} else {
if($scope = $reflector->getClosureScopeClass()){
$scope = $scope->name;
}
}
$this->reference = spl_object_hash($this->closure);
$this->scope[$this->closure] = $this;
$use = $this->transformUseVariables($reflector->getUseVariables());
$code = $reflector->getCode();
$this->mapByReference($use);
$ret = array(
'use' => $use,
'function' => $code,
'scope' => $scope,
'this' => $object,
'self' => $this->reference,
);
if (static::$securityProvider !== null && $this->scope->serializations === 1) {
$ser = \serialize($ret);
$ret = static::$securityProvider->sign($ser);
}
if (!--$this->scope->serializations && !--$this->scope->toserialize) {
$this->scope = null;
}
return [json_encode($ret)];
}
public function __unserialize($data)
{
if (is_array($data) && count($data) === 1 && isset($data[0])) {
$data = $data[0];
}
parent::__unserialize(json_decode($data, true));
}
public function unserialize($data)
{
$json = \unserialize($data);
$this->__unserialize($json);
}
}
|