Passed
Push — master ( 78bb6e...bd5589 )
by Jean-Christophe
01:57
created

ArrayLoader::loadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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)){
1 ignored issue
show
Bug introduced by
$locale . $domain of type string is incompatible with the type boolean|string[] expected by parameter $keys of apc_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
		if(apc_exists(/** @scrutinizer ignore-type */ $locale.$domain)){
Loading history...
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
Bug introduced by
The constant DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
Bug introduced by
The constant DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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) {
1 ignored issue
show
Bug introduced by
The constant DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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