Skip to content

Commit 17fbb72

Browse files
committed
Jet library (core):
------------------- * MVC: Layout postprocessor subsystem added * Translator::getKnownLocales() : array added * Translator::getKnownDictionaries( Locale $locale ) : array added * Translator_Dictionary::removePhrase added * Translator::saveDictionary( Translator_Dictionary $dictionary ): void added
1 parent fca1233 commit 17fbb72

File tree

8 files changed

+161
-12
lines changed

8 files changed

+161
-12
lines changed

library/Jet/AJAX.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class AJAX
1515
{
16-
16+
1717
/**
1818
* @param mixed $response_data (will be encoded by json_encode)
1919
* @param array $http_headers
@@ -24,31 +24,31 @@ public static function commonResponse( mixed $response_data, array $http_headers
2424
while (ob_get_level()) {
2525
ob_end_clean();
2626
}
27-
27+
2828
Debug::setOutputIsJSON( true );
29-
29+
3030
Http_Headers::response($http_code, $http_headers);
31-
31+
3232
echo json_encode( $response_data );
3333
Application::end();
34-
34+
3535
}
36-
36+
3737
/**
3838
* @param bool $success
3939
* @param array $snippets
4040
* @param array $data
4141
*/
4242
public static function operationResponse( bool $success, array $snippets = [], array $data = [] ): void
4343
{
44-
44+
4545
$response = [
4646
'result' => $success ? 'ok' : 'error',
4747
'data' => $data,
4848
'snippets' => $snippets
4949
];
50-
51-
50+
51+
5252
static::commonResponse( $response );
5353
}
5454

@@ -59,7 +59,7 @@ public static function snippetResponse( string $snippet ) : void
5959
}
6060

6161
echo $snippet;
62-
62+
6363
Application::end();
6464

6565
}

library/Jet/Http/Request/Trap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected function trap(): void
3131
* @return mixed
3232
*
3333
* @throws Http_Request_Exception
34+
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
3435
*/
3536
public function __get( string $name ): mixed
3637
{
@@ -84,6 +85,7 @@ public function rewind(): void
8485

8586
/**
8687
* @throws Http_Request_Exception
88+
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
8789
*/
8890
public function current(): mixed
8991
{
@@ -161,6 +163,7 @@ public function offsetUnset( mixed $offset ): void
161163
* @return mixed
162164
*
163165
* @throws Http_Request_Exception
166+
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
164167
*/
165168
public function offsetGet( mixed $offset ): mixed
166169
{

library/Jet/MVC/Layout.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class MVC_Layout extends MVC_View_Abstract
4242
* @var array
4343
*/
4444
protected array $virtual_positions = [];
45+
46+
/**
47+
* @var MVC_Layout_OutputPostprocessor[]
48+
*/
49+
protected array $postprocessors = [];
4550

4651

4752
/**
@@ -239,6 +244,8 @@ public function render(): string
239244

240245
$this->handleJavascripts( $result );
241246
$this->handleCss( $result );
247+
248+
$result = $this->performPostprocess( $result );
242249

243250
$this->output_parts = [];
244251

@@ -599,8 +606,32 @@ protected function handleCss( string &$result ): void
599606
}
600607

601608
$result = $this->_replaceTagByValue( $result, static::TAG_CSS, $snippet );
602-
603-
609+
610+
}
611+
612+
public function addOutputPostprocessor( MVC_Layout_OutputPostprocessor $postprocessor ) : void
613+
{
614+
$this->postprocessors[$postprocessor->getId()] = $postprocessor;
615+
}
616+
617+
public function getPostprocessor( string $id ) : ?MVC_Layout_OutputPostprocessor
618+
{
619+
return $this->postprocessors[$id]??null;
620+
}
621+
622+
public function removePostprocessor( string $id ) : void
623+
{
624+
if(isset($this->postprocessors[$id])) {
625+
unset( $this->postprocessors[$id] );
626+
}
604627
}
605628

629+
public function performPostprocess( $output ) : string
630+
{
631+
foreach($this->postprocessors as $postprocessor) {
632+
$output = $postprocessor->process( $output );
633+
}
634+
635+
return $output;
636+
}
606637
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
*
4+
* @copyright Copyright (c) Miroslav Marek <mirek.marek@web-jet.cz>
5+
* @license http://www.php-jet.net/license/license.txt
6+
* @author Miroslav Marek <mirek.marek@web-jet.cz>
7+
*/
8+
namespace Jet;
9+
10+
abstract class MVC_Layout_OutputPostprocessor {
11+
12+
protected MVC_Layout $layout;
13+
protected string $output;
14+
15+
/**
16+
* @param MVC_Layout $layout
17+
*/
18+
public function __construct( MVC_Layout $layout )
19+
{
20+
$this->layout = $layout;
21+
}
22+
23+
abstract public function getId() : string;
24+
25+
/**
26+
* @param string $output
27+
* @return string
28+
*/
29+
abstract public function process( string $output ) : string;
30+
}

library/Jet/Translator.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ public static function saveUpdatedDictionaries(): void
141141
}
142142
}
143143
}
144+
145+
/**
146+
*
147+
*/
148+
public static function saveDictionary( Translator_Dictionary $dictionary ): void
149+
{
150+
static::getBackend()->saveDictionary( $dictionary );
151+
}
152+
144153

