-
Notifications
You must be signed in to change notification settings - Fork 0
/
Root.php
95 lines (63 loc) · 1.72 KB
/
Root.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Pronode;
class Root extends Node {
/**
* Ghost flag to prevent dumping Ghost Root on __destruct
*/
public bool $isGhost = false;
function __construct($data) {
parent::__construct($data);
// Ghost operations:
$ghost = $this->ghostLoad();
$this->ghostApply($ghost);
}
public function __destruct() {
if ($this->isRoot() && !$this->isGhost) {
$this->ghostDump();
}
}
/**
* Applies Ghost to the Root by creating a App Tree Skeleton
* where every Node has ->ghost scalar property set to the value
* generated with previous Request.
*
* Ghost should be loaded with ->ghostLoad() method first.
*
* @param Root|null $ghost
* @return bool
*/
protected function ghostApply(?Root $ghost) : bool {
if ($ghost === null) return false;
$this->children = $ghost->children;
foreach ($this->children as &$child) {
$child->parent = $this;
}
return true;
}
/**
* Dumps serialized Ghost Root to the current Session Ghost Cache file.
*
* Returns the file_put_contents() result.
*
* @return int
*/
protected function ghostDump() : int {
$dir = $this->getClassDir($this->data);
$file = $dir.DIRECTORY_SEPARATOR.'ghost.cache';
return file_put_contents($file, serialize($this));
}
/**
* Loads unserialized Ghost Root from the current Session Ghost Cache file.
*
* @return Root|NULL
*/
protected function ghostLoad() : ?Root {
$dir = $this->getClassDir($this->root()->data);
$file = $dir.DIRECTORY_SEPARATOR.'ghost.cache';
if (!file_exists($file)) return null;
$serialized = file_get_contents($file);
$ghost = unserialize($serialized);
$ghost->isGhost = true;
return $ghost;
}
}