forked from opis/closure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonSerializableClosure.php
67 lines (53 loc) · 1.85 KB
/
JsonSerializableClosure.php
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
<?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 = \serialize(array(
'use' => $use,
'function' => $code,
'scope' => $scope,
'this' => $object,
'self' => $this->reference,
));
if (static::$securityProvider !== null) {
$data = static::$securityProvider->sign($ret);
$ret = '@' . json_encode($data);
}
if (!--$this->scope->serializations && !--$this->scope->toserialize) {
$this->scope = null;
}
return $ret;
}
}