| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\LaravelResourceEndpoints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Routing\Exceptions\UrlGenerationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Routing\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Routing\RouteUrlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Routing\UrlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class UrlResolver extends RouteUrlGenerator | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct(UrlGenerator $urlGenerator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         parent::__construct($urlGenerator, $urlGenerator->getRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         // The URL defaults are not given through automatically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->defaults($urlGenerator->getDefaultParameters()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function resolve(Route $route, array $parameters): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $parameters = $this->url->formatParameters($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             return $this->to($route, $parameters, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } catch (UrlGenerationException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             // Create an uri with missing parameters between brackets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $domain = $this->getRouteDomain($route, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             return $this->addQueryString($this->url->format( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $root = $this->replaceRootParameters($route, $domain, $parameters), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $this->replaceRouteParameters($route->uri(), $parameters), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ), $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function replaceRouteParameters($path, array &$parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |          * We should try to find a solution to not including this function, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          * this had to be added to support Laravel 6: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |          * https://github.com/laravel/framework/issues/29736 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $path = $this->replaceNamedParameters($path, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $path = preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return (empty($parameters) && ! Str::endsWith($match[0], '?}')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ? $match[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 : array_shift($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         }, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return trim(preg_replace('/\{.*?\?\}/', '', $path), '/'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  |