| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Helldar\LaravelLangPublisher\Support; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Helldar\LaravelLangPublisher\Concerns\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Helldar\LaravelLangPublisher\Facades\Arr as ArrFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Helldar\LaravelLangPublisher\Facades\Config as ConfigFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Helldar\LaravelLangPublisher\Facades\Reflection as ReflectionFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Helldar\Support\Facades\Helpers\Arr; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Helldar\Support\Facades\Helpers\Filesystem\Directory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Helldar\Support\Facades\Helpers\Filesystem\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Helldar\Support\Facades\Helpers\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | final class Locales | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use Logger; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * List of available locations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param  bool  $all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function available(bool $all = false): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 16 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->log('Getting list of available locations...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $locales = $this->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 16 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return $all ? $locales : $this->filter($locales); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * List of installed locations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function installed(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->log('Getting list of installed locations...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $json = $this->findJsonFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 7 |  |         $php  = $this->findPhpFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $installed = ArrFacade::unique(array_merge($json, $php)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $sorted = Arr::sort($installed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return array_values($sorted); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Retrieving a list of protected locales. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function protects(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->log('Retrieving a list of protected locales...'); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         return ArrFacade::unique([ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $this->getDefault(), | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $this->getFallback(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Checks if a language pack is installed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param  string  $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function isAvailable(string $locale): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 13 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->log('Checks if a language pack is installed...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 13 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return in_array($locale, $this->available(true), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 13 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * The checked locale protecting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param  string  $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function isProtected(string $locale): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 4 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->log('The checked locale protecting...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $locale === $this->getDefault() || $locale === $this->getFallback(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Checks whether it is possible to install the language pack. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param  string  $locale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function isInstalled(string $locale): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->log('Checks whether it is possible to install the language pack...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return in_array($locale, $this->installed(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Getting the default localization name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function getDefault(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->log('Getting the default localization name...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return ConfigFacade::defaultLocale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Getting the fallback localization name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function getFallback(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->log('Getting the fallback localization name...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return ConfigFacade::fallbackLocale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     protected function all(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->log('Getting a list of all available localizations without filtering...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return array_values(ReflectionFacade::getConstants(LocalesList::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 13 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     protected function filter(array $locales): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 13 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->log('Filtering localizations...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 13 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |         $unique = ArrFacade::unique($locales); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $ignore = ConfigFacade::ignores(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return array_values(array_filter($unique, static function ($locale) use ($ignore) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 16 |  |             return ! in_array($locale, $ignore, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 16 |  |     protected function findJsonFiles(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->log('Getting a list of localizations from json files...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $files = File::names($this->resourcesPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         return Arr::map($files, static function ($filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 10 |  |             return Str::before($filename, '.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 10 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 10 |  |     protected function findPhpFiles(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 10 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->log('Getting a list of localizations from php files...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 7 |  |         return Directory::names($this->resourcesPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     protected function resourcesPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 7 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $this->log('Getting the path to application resources...'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 7 |  |         return ConfigFacade::resourcesPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  | 
            
                        
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: