PluginProbe
User Access Manager / 2.3.20
User Access Manager v2.3.20
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.20, at src/Object/ObjectMapHandler.php

170 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 private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array
31 {
32 $processMap = ($subMap === null) ? $map : $subMap;
33
34 foreach ($processMap as $id => $subIds) {
35 foreach ($subIds as $subId => $type) {
36 if (isset($map[$subId]) === true && isset($processed[$id][$subId]) === false) {
37 $map[$id] += $this->processTreeMapElements($map, [$subId => $map[$subId]], $processed)[$subId];
38 $processed[$id][$subId] = $subId;
39 }
40 }
41 }
42
43 return $map;
44 }
45
46 private function getTreeMap(string $select, string $generalType): array
47 {
48 $treeMap = [
49 self::TREE_MAP_CHILDREN => [
50 $generalType => []
51 ],
52 self::TREE_MAP_PARENTS => [
53 $generalType => []
54 ]
55 ];
56 $results = (array) $this->database->getResults($select);
57
58 foreach ($results as $result) {
59 $treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type;
60 $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type;
61 $treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type;
62 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type;
63 }
64
65 foreach ($treeMap as $mapType => $mapsByObjectType) {
66 foreach ($mapsByObjectType as $objectType => $map) {
67 $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map);
68 }
69 }
70
71 return $treeMap;
72 }
73
74 private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array
75 {
76 $map = $this->cache->get($cacheKey);
77
78 if ($map === null) {
79 $map = $this->getTreeMap($query, $generalType);
80 $this->cache->add($cacheKey, $map);
81 }
82
83 return $map;
84 }
85
86 public function getPostTreeMap(): array
87 {
88 if ($this->postTreeMap === null) {
89 $query = "SELECT `ID` AS `id`, `post_parent` AS `parentId`, `post_type` AS `type`
90 FROM `{$this->database->getPostsTable()}`
91 WHERE `post_parent` != 0 AND `post_type` != 'revision'";
92
93 $this->postTreeMap = $this->getCachedTreeMap(
94 self::POST_TREE_MAP_CACHE_KEY,
95 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
96 $query
97 );
98 }
99
100 return $this->postTreeMap;
101 }
102
103 public function getTermTreeMap(): array
104 {
105 if ($this->termTreeMap === null) {
106 $query = "SELECT `term_id` AS `id`, `parent` AS `parentId`, `taxonomy` AS `type`
107 FROM `{$this->database->getTermTaxonomyTable()}`
108 WHERE `parent` != 0";
109
110 $this->termTreeMap = $this->getCachedTreeMap(
111 self::TERM_TREE_MAP_CACHE_KEY,
112 ObjectHandler::GENERAL_TERM_OBJECT_TYPE,
113 $query
114 );
115 }
116
117 return $this->termTreeMap;
118 }
119
120 private function getCachedMap(string $cacheKey, string $query): array
121 {
122 $map = $this->cache->get($cacheKey);
123
124 if ($map === null) {
125 $map = [];
126 $results = (array) $this->database->getResults($query);
127
128 foreach ($results as $result) {
129 $map[$result->parentId][$result->objectId] = $result->type;
130 }
131
132 $this->cache->add($cacheKey, $map);
133 }
134
135 return $map;
136 }
137
138 public function getTermPostMap(): array
139 {
140 if ($this->termPostMap === null) {
141 $select = "
142 SELECT `tr`.`object_id` AS `objectId`, `tt`.`term_id` AS `parentId`, `p`.`post_type` AS `type`
143 FROM `{$this->database->getTermRelationshipsTable()}` AS `tr`
144 LEFT JOIN `{$this->database->getPostsTable()}` AS `p`
145 ON (`tr`.`object_id` = `p`.`ID`)
146 LEFT JOIN `{$this->database->getTermTaxonomyTable()}` AS `tt`
147 ON (`tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`)";
148
149 $this->termPostMap = $this->getCachedMap(self::TERM_POST_MAP_CACHE_KEY, $select);
150 }
151
152 return $this->termPostMap;
153 }
154
155 public function getPostTermMap(): array
156 {
157 if ($this->postTermMap === null) {
158 $select = "
159 SELECT `tr`.`object_id` AS `parentId`, `tt`.`term_id` AS `objectId`, `tt`.`taxonomy` AS `type`
160 FROM `{$this->database->getTermRelationshipsTable()}` AS `tr`
161 LEFT JOIN `{$this->database->getTermTaxonomyTable()}` AS `tt`
162 ON (`tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`)";
163
164 $this->postTermMap = $this->getCachedMap(self::POST_TERM_MAP_CACHE_KEY, $select);
165 }
166
167 return $this->postTermMap;
168 }
169 }
170