Skip to content

Commit

Permalink
Jet library (core):
Browse files Browse the repository at this point in the history
-------------------
* 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
  • Loading branch information
mirekmarek committed Mar 22, 2024
1 parent fca1233 commit 17fbb72
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 12 deletions.
20 changes: 10 additions & 10 deletions library/Jet/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class AJAX
{

/**
* @param mixed $response_data (will be encoded by json_encode)
* @param array $http_headers
Expand All @@ -24,31 +24,31 @@ public static function commonResponse( mixed $response_data, array $http_headers
while (ob_get_level()) {
ob_end_clean();
}

Debug::setOutputIsJSON( true );

Http_Headers::response($http_code, $http_headers);

echo json_encode( $response_data );
Application::end();

}

/**
* @param bool $success
* @param array $snippets
* @param array $data
*/
public static function operationResponse( bool $success, array $snippets = [], array $data = [] ): void
{

$response = [
'result' => $success ? 'ok' : 'error',
'data' => $data,
'snippets' => $snippets
];


static::commonResponse( $response );
}

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

echo $snippet;

Application::end();

}
Expand Down
3 changes: 3 additions & 0 deletions library/Jet/Http/Request/Trap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected function trap(): void
* @return mixed
*
* @throws Http_Request_Exception
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
public function __get( string $name ): mixed
{
Expand Down Expand Up @@ -84,6 +85,7 @@ public function rewind(): void

/**
* @throws Http_Request_Exception
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
public function current(): mixed
{
Expand Down Expand Up @@ -161,6 +163,7 @@ public function offsetUnset( mixed $offset ): void
* @return mixed
*
* @throws Http_Request_Exception
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
public function offsetGet( mixed $offset ): mixed
{
Expand Down
35 changes: 33 additions & 2 deletions library/Jet/MVC/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class MVC_Layout extends MVC_View_Abstract
* @var array
*/
protected array $virtual_positions = [];

/**
* @var MVC_Layout_OutputPostprocessor[]
*/
protected array $postprocessors = [];


/**
Expand Down Expand Up @@ -239,6 +244,8 @@ public function render(): string

$this->handleJavascripts( $result );
$this->handleCss( $result );

$result = $this->performPostprocess( $result );

$this->output_parts = [];

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

$result = $this->_replaceTagByValue( $result, static::TAG_CSS, $snippet );



}

public function addOutputPostprocessor( MVC_Layout_OutputPostprocessor $postprocessor ) : void
{
$this->postprocessors[$postprocessor->getId()] = $postprocessor;
}

public function getPostprocessor( string $id ) : ?MVC_Layout_OutputPostprocessor
{
return $this->postprocessors[$id]??null;
}

public function removePostprocessor( string $id ) : void
{
if(isset($this->postprocessors[$id])) {
unset( $this->postprocessors[$id] );
}
}

public function performPostprocess( $output ) : string
{
foreach($this->postprocessors as $postprocessor) {
$output = $postprocessor->process( $output );
}

return $output;
}
}
30 changes: 30 additions & 0 deletions library/Jet/MVC/Layout/OutputPostprocessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
*
* @copyright Copyright (c) Miroslav Marek <[email protected]>
* @license http://www.php-jet.net/license/license.txt
* @author Miroslav Marek <[email protected]>
*/
namespace Jet;

abstract class MVC_Layout_OutputPostprocessor {

protected MVC_Layout $layout;
protected string $output;

/**
* @param MVC_Layout $layout
*/
public function __construct( MVC_Layout $layout )
{
$this->layout = $layout;
}

abstract public function getId() : string;

/**
* @param string $output
* @return string
*/
abstract public function process( string $output ) : string;
}
23 changes: 23 additions & 0 deletions library/Jet/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public static function saveUpdatedDictionaries(): void
}
}
}

/**
*
*/
public static function saveDictionary( Translator_Dictionary $dictionary ): void
{
static::getBackend()->saveDictionary( $dictionary );
}


/**
* Gets translation of given text
Expand Down Expand Up @@ -242,4 +251,18 @@ public static function uninstallApplicationModuleDictionaries( Application_Modul
static::getBackend()->uninstallApplicationModuleDictionaries( $module );
}

/**
* @return Locale[]
*/
public static function getKnownLocales() : array
{
return static::getBackend()->getKnownLocales();
}

public static function getKnownDictionaries( Locale $locale ) : array
{
return static::getBackend()->getKnownDictionaries( $locale );
}


}
7 changes: 7 additions & 0 deletions library/Jet/Translator/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function generateHash( string $phrase ): string
return md5( $phrase );
}

/**
* @return Locale[]
*/
abstract public function getKnownLocales() : array;

abstract public function getKnownDictionaries( Locale $locale ) : array;

abstract public function installApplicationModuleDictionaries( Application_Module_Manifest $module ) : void;

abstract public function collectApplicationModuleDictionaries( Application_Module_Manifest $module ) : void;
Expand Down
39 changes: 39 additions & 0 deletions library/Jet/Translator/Backend/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,44 @@ public function uninstallApplicationModuleDictionaries( Application_Module_Manif
}
}

/**
* @return Locale[]
*/
public function getKnownLocales() : array
{
$res = [];

$dirs = IO_Dir::getSubdirectoriesList( SysConf_Path::getDictionaries() );

foreach($dirs as $path=>$name) {
$locale = new Locale($name);
if(
$locale->getRegion() &&
$locale->getLanguage()
) {
$res[$locale->toString()] = $locale;
}

}

return $res;
}

public function getKnownDictionaries( Locale $locale ) : array
{
$res = [];

$dir = SysConf_Path::getDictionaries() . $locale . '/';


$files = IO_Dir::getFilesList($dir, '*.php');
foreach($files as $path=>$name) {
$name = substr($name, 0, -4);
$res[$name] = $name;
}

return $res;
}


}
16 changes: 16 additions & 0 deletions library/Jet/Translator/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ public function addPhrase( Translator_Dictionary_Phrase $phrase, bool $save_requ
$this->save_required = true;
}
}

/**
* @param Translator_Dictionary_Phrase $phrase
* @param bool $save_required
*
*/
public function removePhrase( Translator_Dictionary_Phrase $phrase, bool $save_required = true ): void
{
if(isset($this->phrases[$phrase->getHash()])) {
unset($this->phrases[$phrase->getHash()]);
}

if( $save_required ) {
$this->save_required = true;
}
}

/**
* @return bool
Expand Down

0 comments on commit 17fbb72

Please sign in to comment.