PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Object / ObjectMapHandler.php

ObjectMapHandler.php in User Access Manager 2.3.13, at src/Object/ObjectMapHandler.php

179 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Object;
6
7 use UserAccessManager\Cache\Cache;
8 use UserAccessManager\Database\Database;
9
10 class ObjectMapHandler
11 {
12 public const TREE_MAP_PARENTS = 'PARENT';
13 public const TREE_MAP_CHILDREN = 'CHILDREN';
14 public const POST_TREE_MAP_CACHE_KEY = 'uamPostTreeMap';
15 public const TERM_TREE_MAP_CACHE_KEY = 'uamTermTreeMap';
16 public const TERM_POST_MAP_CACHE_KEY = 'uamTermPostMap';
17 public const POST_TERM_MAP_CACHE_KEY = 'uamPostTermMap';
18
19 private ?array $termPostMap = null;
20 private ?array $postTermMap = null;
21 private ?array $termTreeMap = null;
22 private ?array $postTreeMap = null;
23
24 public function __construct(
25 private Database $database,
26 private Cache $cache
27 ) {
28 }
29
30 /**
31 * Resolves all tree map elements
32 * @param array $map
33 * @param array|null $subMap
34 * @param array $processed
35 * @return array
36 */
37 private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array
38 {
39 $processMap = ($subMap === null) ? $map : $subMap;
40
41 foreach ($processMap as $id => $subIds) {
42 foreach ($subIds as $subId => $type) {
43 if (isset($map[$subId]) === true && isset($processed[$id][$subId]) === false) {
44 $map[$id] += $this->processTreeMapElements($map, [$subId => $map[$subId]], $processed)[$subId];
45 $processed[$id][$subId] = $subId;
46 }
47 }
48 }
49
50 return $map;
51 }
52
53 private function getTreeMap(string $select, string $generalType): array
54 {
55 $treeMap = [
56 self::TREE_MAP_CHILDREN => [
57 $generalType => []
58 ],
59 self::TREE_MAP_PARENTS => [
60 $generalType => []
61 ]
62 ];
63 $results = (array) $this->database->getResults($select);
64
65 foreach ($results as $result) {
66 $treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type;
67 $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type;
68 $treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type;
69 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type;
70 }
71
72 //Process elements
73 foreach ($treeMap as $mapType => $mayTypeMap) {
74 foreach ($mayTypeMap as $objectType => $map) {
75 $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map);
76 }
77 }
78
79 return $treeMap;
80 }
81
82 private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array
83 {
84 $map = $this->cache->get($cacheKey);
85
86 if ($map === null) {
87 $map = $this->getTreeMap($query, $generalType);
88 $this->cache->add($cacheKey, $map);
89 }
90
91
92 return $map;
93 }
94
95 public function getPostTreeMap(): ?array
96 {
97 if ($this->postTreeMap === null) {
98 $query = "SELECT ID AS id, post_parent AS parentId, post_type AS type
99 FROM {$this->database->getPostsTable()}
100 WHERE post_parent != 0 AND post_type != 'revision'";
101
102 $this->postTreeMap = $this->getCachedTreeMap(
103 self::POST_TREE_MAP_CACHE_KEY,
104 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
105 $query
106 );
107 }
108
109 return $this->postTreeMap;
110 }
111
112 public function getTermTreeMap(): ?array
113 {
114 if ($this->termTreeMap === null) {
115 $query = "SELECT term_id AS id, parent AS parentId, taxonomy AS type
116 FROM {$this->database->getTermTaxonomyTable()}
117 WHERE parent != 0";
118
119 $this->termTreeMap = $this->getCachedTreeMap(
120 self::TERM_TREE_MAP_CACHE_KEY,
121 ObjectHandler::GENERAL_TERM_OBJECT_TYPE,
122 $query
123 );
124 }
125
126 return $this->termTreeMap;
127 }
128
129 private function getCachedMap(string $cacheKey, string $query): array
130 {
131 $map = $this->cache->get($cacheKey);
132
133 if ($map === null) {
134 $map = [];
135 $results = (array) $this->database->getResults($query);
136
137 foreach ($results as $result) {
138 $map[$result->parentId][$result->objectId] = $result->type;
139 }
140
141 $this->cache->add($cacheKey, $map);
142 }
143
144 return $map;
145 }
146
147 public function getTermPostMap(): ?array
148 {
149 if ($this->termPostMap === null) {
150 $select = "
151 SELECT tr.object_id AS objectId, tt.term_id AS parentId, p.post_type AS type
152 FROM {$this->database->getTermRelationshipsTable()} AS tr
153 LEFT JOIN {$this->database->getPostsTable()} AS p
154 ON (tr.object_id = p.ID)
155 LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
156 ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
157
158 $this->termPostMap = $this->getCachedMap(self::TERM_POST_MAP_CACHE_KEY, $select);
159 }
160
161 return $this->termPostMap;
162 }
163
164 public function getPostTermMap(): ?array
165 {
166 if ($this->postTermMap === null) {
167 $select = "
168 SELECT tr.object_id AS parentId, tt.term_id AS objectId, tt.taxonomy AS type
169 FROM {$this->database->getTermRelationshipsTable()} AS tr
170 LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
171 ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
172
173 $this->postTermMap = $this->getCachedMap(self::POST_TERM_MAP_CACHE_KEY, $select);
174 }
175
176 return $this->postTermMap;
177 }
178 }
179