| Total Complexity | 19 |
| Total Lines | 87 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | class ArrayLoader implements LoaderInterface { |
||
| 9 | private $rootDir; |
||
| 10 | |||
| 11 | public function __construct($rootDir){ |
||
| 12 | $this->rootDir=$rootDir; |
||
| 13 | } |
||
| 14 | |||
| 15 | public function load($locale, $domain = '*') { |
||
| 16 | if(apc_exists($locale.$domain)){ |
||
|
1 ignored issue
–
show
|
|||
| 17 | Logger::info("Translate", "Loading ".$locale.'.'.$domain." from apc_cache","load"); |
||
| 18 | return apc_fetch($locale.$domain); |
||
| 19 | } |
||
| 20 | $messages=[]; |
||
| 21 | $rootDirectory = $this->getRootDirectory($locale); |
||
| 22 | if(file_exists($rootDirectory)){ |
||
| 23 | $files=UFileSystem::glob_recursive($rootDirectory.$domain.".php"); |
||
| 24 | foreach ($files as $file){ |
||
| 25 | if(file_exists($file)){ |
||
| 26 | $name=basename($file,".php"); |
||
| 27 | Logger::info("Translate", "Loading ".$locale.'.'.$domain." from file ".$name,"load",[get_class()]); |
||
| 28 | $messages[$name]=$this->loadFile($file); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | $this->flatten($messages); |
||
| 32 | apc_store($locale.$domain, $messages); |
||
| 33 | }else{ |
||
| 34 | return false; |
||
| 35 | } |
||
| 36 | |||
| 37 | return $messages; |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function loadFile($filename){ |
||
| 41 | return include $filename; |
||
| 42 | } |
||
| 43 | |||
| 44 | private function getRootDirectory($locale){ |
||
| 45 | return $this->rootDir. \DS.$locale.\DS; |
||
|
1 ignored issue
–
show
|
|||
| 46 | } |
||
| 47 | |||
| 48 | private function getDirectory($domain,&$filename){ |
||
| 49 | $parts=explode(".",$domain); |
||
| 50 | $filename=array_pop($parts).".php"; |
||
| 51 | return implode(\DS, $parts); |
||
|
1 ignored issue
–
show
|
|||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Flattens an nested array of translations. |
||
| 56 | * |
||
| 57 | * The scheme used is: |
||
| 58 | * 'key' => array('key2' => array('key3' => 'value')) |
||
| 59 | * Becomes: |
||
| 60 | * 'key.key2.key3' => 'value' |
||
| 61 | * |
||
| 62 | * This function takes an array by reference and will modify it |
||
| 63 | * |
||
| 64 | * @param array &$messages The array that will be flattened |
||
| 65 | * @param array $subnode Current subnode being parsed, used internally for recursive calls |
||
| 66 | * @param string $path Current path being parsed, used internally for recursive calls |
||
| 67 | */ |
||
| 68 | private function flatten(array &$messages, array $subnode = null, $path = null){ |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | public function save($messages, $locale,$domain) { |
||
| 86 | $content="<?php\nreturn ".UArray::asPhpArray($messages,'array').';'; |
||
| 98 |