Skip to content

Commit 1131791

Browse files
committed
Update 1.0.2
1 parent 5b5cddb commit 1131791

17 files changed

+354
-217
lines changed

composer.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "floatphp/kernel",
3-
"version" : "1.0.1",
3+
"version" : "1.0.2",
44
"type": "library",
55
"description": "FloatPHP Kernel Components",
66
"keywords": ["php","micro-framework","framework","PSR","ORM","jakiboy","composer"],
@@ -22,13 +22,13 @@
2222
"php": ">=7.4",
2323
"ext-curl": "*",
2424
"ext-intl": "*",
25-
"twig/twig": "^3.5.0",
25+
"twig/twig": "^3.5.1",
2626
"justinrainbow/json-schema": "^5.2.12",
27-
"floatphp/classes": "^1.0.1",
28-
"floatphp/helpers": "^1.0.1",
29-
"floatphp/interfaces": "^1.0.1",
30-
"floatphp/exceptions": "^1.0.1",
31-
"floatphp/cli": "^1.0.1"
27+
"floatphp/classes": "^1.0.2",
28+
"floatphp/helpers": "^1.0.2",
29+
"floatphp/interfaces": "^1.0.2",
30+
"floatphp/exceptions": "^1.0.2",
31+
"floatphp/cli": "^1.0.2"
3232
},
3333
"autoload": {
3434
"psr-4" : {

src/AbstractAuthController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author : JIHAD SINNAOUR
44
* @package : FloatPHP
55
* @subpackage : Kernel Component
6-
* @version : 1.0.1
6+
* @version : 1.0.2
77
* @category : PHP framework
88
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <[email protected]>
99
* @link : https://www.floatphp.com

src/ApiController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author : JIHAD SINNAOUR
44
* @package : FloatPHP
55
* @subpackage : Kernel Component
6-
* @version : 1.0.1
6+
* @version : 1.0.2
77
* @category : PHP framework
88
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <[email protected]>
99
* @link : https://www.floatphp.com
@@ -78,7 +78,7 @@ public function isHttpAuthenticated() : bool
7878
*/
7979
protected function isGranted($token) : bool
8080
{
81-
$encryption = new Encryption($token,$this->getSecret(true));
81+
$encryption = new Encryption($token, $this->getSecret(true));
8282
$access = $encryption->decrypt();
8383
$pattern = '/{user:(.*?)}{pswd:(.*?)}/';
8484
$username = Stringify::match($pattern,$access,1);

src/BackendController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author : JIHAD SINNAOUR
44
* @package : FloatPHP
55
* @subpackage : Kernel Component
6-
* @version : 1.0.1
6+
* @version : 1.0.2
77
* @category : PHP framework
88
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <[email protected]>
99
* @link : https://www.floatphp.com

src/Base.php

+100-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author : JIHAD SINNAOUR
44
* @package : FloatPHP
55
* @subpackage : Kernel Component
6-
* @version : 1.0.1
6+
* @version : 1.0.2
77
* @category : PHP framework
88
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <[email protected]>
99
* @link : https://www.floatphp.com
@@ -17,9 +17,15 @@
1717
namespace FloatPHP\Kernel;
1818

1919
use FloatPHP\Classes\{
20-
Filesystem\Arrayify, Filesystem\Stringify, Filesystem\Translation,
21-
Html\Hook, Html\Shortcode,
22-
Http\Session, Http\Server, Http\Request,
20+
Filesystem\Arrayify,
21+
Filesystem\Stringify,
22+
Filesystem\TypeCheck,
23+
Filesystem\Translation,
24+
Html\Hook,
25+
Html\Shortcode,
26+
Http\Session,
27+
Http\Server,
28+
Http\Request,
2329
Security\Tokenizer
2430
};
2531
use FloatPHP\Helpers\Filesystem\{
@@ -201,54 +207,130 @@ protected function getLanguage()
201207
} else {
202208
$lang = Session::get('--default-lang');
203209
}
204-
return $this->applyFilter('--default-lang',$lang);
210+
return $this->applyFilter('--default-lang', $lang);
205211
}
206212

207213
/**
214+
* Translate string,
215+
* May require quotes escaping.
216+
*
208217
* @access protected
209218
* @param string $string
210219
* @return string
211220
*/
212221
protected function translate($string = '') : string
213222
{
214-
// Set cache filters
215-
$path = $this->applyFilter('translation-cache-path','translate');
216-
$ttl = $this->applyFilter('translation-cache-ttl',3600);
217-
218223
// Cache translation
219-
$cache = new Cache($path,$ttl);
224+
$cache = new Cache();
220225

221226
// Translation cache id
222227
$length = strlen($string);
223228
$lang = $this->getLanguage();
224-
$uppercases = Stringify::matchAll('/([A-Z])/',$string);
229+
$uppercases = Stringify::matchAll('/([A-Z])/', $string);
225230
$translateId = '';
231+
226232
foreach ($uppercases as $uppercase) {
227-
$translateId = Stringify::replace($uppercase,"{$uppercase}-1",$string);
233+
$translateId = Stringify::replace($uppercase, "{$uppercase}-1", $string);
228234
}
235+
229236
if ( empty($translateId) ) {
230237
$translateId = $string;
231238
}
239+
232240
$translateId = Stringify::slugify("translation-{$lang}-{$length}-{$translateId}");
233241
$translation = $cache->get($translateId);
242+
234243
if ( !$cache->isCached() ) {
235-
$path = $this->applyFilter('translation-path',$this->getTranslatePath());
236-
$translator = new Translation($lang,$path);
244+
$path = $this->applyFilter('translation-path', $this->getTranslatePath());
245+
$translator = new Translation($lang, $path);
237246
$translation = $translator->translate($string);
238-
$cache->set($translation,'translation');
247+
$cache->set($translation, 'translation');
239248
}
249+
240250
return ($translation) ? $translation : (string)$string;
241251
}
242252

243253
/**
254+
* Translate array of strings.
255+
*
256+
* @access protected
257+
* @param array $strings
258+
* @return array
259+
*/
260+
protected function translateArray(array $strings = []) : array
261+
{
262+
foreach ($strings as $key => $value) {
263+
$strings[$key] = $this->translate($value);
264+
}
265+
return $strings;
266+
}
267+
268+
/**
269+
* Translate string including variables,
270+
* May require quotes escaping.
271+
*
244272
* @access protected
245273
* @param string $string
246-
* @param string $vars
274+
* @param mixed $vars
247275
* @return string
248276
*/
249-
protected function translateVars($string,...$vars) : string
277+
protected function translateVars(string $string, $vars = null) : string
278+
{
279+
if ( TypeCheck::isArray($vars) ) {
280+
return vsprintf(
281+
$this->translate($string),
282+
$vars
283+
);
284+
285+
} else {
286+
$vars = (string)$vars;
287+
return sprintf($this->translate(
288+
Stringify::replace($vars, '%s', $string)
289+
), $vars);
290+
}
291+
}
292+
293+
/**
294+
* Translate deep strings.
295+
*
296+
* @access protected
297+
* @param array $strings
298+
* @return array
299+
*/
300+
protected function translateDeepStrings(array $strings)
250301
{
251-
return sprintf($this->translate($string),$vars);
302+
Arrayify::walkRecursive($strings, function(&$string) {
303+
if ( TypeCheck::isString($string) ) {
304+
$string = $this->translate($string);
305+
}
306+
});
307+
return $strings;
308+
}
309+
310+
/**
311+
* Load translated strings (admin/front).
312+
*
313+
* @access protected
314+
* @param string $type
315+
* @return array
316+
*/
317+
protected function loadStrings($type = 'admin')
318+
{
319+
$strings = $this->applyFilter('strings', $this->getStrings());
320+
switch ($type) {
321+
case 'admin':
322+
return $this->translateDeepStrings(
323+
$strings['admin']
324+
);
325+
break;
326+
327+
case 'front':
328+
return $this->translateDeepStrings(
329+
$strings['front']
330+
);
331+
break;
332+
}
333+
return $this->translateDeepStrings($strings);
252334
}
253335

254336
/**

src/BaseController.php

+26-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author : JIHAD SINNAOUR
44
* @package : FloatPHP
55
* @subpackage : Kernel Component
6-
* @version : 1.0.1
6+
* @version : 1.0.2
77
* @category : PHP framework
88
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <[email protected]>
99
* @link : https://www.floatphp.com
@@ -49,33 +49,35 @@ public function hasAccess() : bool
4949
$access = false;
5050

5151
// Allow local access
52-
if ( Stringify::contains(['127.0.0.1','::1'],$ip) ) {
52+
if ( Stringify::contains(['127.0.0.1','::1'], $ip) ) {
5353
$access = true;
5454

5555
} else {
5656

5757
// Check allowed IPs
58-
$allowed = $this->applyFilter('access-allowed-ip',$this->getAllowedAccess());
58+
$allowed = $this->applyFilter('access-allowed-ip', $this->getAllowedAccess());
5959
if ( !empty($allowed) ) {
60-
if ( Stringify::contains($allowed,$ip) ) {
60+
if ( Stringify::contains($allowed, $ip) ) {
6161
$access = true;
62+
6263
} else {
6364
$access = false;
6465
}
6566

6667
} else {
6768
// Deny access
68-
$denied = $this->applyFilter('access-denied-ip',$this->getDeniedAccess());
69-
if ( Stringify::contains($denied,$ip) ) {
69+
$denied = $this->applyFilter('access-denied-ip', $this->getDeniedAccess());
70+
if ( Stringify::contains($denied, $ip) ) {
7071
$access = false;
72+
7173
} else {
7274
$access = true;
7375
}
7476
}
7577
}
7678

77-
$data = ['ip' => $ip,'access' => $access];
78-
$this->doAction('ip-access',$data);
79+
$data = ['ip' => $ip, 'access' => $access];
80+
$this->doAction('ip-access', $data);
7981
return $access;
8082
}
8183

@@ -88,8 +90,8 @@ public function hasAccess() : bool
8890
protected function addJS($js, $hook = 'add-js')
8991
{
9092
$this->addAction($hook, function() use($js) {
91-
$tpl = $this->applyFilter('view-js','system/js');
92-
$this->render(['js' => $js],$tpl);
93+
$tpl = $this->applyFilter('view-js', 'system/js');
94+
$this->render(['js' => $js], $tpl);
9395
});
9496
}
9597

@@ -102,8 +104,8 @@ protected function addJS($js, $hook = 'add-js')
102104
protected function addCSS($css, $hook = 'add-css')
103105
{
104106
$this->addAction($hook, function() use($css){
105-
$tpl = $this->applyFilter('view-css','system/css');
106-
$this->render(['css' => $css],$tpl);
107+
$tpl = $this->applyFilter('view-css', 'system/css');
108+
$this->render(['css' => $css], $tpl);
107109
});
108110
}
109111

@@ -116,7 +118,7 @@ protected function addCSS($css, $hook = 'add-css')
116118
*/
117119
public function redirect($url = '/', $code = 301, $message = 'Moved Permanently')
118120
{
119-
Server::redirect($url,$code,$message);
121+
Server::redirect($url, $code, $message);
120122
}
121123

122124
/**
@@ -133,7 +135,7 @@ protected function verifyToken($token = '', $source = '')
133135
$data = Stringify::unserialize($transient->getTemp($token));
134136

135137
// Override
136-
$this->doAction('verify-token',$data);
138+
$this->doAction('verify-token', $data);
137139

138140
// Verify token data
139141
if ( $data ) {
@@ -167,30 +169,30 @@ protected function verifyToken($token = '', $source = '')
167169
*/
168170
protected function verifyRequest($force = false)
169171
{
170-
$token = $this->applyFilter('verify-request-token','--token');
171-
$source = $this->applyFilter('verify-request-source','--source');
172-
$ignore = $this->applyFilter('verify-request-ignore','--ignore');
172+
$token = $this->applyFilter('verify-request-token', '--token');
173+
$source = $this->applyFilter('verify-request-source', '--source');
174+
$ignore = $this->applyFilter('verify-request-ignore', '--ignore');
173175

174176
if ( $force ) {
175177
if ( !Request::isSetted($token) ) {
176-
$msg = $this->applyFilter('invalid-request-signature','Invalid request signature');
178+
$msg = $this->applyFilter('invalid-request-signature', 'Invalid request signature');
177179
$msg = $this->translate($msg);
178-
$this->setResponse($msg,[],'error',401);
180+
$this->setResponse($msg, [], 'error', 401);
179181
}
180182
}
181183

182184
if ( Request::isSetted($token) ) {
183185
$source = Request::isSetted($source) ? Request::get($source) : '';
184186
if ( !$this->verifyToken(Request::get($token),$source) ) {
185-
$msg = $this->applyFilter('invalid-request-token','Invalid request token');
187+
$msg = $this->applyFilter('invalid-request-token', 'Invalid request token');
186188
$msg = $this->translate($msg);
187-
$this->setResponse($msg,[],'error',401);
189+
$this->setResponse($msg, [], 'error', 401);
188190
}
189191
}
190192
if ( Request::isSetted($ignore) && !empty(Request::get($ignore)) ) {
191-
$msg = $this->applyFilter('invalid-request-data','Invalid request data');
193+
$msg = $this->applyFilter('invalid-request-data', 'Invalid request data');
192194
$msg = $this->translate($msg);
193-
$this->setResponse($msg,[],'error',401);
195+
$this->setResponse($msg, [], 'error', 401);
194196
}
195197
}
196198

@@ -205,7 +207,7 @@ protected function sanitizeRequest($verify = true, $force = false)
205207
$this->verifyRequest($force);
206208
}
207209
$request = Request::get();
208-
$excepts = $this->applyFilter('sanitize-request',[
210+
$excepts = $this->applyFilter('sanitize-request', [
209211
'submit',
210212
'--token',
211213
'--source',

0 commit comments

Comments
 (0)