Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ArrayLoader.php
Go to the documentation of this file.
1<?php
2
4
6use Ubiquity\utils\base\UArray;
9
19class ArrayLoader implements LoaderInterface {
20 private $rootDir;
21 private $key = 'translations/';
22
23 private function getRootKey($locale = null, $domain = null) {
24 return $this->key . $locale ?? '' . $domain ?? '';
25 }
26
27 public function __construct($rootDir) {
28 $this->rootDir = $rootDir;
29 }
30
31 public function loadDomain($locale, $domain) {
32 $messages = [ ];
33 $rootDirectory = $this->getRootDirectory ( $locale );
34 if (\file_exists ( $rootDirectory )) {
35 $filename = $rootDirectory . \DS . $domain . '.php';
36 if (\file_exists ( $filename )) {
37 $messages = $this->loadFile ( $filename );
38 }
39 }
40 return $messages;
41 }
42
43 public function load($locale, $domain = '*') {
44 $key = $this->getRootKey ( $locale, $domain );
45 if (CacheManager::$cache->exists ( $key )) {
46 return CacheManager::$cache->fetch ( $key );
47 }
48 $messages = [ ];
49 $rootDirectory = $this->getRootDirectory ( $locale );
50 if (\file_exists ( $rootDirectory )) {
51 $files = UFileSystem::glob_recursive ( $rootDirectory . $domain . '.php' );
52 foreach ( $files as $file ) {
53 if (\file_exists ( $file )) {
54 $name = \basename ( $file, '.php' );
55 Logger::info ( 'Translate', 'Loading ' . $locale . '.' . $domain . ' from file ' . $name, 'load', [ \get_class () ] );
56 $messages [$name] = $this->loadFile ( $file );
57 }
58 }
59 $this->flatten ( $messages );
60 CacheManager::$cache->store ( $key, $messages );
61 } else {
62 return false;
63 }
64
65 return $messages;
66 }
67
68 public function clearCache($locale = null, $domain = null) {
69 if (isset ( $locale )) {
70 CacheManager::$cache->remove ( $this->getRootKey ( $locale, $domain ) );
71 } else {
72 CacheManager::$cache->clearCache ( $this->getRootKey ( $locale, $domain ) );
73 }
74 }
75
76 protected function loadFile($filename) {
77 return include $filename;
78 }
79
80 private function getRootDirectory($locale) {
81 return $this->rootDir . \DS . $locale . \DS;
82 }
83
84 private function getDirectory($domain, &$filename) {
85 $parts = \explode ( '.', $domain );
86 $filename = \array_pop ( $parts ) . ".php";
87 return \implode ( \DS, $parts );
88 }
89
104 private function flatten(array &$messages, array $subnode = null, $path = null) {
105 if (null === $subnode) {
106 $subnode = &$messages;
107 }
108 foreach ( $subnode as $key => $value ) {
109 if (\is_array ( $value )) {
110 $nodePath = $path ? $path . '.' . $key : $key;
111 $this->flatten ( $messages, $value, $nodePath );
112 if (null === $path) {
113 unset ( $messages [$key] );
114 }
115 } elseif (null !== $path) {
116 $messages [$path . '.' . $key] = $value;
117 }
118 }
119 }
120
121 public function save($messages, $locale, $domain) {
122 $content = "<?php\nreturn " . UArray::asPhpArray ( $messages, 'array' ) . ';';
123 $filename = "";
124 $path = $this->getRootDirectory ( $locale ) . $this->getDirectory ( $domain, $filename );
125 if (UFileSystem::safeMkdir ( $path )) {
126 if (@\file_put_contents ( $path . \DS . $filename, $content, LOCK_EX ) === false) {
127 throw new \Exception ( "Unable to write cache file: {$filename}" );
128 }
129 } else {
130 throw new \Exception ( "Unable to create folder : {$path}" );
131 }
132 }
133
138 public function getRootDir() {
139 return $this->rootDir;
140 }
141
142 public function getDomains($locale) {
143 $domains = [ ];
144 $rootDirectory = $this->getRootDirectory ( $locale );
145 if (\file_exists ( $rootDirectory )) {
146 $files = UFileSystem::glob_recursive ( $rootDirectory . '*.php' );
147 foreach ( $files as $file ) {
148 $domains [] = \basename ( $file, '.php' );
149 }
150 }
151 return $domains;
152 }
153
154 public function cacheExists($locale, $domain = '*') {
155 $key = $this->getRootKey ( $locale, $domain );
156 return CacheManager::$cache->exists ( $key );
157 }
158}
Manager for caches (Router, Rest, models).
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.