| 1 |  |  | <?php namespace App\Services\Routing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Illuminate\Container\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Contracts\Events\Dispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class LocalizedRouter extends Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 15 |  |     public function __construct(Dispatcher $events, Container $container = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 15 |  |         parent::__construct($events, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 15 |  |         $this->routes = new LocalizedRouteCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 15 |  |     protected function addRoute($methods, $uri, $action) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 15 |  |         if (isset($action['locale'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             // Now we apply our Localization modifications. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 15 |  |             $uri = $this->localizeUris($uri, $action['locale']); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 15 |  |         return parent::addRoute($methods, $uri, $action); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 15 |  |     public function resource($name, $controller, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 15 |  |         if ($this->container && $this->container->bound(LocalizedResourceRegistrar::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $registrar = $this->container->make(LocalizedResourceRegistrar::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 15 |  |             $registrar = new LocalizedResourceRegistrar($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 15 |  |         $registrar->register($name, $controller, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 15 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Translates URIs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 15 |  |     private function localizeUris($uri, $locale) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 15 |  |         $uriExploded = explode('/', trim($uri, '/')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 15 |  |         $localizedUriTranslationBitParts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 15 |  |         while (list($level, $bitName) = each($uriExploded)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 15 |  |             if ($level == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 15 |  |                 $localizedUriTranslationBitParts[$level] = 'routes.' . $bitName . '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 15 |  |                 $localizedUriTranslationBitParts[$level] = trim($localizedUriTranslationBitParts[$level - 1], '.') . '.' . $bitName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 15 |  |         foreach ($localizedUriTranslationBitParts as $level => &$translationBitPart) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 15 |  |             $phraseToGetTranslationFor = $translationBitPart; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 15 |  |             if (preg_match('#(?<!routes)\.\{[^\}]+\}\.#', $translationBitPart)) { // For lower-level paths, in order not to hit 'routes.' index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 $phraseToGetTranslationFor = preg_replace('#\{[^\}]+\}\.?#', '', $translationBitPart); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 15 |  |             app('translator')->setLocale($locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 15 |  |             $translatedPhrase = app('translator')->get($phraseToGetTranslationFor); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 15 |  |             if (false !== strpos($translatedPhrase, '.')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 15 |  |                 $translationBitPart = $uriExploded[$level]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 15 |  |                 $translationBitPart = $translatedPhrase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 15 |  |             unset($translationBitPart); // Delete the reference (won't delete the original). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 15 |  |         return implode('/', $localizedUriTranslationBitParts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |