Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
MessagesUpdates.php
Go to the documentation of this file.
1<?php
2
4
6
18 protected $locale;
19 protected $domain;
20 protected $key = "tmp/translations/";
21 protected $toUpdate;
22 protected $toAdd;
23 protected $toDelete;
24 protected $dirty;
25 protected $newKeys;
26
27 protected function getKey() {
28 return md5 ( $this->locale . '.' . $this->domain );
29 }
30
31 public function __construct($locale, $domain) {
32 $this->locale = $locale;
33 $this->domain = $domain;
34 $this->dirty = false;
35 }
36
37 public function load() {
38 $key = $this->getKey ();
39 if (CacheManager::$cache->exists ( $this->key . $key )) {
40 $array = CacheManager::$cache->fetch ( $this->key . $key );
41 }
42 $this->newKeys = $array ['newKeys'] ?? [ ];
43 $this->toAdd = $array ['toAdd'] ?? [ ];
44 $this->toUpdate = $array ['toUpdate'] ?? [ ];
45 $this->toDelete = $array ['toDelete'] ?? [ ];
46 }
47
48 public function exists() {
49 $key = $this->getKey ();
50 return CacheManager::$cache->exists ( $this->key . $key );
51 }
52
53 public function hasUpdates() {
54 return \count ( $this->toUpdate ) > 0 || \count ( $this->toDelete ) > 0 || \count ( $this->toAdd ) > 0;
55 }
56
57 public function mergeMessages($messages, $beforeSave = false) {
58 foreach ( $this->toDelete as $k ) {
59 if (isset ( $messages [$k] )) {
60 unset ( $messages [$k] );
61 }
62 }
63 foreach ( $this->toUpdate as $k => $v ) {
64 $messages [$k] = $v;
65 }
66 foreach ( $this->toAdd as $k => $v ) {
67 if (isset ( $this->newKeys [$k] )) {
68 if ($beforeSave) {
69 $messages [$k] = $v;
70 } else {
71 $messages [$k] = [ $v,$this->newKeys [$k] ];
72 }
73 }
74 }
75 return $messages;
76 }
77
78 public function addToDelete($key) {
79 $this->toDelete [] = $key;
80 if (isset ( $this->toUpdate [$key] )) {
81 unset ( $this->toUpdate [$key] );
82 }
83 $this->dirty = true;
84 }
85
86 public function updateValue($key, $value) {
87 $this->toUpdate [$key] = $value;
88 if (($index = array_search ( $key, $this->toDelete )) !== false) {
89 unset ( $this->toDelete [$index] );
90 }
91 $this->dirty = true;
92 }
93
94 public function addValue($key, $value, $keyId = null) {
95 $this->toAdd [$key] = $value;
96 if (isset ( $keyId )) {
97 if (($id = array_search ( $keyId, $this->newKeys )) !== false) {
98 unset ( $this->toAdd [$id] );
99 unset ( $this->newKeys [$id] );
100 }
101 $this->newKeys [$key] = $keyId;
102 }
103
104 $this->dirty = true;
105 }
106
107 public function removeAddValue($key) {
108 if (isset ( $this->toAdd [$key] )) {
109 unset ( $this->toAdd [$key] );
110 }
111 }
112
113 public function replaceKey($key, $newKey, $value) {
114 $this->addToDelete ( $key );
115 $this->updateValue ( $newKey, $value );
116 }
117
118 public function removeNewKey($key) {
119 if (($k = array_search ( $key, $this->newKeys )) !== false) {
120 if (isset ( $this->toAdd [$k] )) {
121 unset ( $this->toAdd [$k] );
122 }
123 unset ( $this->newKeys [$k] );
124 $this->dirty = true;
125 }
126 }
127
128 public function save() {
129 if ($this->dirty) {
130 $key = $this->getKey ();
131 CacheManager::$cache->store ( $this->key . $key, [ 'newKeys' => $this->newKeys,'toAdd' => $this->toAdd,'toUpdate' => $this->toUpdate,'toDelete' => $this->toDelete ] );
132 $this->dirty = false;
133 return true;
134 }
135 return false;
136 }
137
138 public function delete() {
139 $key = $this->getKey ();
140 CacheManager::$cache->remove ( $this->key . $key );
141 }
142
143 public function isDirty() {
144 return $this->dirty;
145 }
146
147 public function __toString() {
148 $res = [ ];
149 if (($nb = \count ( $this->toAdd )) > 0) {
150 $res [] = '+' . $nb;
151 }
152 if (($nb = \count ( $this->toUpdate )) > 0) {
153 $res [] = '±' . $nb;
154 }
155 if (($nb = \count ( $this->toDelete )) > 0) {
156 $res [] = '-' . $nb;
157 }
158 return \implode ( ', ', $res );
159 }
160}
161
Manager for caches (Router, Rest, models).
mergeMessages($messages, $beforeSave=false)
addValue($key, $value, $keyId=null)