Completed
Push — master ( 1a59cb...7c1957 )
by Jean-Christophe
04:31
created

ArrayLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 2
	private function getRootKey($locale=null,$domain=null){
12 2
		return $this->rootDir.$locale??''.$domain??'';
13
	}
14 2
	public function __construct($rootDir){
15 2
		$this->rootDir=$rootDir;
16 2
	}
17
	
18 2
	public function load($locale, $domain = '*') {
19 2
		$key=$this->getRootKey($locale,$domain);
20 2
		if(apcu_exists($key)){
21
			Logger::info('Translate', 'Loading '.$locale.'.'.$domain.' from apcu_cache','load');
22
			return apcu_fetch($key);
23
		}
24 2
		$messages=[];
25 2
		$rootDirectory = $this->getRootDirectory($locale);
26 2
		if(file_exists($rootDirectory)){
27 1
			$files=UFileSystem::glob_recursive($rootDirectory.$domain.'.php');
28 1
			foreach ($files as $file){
29 1
				if(file_exists($file)){
30 1
					$name=basename($file,'.php');
31 1
					Logger::info('Translate', 'Loading '.$locale.'.'.$domain.' from file '.$name,'load',[get_class()]);
32 1
					$messages[$name]=$this->loadFile($file);
33
				}
34
			}
35 1
			$this->flatten($messages);
36 1
			apcu_store($key, $messages);
37
		}else{
38 1
			return false;
39
		}
40
		
41 1
		return $messages;
42
	}
43
	
44
	public function clearCache($locale=null,$domain=null){
45
		$iterator=new \APCuIterator('/^'.$this->getRootKey($locale,$domain).'/');
46
		foreach($iterator as $apcu_cache){
47
			apcu_delete($apcu_cache['key']);
48
		}
49
	}
50
	
51 1
	protected function loadFile($filename){
52 1
		return include $filename;
53
	}
54
	
55 2
	private function getRootDirectory($locale){
56 2
		return  $this->rootDir. \DS.$locale.\DS;
57
	}
58
	
59
	private function getDirectory($domain,&$filename){
60
		$parts=explode('.',$domain);
61
		$filename=array_pop($parts).".php";
62
		return implode(\DS, $parts);
63
	}
64
	
65
	/**
66
	 * Flattens an nested array of translations.
67
	 *
68
	 * The scheme used is:
69
	 *   'key' => array('key2' => array('key3' => 'value'))
70
	 * Becomes:
71
	 *   'key.key2.key3' => 'value'
72
	 *
73
	 * This function takes an array by reference and will modify it
74
	 *
75
	 * @param array  &$messages The array that will be flattened
76
	 * @param array  $subnode   Current subnode being parsed, used internally for recursive calls
77
	 * @param string $path      Current path being parsed, used internally for recursive calls
78
	 */
79 1
	private function flatten(array &$messages, array $subnode = null, $path = null){
80 1
		if (null === $subnode) {
81 1
			$subnode = &$messages;
82
		}
83 1
		foreach ($subnode as $key => $value) {
84 1
			if (\is_array($value)) {
85 1
				$nodePath = $path ? $path.'.'.$key : $key;
86 1
				$this->flatten($messages, $value, $nodePath);
87 1
				if (null === $path) {
88 1
					unset($messages[$key]);
89
				}
90 1
			} elseif (null !== $path) {
91 1
				$messages[$path.'.'.$key] = $value;
92
			}
93
		}
94 1
	}
95
	
96
	public function save($messages, $locale,$domain) {
97
		$content="<?php\nreturn ".UArray::asPhpArray($messages,'array').';';
98
		$filename="";
99
		$path=$this->getRootDirectory($locale).$this->getDirectory($domain,$filename);
100
		if(UFileSystem::safeMkdir($path)){
101
			if (@\file_put_contents($path.\DS.$filename, $content, LOCK_EX) === false) {
102
				throw new \Exception("Unable to write cache file: {$filename}");
103
			}
104
		}else{
105
			throw new \Exception("Unable to create folder : {$path}");
106
		}
107
	}
108
}
109