Newer
Older
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
<?php
trait StudipTreeNodeCachableTrait
{
protected static $descendants_cache_array = null;
protected static function getDescendantsCacheArray(): StudipCachedArray
{
if (self::$descendants_cache_array === null) {
self::$descendants_cache_array = new StudipCachedArray(
static::class . '/descendants',
30 * 60
);
}
return self::$descendants_cache_array;
}
protected static function registerCachableCallbacks(array $config): array
{
if (!isset($config['registered_callbacks'])) {
$config['registered_callbacks'] = [];
}
if (!isset($config['registered_callbacks']['before_store'])) {
$config['registered_callbacks']['before_store'] = [];
}
$config['registered_callbacks']['before_store'][] = function ($node): void {
self::getDescendantsCacheArray()->expire();
};
if (!isset($config['registered_callbacks']['after_delete'])) {
$config['registered_callbacks']['after_delete'] = [];
}
$config['registered_callbacks']['after_delete'][] = function ($node): void {
self::getDescendantsCacheArray()->expire();
};
return $config;
}
protected function getDescendantIds(): array
{
$cache = self::getDescendantsCacheArray();
if (isset($cache[$this->id])) {
return $cache[$this->id];
}
$ids = [];
foreach ($this->getChildNodes() as $child) {
$ids = array_merge($ids, [$child->id], $child->getDescendantIds());
}
$cache[$this->id] = $ids;
return $ids;
}
}