|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\translation\loader; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
5
|
|
|
use Ubiquity\utils\base\UArray; |
|
6
|
|
|
use Ubiquity\log\Logger; |
|
7
|
|
|
|
|
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)){ |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function getDirectory($domain,&$filename){ |
|
49
|
|
|
$parts=explode(".",$domain); |
|
50
|
|
|
$filename=array_pop($parts).".php"; |
|
51
|
|
|
return implode(\DS, $parts); |
|
|
|
|
|
|
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){ |
|
69
|
|
|
if (null === $subnode) { |
|
70
|
|
|
$subnode = &$messages; |
|
71
|
|
|
} |
|
72
|
|
|
foreach ($subnode as $key => $value) { |
|
73
|
|
|
if (\is_array($value)) { |
|
74
|
|
|
$nodePath = $path ? $path.'.'.$key : $key; |
|
75
|
|
|
$this->flatten($messages, $value, $nodePath); |
|
76
|
|
|
if (null === $path) { |
|
77
|
|
|
unset($messages[$key]); |
|
78
|
|
|
} |
|
79
|
|
|
} elseif (null !== $path) { |
|
80
|
|
|
$messages[$path.'.'.$key] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function save($messages, $locale,$domain) { |
|
86
|
|
|
$content="<?php\nreturn ".UArray::asPhpArray($messages,'array').';'; |
|
87
|
|
|
$filename=""; |
|
88
|
|
|
$path=$this->getRootDirectory($locale).$this->getDirectory($domain,$filename); |
|
89
|
|
|
if(UFileSystem::safeMkdir($path)){ |
|
90
|
|
|
if (@\file_put_contents($path.\DS.$filename, $content, LOCK_EX) === false) { |
|
|
|
|
|
|
91
|
|
|
throw new \Exception("Unable to write cache file: {$filename}"); |
|
92
|
|
|
} |
|
93
|
|
|
}else{ |
|
94
|
|
|
throw new \Exception("Unable to create folder : {$path}"); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|