Test Failed
Branch master (ba7917)
by Jean-Christophe
08:55
created

ArrayLoader::load()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

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