|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | + |
| 7 | +class SpotlightService |
| 8 | +{ |
| 9 | + protected array $registeredModels = []; |
| 10 | + protected array $manualItems = []; |
| 11 | + protected array $staticItems = []; |
| 12 | + |
| 13 | + public function registerModel(string $modelClass): void |
| 14 | + { |
| 15 | + if (!in_array($modelClass, $this->registeredModels)) { |
| 16 | + $this->registeredModels[] = $modelClass; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public function addItem(array $item): void |
| 21 | + { |
| 22 | + if (!isset($item['title']) || !isset($item['url'])) { |
| 23 | + throw new InvalidArgumentException('Spotlight items must have at least a title and URL'); |
| 24 | + } |
| 25 | + |
| 26 | + $item['description'] = $item['description'] ?? ''; |
| 27 | + $item['icon'] = $item['icon'] ?? 'link'; |
| 28 | + $item['id'] = $item['id'] ?? 'manual-' . count($this->manualItems); |
| 29 | + $item['model_type'] = $item['model_type'] ?? 'manual'; |
| 30 | + $item['module'] = $item['module'] ?? null; |
| 31 | + $item['permissions'] = $item['permissions'] ?? null; |
| 32 | + |
| 33 | + if (isset($item['module']) && is_string($item['module']) && $this->isTranslationKey($item['module'])) { |
| 34 | + $item['raw_module'] = $item['module']; |
| 35 | + $item['module'] = __($item['module']); |
| 36 | + } |
| 37 | + |
| 38 | + $this->manualItems[] = $item; |
| 39 | + } |
| 40 | + |
| 41 | + public function addStaticItem(array $item): void |
| 42 | + { |
| 43 | + if (!isset($item['title']) || !isset($item['url'])) { |
| 44 | + throw new InvalidArgumentException('Spotlight items must have at least a title and URL'); |
| 45 | + } |
| 46 | + |
| 47 | + $item['icon'] = $item['icon'] ?? 'link'; |
| 48 | + $item['id'] = $item['id'] ?? 'static-' . count($this->staticItems); |
| 49 | + $item['permissions'] = $item['permissions'] ?? null; |
| 50 | + $item['module'] = $item['module'] ?? null; |
| 51 | + |
| 52 | + if (isset($item['module']) && is_string($item['module']) && $this->isTranslationKey($item['module'])) { |
| 53 | + $item['raw_module'] = $item['module']; |
| 54 | + $item['module'] = __($item['module']); |
| 55 | + } |
| 56 | + |
| 57 | + if (is_string($item['title']) && $this->isTranslationKey($item['title'])) { |
| 58 | + $item['raw_title'] = $item['title']; |
| 59 | + $item['title'] = __($item['title']); |
| 60 | + } |
| 61 | + |
| 62 | + if (isset($item['description']) && is_string($item['description']) && $this->isTranslationKey($item['description'])) { |
| 63 | + $item['raw_description'] = $item['description']; |
| 64 | + $item['description'] = __($item['description']); |
| 65 | + } |
| 66 | + |
| 67 | + $this->staticItems[] = $item; |
| 68 | + } |
| 69 | + |
| 70 | + protected function isTranslationKey(string $text): bool |
| 71 | + { |
| 72 | + return str_contains($text, '::') || |
| 73 | + preg_match('/^[a-z0-9_]+\.[a-z0-9_.]+$/i', $text); |
| 74 | + } |
| 75 | + |
| 76 | + protected function refreshTranslations(array &$item): void |
| 77 | + { |
| 78 | + if (isset($item['raw_module'])) { |
| 79 | + $item['module'] = __($item['raw_module']); |
| 80 | + } |
| 81 | + |
| 82 | + if (isset($item['raw_title'])) { |
| 83 | + $item['title'] = __($item['raw_title']); |
| 84 | + } |
| 85 | + |
| 86 | + if (isset($item['raw_description'])) { |
| 87 | + $item['description'] = __($item['raw_description']); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public function getRegisteredModels(): array |
| 92 | + { |
| 93 | + return $this->registeredModels; |
| 94 | + } |
| 95 | + |
| 96 | + public function getManualItems(): array |
| 97 | + { |
| 98 | + $items = $this->manualItems; |
| 99 | + |
| 100 | + foreach ($items as &$item) { |
| 101 | + $this->refreshTranslations($item); |
| 102 | + } |
| 103 | + |
| 104 | + return $items; |
| 105 | + } |
| 106 | + |
| 107 | + public function getStaticItems(): array |
| 108 | + { |
| 109 | + $items = array_filter($this->staticItems, [$this, 'userCanViewItem']); |
| 110 | + |
| 111 | + foreach ($items as &$item) { |
| 112 | + $this->refreshTranslations($item); |
| 113 | + } |
| 114 | + |
| 115 | + return $items; |
| 116 | + } |
| 117 | + |
| 118 | + protected function userCanViewItem(array $item): bool |
| 119 | + { |
| 120 | + if (empty($item['permissions'])) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + $user = auth()->user(); |
| 125 | + if (!$user) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (is_array($item['permissions'])) { |
| 130 | + foreach ($item['permissions'] as $permission) { |
| 131 | + if ($user->can($permission)) { // @phpstan-ignore-line |
| 132 | + return true; |
| 133 | + } |
| 134 | + } |
| 135 | + } else { |
| 136 | + return $user->can($item['permissions']); // @phpstan-ignore-line |
| 137 | + } |
| 138 | + |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + public function search(string $term): array |
| 143 | + { |
| 144 | + if (empty($term)) { |
| 145 | + return []; |
| 146 | + } |
| 147 | + |
| 148 | + $results = []; |
| 149 | + $searchTerm = strtolower($term); |
| 150 | + |
| 151 | + foreach ($this->registeredModels as $modelClass) { |
| 152 | + $model = new $modelClass; |
| 153 | + $modelResults = $modelClass::spotlightSearch($term) |
| 154 | + ->get() |
| 155 | + ->filter(function ($item) { |
| 156 | + return $item->userCanViewInSpotlight(); |
| 157 | + }) |
| 158 | + ->map(function ($item) { |
| 159 | + return $item->toSpotlightResult(); |
| 160 | + }); |
| 161 | + |
| 162 | + $results = array_merge($results, $modelResults->toArray()); |
| 163 | + } |
| 164 | + |
| 165 | + $manualItems = $this->getManualItems(); |
| 166 | + |
| 167 | + foreach ($manualItems as $item) { |
| 168 | + $matchFound = false; |
| 169 | + |
| 170 | + if (str_contains(strtolower($item['title']), $searchTerm)) { |
| 171 | + $matchFound = true; |
| 172 | + } |
| 173 | + |
| 174 | + if (!empty($item['description']) && str_contains(strtolower($item['description']), $searchTerm)) { |
| 175 | + $matchFound = true; |
| 176 | + } |
| 177 | + |
| 178 | + if (!empty($item['module']) && str_contains(strtolower($item['module']), $searchTerm)) { |
| 179 | + $matchFound = true; |
| 180 | + } |
| 181 | + |
| 182 | + if ($matchFound && $this->userCanViewItem($item)) { |
| 183 | + $results[] = $item; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + usort($results, function ($a, $b) use ($searchTerm) { |
| 188 | + $aTitle = strtolower($a['title']); |
| 189 | + $bTitle = strtolower($b['title']); |
| 190 | + |
| 191 | + if ($aTitle === $searchTerm && $bTitle !== $searchTerm) { |
| 192 | + return -1; |
| 193 | + } |
| 194 | + if ($bTitle === $searchTerm && $aTitle !== $searchTerm) { |
| 195 | + return 1; |
| 196 | + } |
| 197 | + |
| 198 | + if (str_starts_with($aTitle, $searchTerm) && !str_starts_with($bTitle, $searchTerm)) { |
| 199 | + return -1; |
| 200 | + } |
| 201 | + if (str_starts_with($bTitle, $searchTerm) && !str_starts_with($aTitle, $searchTerm)) { |
| 202 | + return 1; |
| 203 | + } |
| 204 | + |
| 205 | + $aModule = strtolower($a['module'] ?? ''); |
| 206 | + $bModule = strtolower($b['module'] ?? ''); |
| 207 | + if ($aModule === $searchTerm && $bModule !== $searchTerm) { |
| 208 | + return -1; |
| 209 | + } |
| 210 | + if ($bModule === $searchTerm && $aModule !== $searchTerm) { |
| 211 | + return 1; |
| 212 | + } |
| 213 | + |
| 214 | + return strcmp($aTitle, $bTitle); |
| 215 | + }); |
| 216 | + |
| 217 | + return array_slice($results, 0); |
| 218 | + } |
| 219 | +} |
0 commit comments