|
1 | | -<?php namespace Geocoder\Laravel\Providers; |
| 1 | +<?php |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * This file is part of the Geocoder Laravel package. |
5 | 5 | * For the full copyright and license information, please view the LICENSE |
6 | 6 | * file that was distributed with this source code. |
7 | 7 | * |
8 | | - * @author Mike Bronner <hello@genealabs.com> |
9 | | - * @license MIT License |
| 8 | + * @author Mike Bronner <mike@genealabs.com> |
| 9 | + * @license MIT License |
10 | 10 | */ |
11 | 11 |
|
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Geocoder\Laravel\Providers; |
| 15 | + |
12 | 16 | use Geocoder\Laravel\Facades\Geocoder; |
13 | 17 | use Geocoder\Laravel\ProviderAndDumperAggregator; |
| 18 | +use Geocoder\Model\Address; |
| 19 | +use Illuminate\Support\Collection; |
14 | 20 | use Illuminate\Support\ServiceProvider; |
| 21 | +use PhpToken; |
| 22 | +use ReflectionClass; |
15 | 23 |
|
16 | 24 | class GeocoderService extends ServiceProvider |
17 | 25 | { |
| 26 | + // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint |
18 | 27 | protected $defer = false; |
| 28 | + protected static array $discoveredSerializableClasses = []; |
19 | 29 |
|
20 | | - public function boot() |
| 30 | + public function boot(): void |
21 | 31 | { |
22 | 32 | $configPath = __DIR__ . "/../../config/geocoder.php"; |
23 | | - $this->publishes( |
24 | | - [$configPath => $this->configPath("geocoder.php")], |
25 | | - "config" |
26 | | - ); |
| 33 | + $this->publishes([$configPath => $this->configPath("geocoder.php")], "config"); |
27 | 34 | $this->mergeConfigFrom($configPath, "geocoder"); |
| 35 | + $this->registerSerializableClasses(); |
| 36 | + } |
| 37 | + |
| 38 | + public function provides(): array |
| 39 | + { |
| 40 | + return ["geocoder", ProviderAndDumperAggregator::class]; |
28 | 41 | } |
29 | 42 |
|
30 | | - public function register() |
| 43 | + public function register(): void |
31 | 44 | { |
32 | 45 | $this->app->alias("Geocoder", Geocoder::class); |
33 | 46 | $this->app->singleton(ProviderAndDumperAggregator::class, function () { |
34 | 47 | return (new ProviderAndDumperAggregator) |
35 | 48 | ->registerProvidersFromConfig(collect(config("geocoder.providers"))); |
36 | 49 | }); |
37 | | - $this->app->bind('geocoder', ProviderAndDumperAggregator::class); |
| 50 | + $this->app->bind("geocoder", ProviderAndDumperAggregator::class); |
38 | 51 | } |
39 | 52 |
|
40 | | - public function provides() : array |
| 53 | + protected function registerSerializableClasses(): void |
41 | 54 | { |
42 | | - return ["geocoder", ProviderAndDumperAggregator::class]; |
| 55 | + if (! config("geocoder.cache.auto_register_serializable_classes", true)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (self::$discoveredSerializableClasses === []) { |
| 60 | + self::$discoveredSerializableClasses = $this->discoverSerializableClasses(); |
| 61 | + } |
| 62 | + |
| 63 | + $existing = config("cache.serializable_classes"); |
| 64 | + $existing = is_array($existing) |
| 65 | + ? $existing |
| 66 | + : []; |
| 67 | + |
| 68 | + config([ |
| 69 | + "cache.serializable_classes" => collect($existing) |
| 70 | + ->concat(self::$discoveredSerializableClasses) |
| 71 | + ->unique() |
| 72 | + ->values() |
| 73 | + ->toArray(), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + protected function discoverSerializableClasses(): array |
| 78 | + { |
| 79 | + $vendorRoot = $this->vendorRoot(); |
| 80 | + |
| 81 | + if ($vendorRoot === null) { |
| 82 | + return [Collection::class]; |
| 83 | + } |
| 84 | + |
| 85 | + return collect([ |
| 86 | + "{$vendorRoot}/willdurand/geocoder/Model", |
| 87 | + "{$vendorRoot}/geocoder-php/*/Model", |
| 88 | + ]) |
| 89 | + ->flatMap(function (string $pattern): array { |
| 90 | + return glob($pattern) |
| 91 | + ?: []; |
| 92 | + }) |
| 93 | + ->flatMap(function (string $directory): array { |
| 94 | + return glob("{$directory}/*.php") |
| 95 | + ?: []; |
| 96 | + }) |
| 97 | + ->flatMap(function (string $file): array { |
| 98 | + return $this->classNamesFromVendorFile($file); |
| 99 | + }) |
| 100 | + ->prepend(Collection::class) |
| 101 | + ->unique() |
| 102 | + ->values() |
| 103 | + ->toArray(); |
| 104 | + } |
| 105 | + |
| 106 | + protected function vendorRoot(): ?string |
| 107 | + { |
| 108 | + $addressFile = (new ReflectionClass(Address::class))->getFileName(); |
| 109 | + |
| 110 | + if ($addressFile === false) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + $directory = dirname($addressFile); |
| 115 | + |
| 116 | + while ( |
| 117 | + $directory !== "" |
| 118 | + && $directory !== "/" |
| 119 | + && basename($directory) !== "vendor" |
| 120 | + ) { |
| 121 | + $parent = dirname($directory); |
| 122 | + |
| 123 | + if ($parent === $directory) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + $directory = $parent; |
| 128 | + } |
| 129 | + |
| 130 | + return basename($directory) === "vendor" |
| 131 | + ? $directory |
| 132 | + : null; |
| 133 | + } |
| 134 | + |
| 135 | + protected function classNamesFromVendorFile(string $file): array |
| 136 | + { |
| 137 | + $contents = file_get_contents($file); |
| 138 | + |
| 139 | + if ($contents === false) { |
| 140 | + return []; |
| 141 | + } |
| 142 | + |
| 143 | + return $this->extractClassesFromTokens($this->tokenize($contents)); |
| 144 | + } |
| 145 | + |
| 146 | + protected function tokenize(string $contents): array |
| 147 | + { |
| 148 | + return array_values(array_filter( |
| 149 | + PhpToken::tokenize($contents), |
| 150 | + fn (PhpToken $token): bool => ! $token->is([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]), |
| 151 | + )); |
| 152 | + } |
| 153 | + |
| 154 | + protected function extractClassesFromTokens(array $tokens): array |
| 155 | + { |
| 156 | + $namespace = ""; |
| 157 | + $classes = []; |
| 158 | + |
| 159 | + foreach ($tokens as $tokenIndex => $token) { |
| 160 | + if ($token->is(T_NAMESPACE)) { |
| 161 | + $namespace = $this->readNamespaceAt($tokens, $tokenIndex + 1); |
| 162 | + |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + if (! $this->isClassDeclaration($tokens, $tokenIndex)) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + $classes[] = $this->qualify($namespace, $tokens[$tokenIndex + 1]->text); |
| 171 | + } |
| 172 | + |
| 173 | + return $classes; |
| 174 | + } |
| 175 | + |
| 176 | + protected function isClassDeclaration(array $tokens, int $tokenIndex): bool |
| 177 | + { |
| 178 | + if ( |
| 179 | + ! $tokens[$tokenIndex]->is(T_CLASS) |
| 180 | + || ( |
| 181 | + $tokenIndex > 0 |
| 182 | + && $tokens[$tokenIndex - 1]->is(T_NEW) |
| 183 | + ) |
| 184 | + ) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + return isset($tokens[$tokenIndex + 1]) |
| 189 | + && $tokens[$tokenIndex + 1]->is(T_STRING); |
| 190 | + } |
| 191 | + |
| 192 | + protected function qualify(string $namespace, string $name): string |
| 193 | + { |
| 194 | + return $namespace !== "" |
| 195 | + ? "{$namespace}\\{$name}" |
| 196 | + : $name; |
| 197 | + } |
| 198 | + |
| 199 | + protected function readNamespaceAt(array $tokens, int $startingTokenIndex): string |
| 200 | + { |
| 201 | + $namespaceParts = []; |
| 202 | + $tokenCount = count($tokens); |
| 203 | + |
| 204 | + for ($tokenIndex = $startingTokenIndex; $tokenIndex < $tokenCount; $tokenIndex++) { |
| 205 | + if (! $tokens[$tokenIndex]->is([T_STRING, T_NAME_QUALIFIED, T_NS_SEPARATOR])) { |
| 206 | + break; |
| 207 | + } |
| 208 | + |
| 209 | + $namespaceParts[] = $tokens[$tokenIndex]->text; |
| 210 | + } |
| 211 | + |
| 212 | + return implode("", $namespaceParts); |
43 | 213 | } |
44 | 214 |
|
45 | | - protected function configPath(string $path = "") : string |
| 215 | + protected function configPath(string $path = ""): string |
46 | 216 | { |
47 | 217 | if (function_exists("config_path")) { |
48 | 218 | return config_path($path); |
|
0 commit comments