145154
/**
146155
* Gets translation of given text
@@ -242,4 +251,18 @@ public static function uninstallApplicationModuleDictionaries( Application_Modul
242251
static::getBackend()->uninstallApplicationModuleDictionaries( $module );
243252
}
244253

254+
/**
255+
* @return Locale[]
256+
*/
257+
public static function getKnownLocales() : array
258+
{
259+
return static::getBackend()->getKnownLocales();
260+
}
261+
262+
public static function getKnownDictionaries( Locale $locale ) : array
263+
{
264+
return static::getBackend()->getKnownDictionaries( $locale );
265+
}
266+
267+
245268
}

library/Jet/Translator/Backend.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public function generateHash( string $phrase ): string
7070
return md5( $phrase );
7171
}
7272

73+
/**
74+
* @return Locale[]
75+
*/
76+
abstract public function getKnownLocales() : array;
77+
78+
abstract public function getKnownDictionaries( Locale $locale ) : array;
79+
7380
abstract public function installApplicationModuleDictionaries( Application_Module_Manifest $module ) : void;
7481

7582
abstract public function collectApplicationModuleDictionaries( Application_Module_Manifest $module ) : void;

library/Jet/Translator/Backend/Default.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,44 @@ public function uninstallApplicationModuleDictionaries( Application_Module_Manif
133133
}
134134
}
135135

136+
/**
137+
* @return Locale[]
138+
*/
139+
public function getKnownLocales() : array
140+
{
141+
$res = [];
142+
143+
$dirs = IO_Dir::getSubdirectoriesList( SysConf_Path::getDictionaries() );
144+
145+
foreach($dirs as $path=>$name) {
146+
$locale = new Locale($name);
147+
if(
148+
$locale->getRegion() &&
149+
$locale->getLanguage()
150+
) {
151+
$res[$locale->toString()] = $locale;
152+
}
153+
154+
}
155+
156+
return $res;
157+
}
158+
159+
public function getKnownDictionaries( Locale $locale ) : array
160+
{
161+
$res = [];
162+
163+
$dir = SysConf_Path::getDictionaries() . $locale . '/';
164+
165+
166+
$files = IO_Dir::getFilesList($dir, '*.php');
167+
foreach($files as $path=>$name) {
168+
$name = substr($name, 0, -4);
169+
$res[$name] = $name;
170+
}
171+
172+
return $res;
173+
}
174+
136175

137176
}

library/Jet/Translator/Dictionary.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ public function addPhrase( Translator_Dictionary_Phrase $phrase, bool $save_requ
118118
$this->save_required = true;
119119
}
120120
}
121+
122+
/**
123+
* @param Translator_Dictionary_Phrase $phrase
124+
* @param bool $save_required
125+
*
126+
*/
127+
public function removePhrase( Translator_Dictionary_Phrase $phrase, bool $save_required = true ): void
128+
{
129+
if(isset($this->phrases[$phrase->getHash()])) {
130+
unset($this->phrases[$phrase->getHash()]);
131+
}
132+
133+
if( $save_required ) {
134+
$this->save_required = true;
135+
}
136+
}
121137

122138
/**
123139
* @return bool

0 commit comments

Comments
 (0)