-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathGeocoderService.php
More file actions
239 lines (195 loc) · 6.77 KB
/
Copy pathGeocoderService.php
File metadata and controls
239 lines (195 loc) · 6.77 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* This file is part of the Geocoder Laravel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Mike Bronner <mike@genealabs.com>
* @license MIT License
*/
declare(strict_types=1);
namespace Geocoder\Laravel\Providers;
use Geocoder\Laravel\Facades\Geocoder;
use Geocoder\Laravel\ProviderAndDumperAggregator;
use Geocoder\Model\Address;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use PhpToken;
use ReflectionClass;
class GeocoderService extends ServiceProvider
{
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
protected $defer = false;
protected static array $discoveredSerializableClasses = [];
public function boot(): void
{
$configPath = __DIR__ . "/../../config/geocoder.php";
$this->publishes([$configPath => $this->configPath("geocoder.php")], "config");
$this->mergeConfigFrom($configPath, "geocoder");
$this->registerSerializableClasses();
}
public function provides(): array
{
return ["geocoder", ProviderAndDumperAggregator::class];
}
public function register(): void
{
$this->app->alias("Geocoder", Geocoder::class);
$this->app->singleton(ProviderAndDumperAggregator::class, function () {
return (new ProviderAndDumperAggregator)
->registerProvidersFromConfig(collect(config("geocoder.providers")));
});
$this->app->bind("geocoder", ProviderAndDumperAggregator::class);
}
protected function registerSerializableClasses(): void
{
if (! config("geocoder.cache.auto_register_serializable_classes", true)) {
return;
}
$existing = config("cache.serializable_classes");
// Laravel 13 treats a `null` `serializable_classes` as "enforcement
// disabled" — any class may be unserialized from the cache. Converting
// it to an array here would silently switch the entire application into
// strict allow-list mode, breaking cache reads for classes outside this
// package. When enforcement is off there is nothing to register. See #210.
if ($existing === null) {
return;
}
if (self::$discoveredSerializableClasses === []) {
self::$discoveredSerializableClasses = $this->discoverSerializableClasses();
}
$existing = is_array($existing)
? $existing
: [];
config([
"cache.serializable_classes" => collect($existing)
->concat(self::$discoveredSerializableClasses)
->unique()
->values()
->toArray(),
]);
}
protected function discoverSerializableClasses(): array
{
$vendorRoot = $this->vendorRoot();
if ($vendorRoot === null) {
return [Collection::class];
}
return collect([
"{$vendorRoot}/willdurand/geocoder/Model",
"{$vendorRoot}/geocoder-php/*/Model",
])
->flatMap(function (string $pattern): array {
return glob($pattern)
?: [];
})
->flatMap(function (string $directory): array {
return glob("{$directory}/*.php")
?: [];
})
->flatMap(function (string $file): array {
return $this->classNamesFromVendorFile($file);
})
->prepend(Collection::class)
->unique()
->values()
->toArray();
}
protected function vendorRoot(): ?string
{
$addressFile = (new ReflectionClass(Address::class))->getFileName();
if ($addressFile === false) {
return null;
}
$directory = dirname($addressFile);
while (
$directory !== ""
&& $directory !== "/"
&& basename($directory) !== "vendor"
) {
$parent = dirname($directory);
if ($parent === $directory) {
return null;
}
$directory = $parent;
}
return basename($directory) === "vendor"
? $directory
: null;
}
protected function classNamesFromVendorFile(string $file): array
{
$contents = file_get_contents($file);
if ($contents === false) {
return [];
}
return $this->extractClassesFromTokens($this->tokenize($contents));
}
protected function tokenize(string $contents): array
{
return array_values(array_filter(
PhpToken::tokenize($contents),
fn (PhpToken $token): bool => ! $token->is([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]),
));
}
protected function extractClassesFromTokens(array $tokens): array
{
$namespace = "";
$classes = [];
foreach ($tokens as $tokenIndex => $token) {
if ($token->is(T_NAMESPACE)) {
$namespace = $this->readNamespaceAt($tokens, $tokenIndex + 1);
continue;
}
if (! $this->isClassDeclaration($tokens, $tokenIndex)) {
continue;
}
$classes[] = $this->qualify($namespace, $tokens[$tokenIndex + 1]->text);
}
return $classes;
}
protected function isClassDeclaration(array $tokens, int $tokenIndex): bool
{
if (
! $tokens[$tokenIndex]->is(T_CLASS)
|| (
$tokenIndex > 0
&& $tokens[$tokenIndex - 1]->is(T_NEW)
)
) {
return false;
}
return isset($tokens[$tokenIndex + 1])
&& $tokens[$tokenIndex + 1]->is(T_STRING);
}
protected function qualify(string $namespace, string $name): string
{
return $namespace !== ""
? "{$namespace}\\{$name}"
: $name;
}
protected function readNamespaceAt(array $tokens, int $startingTokenIndex): string
{
$namespaceParts = [];
$tokenCount = count($tokens);
for ($tokenIndex = $startingTokenIndex; $tokenIndex < $tokenCount; $tokenIndex++) {
if (! $tokens[$tokenIndex]->is([T_STRING, T_NAME_QUALIFIED, T_NS_SEPARATOR])) {
break;
}
$namespaceParts[] = $tokens[$tokenIndex]->text;
}
return implode("", $namespaceParts);
}
protected function configPath(string $path = ""): string
{
if (function_exists("config_path")) {
return config_path($path);
}
$pathParts = [
app()->basePath(),
"config",
trim($path, "/"),
];
return implode("/", $pathParts);
}
}