| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace kalanis\kw_langs\Loaders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use kalanis\kw_langs\Interfaces\ILoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use kalanis\kw_paths\Interfaces\IPaths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use kalanis\kw_paths\Path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use kalanis\kw_routed_paths\RoutedPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class PhpLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package kalanis\kw_langs\Loaders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Load config data from defined source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Contains personalized autoloader for configs! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class PhpLoader implements ILoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var string[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected array $pathMasks = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%8$s%9$s', # all modules, translations in sub dir separated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%7$s%9$s', # all modules, translations as single file in sub dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%8$s', # all modules, translations separated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         '%2$s%1$s%5$s%1$s%6$s%1$s%7$s%9$s', # all modules, translations in single file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         '%2$s%1$s%3$s%1$s%4$s%1$s%7$s%1$s%8$s%9$s', # custom user translations separated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         '%2$s%1$s%3$s%1$s%4$s%1$s%7$s%1$s%7$s%9$s', # custom user translation as single file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         '%2$s%1$s%7$s%1$s%8$s%9$s', # in lang root translations separated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         '%2$s%1$s%7$s%1$s%7$s%9$s', # in lang root as single file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected Path $pathLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected RoutedPath $routedLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 6 |  |     public function __construct(Path $pathLib, RoutedPath $routedLib) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |         $this->pathLib = $pathLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 6 |  |         $this->routedLib = $routedLib; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 3 |  |     public function load(string $module, string $lang): ?array | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 3 |  |         $path = $this->contentPath($module, $lang); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 3 |  |         return (!empty($path)) ? $this->includedLang($path) : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $lang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |     protected function contentPath(string $module, string $lang): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |         $basicLookupDir = $this->pathLib->getDocumentRoot() . $this->pathLib->getPathToSystemRoot(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 3 |  |         foreach ($this->pathMasks as $pathMask) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |             $unmasked = sprintf( $pathMask, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |                 DIRECTORY_SEPARATOR, $basicLookupDir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |                 IPaths::DIR_USER, $this->routedLib->getUser(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |                 IPaths::DIR_MODULE, $module, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |                 IPaths::DIR_LANG, $lang, IPaths::EXT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |             $path = realpath($unmasked); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             if ($path && is_file($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                 return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return array<string, string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |     protected function includedLang(string $path): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         $lang = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         include_once ($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return (array) $lang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |