File: JsonSerializableClosure.php

package info (click to toggle)
php-opis-closure 3.6.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: php: 3,313; xml: 41; makefile: 17; sh: 8
file content (83 lines) | stat: -rw-r--r-- 2,283 bytes parent folder | download
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);
    }
}