1515
1616use Geocoder \Laravel \Facades \Geocoder ;
1717use Geocoder \Laravel \ProviderAndDumperAggregator ;
18+ use Geocoder \Model \Address ;
1819use Illuminate \Support \Collection ;
1920use Illuminate \Support \ServiceProvider ;
2021use PhpToken ;
22+ use ReflectionClass ;
2123
2224class GeocoderService extends ServiceProvider
2325{
@@ -74,9 +76,15 @@ protected function registerSerializableClasses(): void
7476
7577 protected function discoverSerializableClasses (): array
7678 {
79+ $ vendorRoot = $ this ->vendorRoot ();
80+
81+ if ($ vendorRoot === null ) {
82+ return [Collection::class];
83+ }
84+
7785 return collect ([
78- base_path ( " vendor /willdurand/geocoder/Model ") ,
79- base_path ( " vendor /geocoder-php/*/Model ") ,
86+ "{ $ vendorRoot } /willdurand/geocoder/Model " ,
87+ "{ $ vendorRoot } /geocoder-php/*/Model " ,
8088 ])
8189 ->flatMap (function (string $ pattern ): array {
8290 return glob ($ pattern )
@@ -95,6 +103,35 @@ protected function discoverSerializableClasses(): array
95103 ->toArray ();
96104 }
97105
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+
98135 protected function classNamesFromVendorFile (string $ file ): array
99136 {
100137 $ contents = file_get_contents ($ file );
0 commit comments