Passed
Branch master (7dd754)
by Jean-Christophe
16:28
created

ArrayLoader::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3.0175
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
	private $key = "translations/";
22
23 3
	private function getRootKey($locale = null, $domain = null) {
24 3
		return $this->key . $locale ?? '' . $domain ?? '';
25
	}
26
27 3
	public function __construct($rootDir) {
28 3
		$this->rootDir = $rootDir;
29 3
	}
30
31 1
	public function loadDomain($locale, $domain) {
32 1
		$messages = [ ];
33 1
		$rootDirectory = $this->getRootDirectory ( $locale );
34 1
		if (file_exists ( $rootDirectory )) {
35 1
			$filename = $rootDirectory . \DS . $domain . '.php';
36 1
			if (file_exists ( $filename )) {
37 1
				$messages = $this->loadFile ( $filename );
38
			}
39
		}
40 1
		return $messages;
41
	}
42
43 3
	public function load($locale, $domain = '*') {
44 3
		$key = $this->getRootKey ( $locale, $domain );
45 3
		if (CacheManager::$cache->exists ( $key )) {
46 1
			return CacheManager::$cache->fetch ( $key );
47
		}
48 3
		$messages = [ ];
49 3
		$rootDirectory = $this->getRootDirectory ( $locale );
50 3
		if (file_exists ( $rootDirectory )) {
51 3
			$files = UFileSystem::glob_recursive ( $rootDirectory . $domain . '.php' );
52 3
			foreach ( $files as $file ) {
53 3
				if (file_exists ( $file )) {
54 3
					$name = basename ( $file, '.php' );
55 3
					Logger::info ( 'Translate', 'Loading ' . $locale . '.' . $domain . ' from file ' . $name, 'load', [ get_class () ] );
56 3
					$messages [$name] = $this->loadFile ( $file );
57
				}
58
			}
59 3
			$this->flatten ( $messages );
60 3
			CacheManager::$cache->store ( $key, "return " . UArray::asPhpArray ( $messages, 'array' ) . ';' );
61
		} else {
62 1
			return false;
63
		}
64
65 3
		return $messages;
66
	}
67
68 1
	public function clearCache($locale = null, $domain = null) {
69 1
		if (isset ( $locale )) {
70 1
			CacheManager::$cache->remove ( $this->getRootKey ( $locale, $domain ) );
71
		} else {
72
			CacheManager::$cache->clearCache ( $this->getRootKey ( $locale, $domain ) );
73
		}
74 1
	}
75
76 3
	protected function loadFile($filename) {
77 3
		return include $filename;
78
	}
79
80 3
	private function getRootDirectory($locale) {
81 3
		return $this->rootDir . \DS . $locale . \DS;
82
	}
83
84 1
	private function getDirectory($domain, &$filename) {
85 1
		$parts = explode ( '.', $domain );
86 1
		$filename = array_pop ( $parts ) . ".php";
87 1
		return implode ( \DS, $parts );
88
	}
89
90
	/**
91
	 * Flattens an nested array of translations.
92
	 *
93
	 * The scheme used is:
94
	 * 'key' => array('key2' => array('key3' => 'value'))
95
	 * Becomes:
96
	 * 'key.key2.key3' => 'value'
97
	 *
98
	 * This function takes an array by reference and will modify it
99
	 *
100
	 * @param array &$messages The array that will be flattened
101
	 * @param array $subnode Current subnode being parsed, used internally for recursive calls
102
	 * @param string $path Current path being parsed, used internally for recursive calls
103
	 */
104 3
	private function flatten(array &$messages, array $subnode = null, $path = null) {
105 3
		if (null === $subnode) {
106 3
			$subnode = &$messages;
107
		}
108 3
		foreach ( $subnode as $key => $value ) {
109 3
			if (\is_array ( $value )) {
110 3
				$nodePath = $path ? $path . '.' . $key : $key;
111 3
				$this->flatten ( $messages, $value, $nodePath );
112 3
				if (null === $path) {
113 3
					unset ( $messages [$key] );
114
				}
115 3
			} elseif (null !== $path) {
116 3
				$messages [$path . '.' . $key] = $value;
117
			}
118
		}
119 3
	}
120
121 1
	public function save($messages, $locale, $domain) {
122 1
		$content = "<?php\nreturn " . UArray::asPhpArray ( $messages, 'array' ) . ';';
123 1
		$filename = "";
124 1
		$path = $this->getRootDirectory ( $locale ) . $this->getDirectory ( $domain, $filename );
125 1
		if (UFileSystem::safeMkdir ( $path )) {
126 1
			if (@\file_put_contents ( $path . \DS . $filename, $content, LOCK_EX ) === false) {
127 1
				throw new \Exception ( "Unable to write cache file: {$filename}" );
128
			}
129
		} else {
130
			throw new \Exception ( "Unable to create folder : {$path}" );
131
		}
132 1
	}
133
134
	/**
135
	 *
136
	 * @return string
137
	 */
138 1
	public function getRootDir() {
139 1
		return $this->rootDir;
140
	}
141
142 1
	public function getDomains($locale) {
143 1
		$domains = [ ];
144 1
		$rootDirectory = $this->getRootDirectory ( $locale );
145 1
		if (file_exists ( $rootDirectory )) {
146 1
			$files = UFileSystem::glob_recursive ( $rootDirectory . '*.php' );
147 1
			foreach ( $files as $file ) {
148 1
				$domains [] = basename ( $file, '.php' );
149
			}
150
		}
151 1
		return $domains;
152
	}
153
154 1
	public function cacheExists($locale, $domain = '*') {
155 1
		$key = $this->getRootKey ( $locale, $domain );
156 1
		return CacheManager::$cache->exists ( $key );
157
	}
158
}
159