minor performance improvements

This commit is contained in:
Dennis Eichhorn 2018-12-30 19:49:55 +01:00
parent 29bee2e1f5
commit b04628f890
4 changed files with 105 additions and 71 deletions

View File

@ -329,12 +329,12 @@ class Account implements ArrayableInterface, \JsonSerializable
$app = $app !== null ? \strtolower($app) : $app;
foreach ($this->permissions as $p) {
if (($p->getUnit() === $unit || $p->getUnit() === null || $unit === null)
&& ($p->getApp() === $app || $p->getApp() === null || $app === null)
&& ($p->getModule() === $module || $p->getModule() === null || $module === null)
&& ($p->getType() === $type || $p->getType() === null || $type === null)
&& ($p->getElement() === $element || $p->getElement() === null || $element === null)
&& ($p->getComponent() === $component || $p->getComponent() === null || $component === null)
if (($unit === null || $p->getUnit() === $unit || $p->getUnit() === null)
&& ($app === null || $p->getApp() === $app || $p->getApp() === null)
&& ($module === null || $p->getModule() === $module || $p->getModule() === null)
&& ($type === null || $p->getType() === $type || $p->getType() === null)
&& ($element === null || $p->getElement() === $element || $p->getElement() === null)
&& ($component === null || $p->getComponent() === $component || $p->getComponent() === null)
&& ($p->getPermission() | $permission) === $p->getPermission()
) {
return true;

View File

@ -276,23 +276,18 @@ abstract class GrammarAbstract
if (\strrpos($system, $keyword, -\strlen($system)) !== false) {
$prefix = '';
$identifier = '';
break;
}
}
if (\count($split = \explode('.', $system)) > 1) {
$fullSystem = '';
$split = \explode('.', $system);
$fullSystem = '';
foreach ($split as $key => $system) {
if ($key === 0) {
$fullSystem .= $this->compileSystem($prefix . $system);
} else {
$fullSystem .= '.' . ($system === '*' ? '*' : $this->compileSystem($system));
}
}
return $fullSystem;
foreach ($split as $key => $system) {
$fullSystem .= '.' . $identifier . ($key === 0 ? $prefix : '') . $system . $identifier;
}
return $identifier . $prefix . $system . $identifier;
return \ltrim($fullSystem, '.');
}
}

View File

@ -15,7 +15,6 @@ declare(strict_types=1);
namespace phpOMS\Localization;
use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
use phpOMS\System\File\Local\Directory;
use phpOMS\Utils\Converter\AngleType;
use phpOMS\Utils\Converter\TemperatureType;
@ -140,15 +139,8 @@ final class Localization
/**
* Load localization from language code
*
* Files need to return a php array of the following structure:
* return [
* '{MODULE_NAME}' => [
* '{INTERNAL_STRING_REPRESENTATION}' => '{OUTPUT_STRING}',
* // more key/value pairs here
* ],
* ];
*
* @param string $langCode Language code
* @param string $langCode Language code
* @param string $countryCode Country code
*
* @return void
*
@ -156,28 +148,27 @@ final class Localization
*
* @since 1.0.0
*/
public function loadFromLanguage(string $langCode) : void
public function loadFromLanguage(string $langCode, string $countryCode = '*') : void
{
$langCode = \strtolower($langCode);
$langCode = \strtolower($langCode);
$countryCode = \strtoupper($countryCode);
if (!ISO639x1Enum::isValidValue($langCode)) {
throw new InvalidEnumValue($langCode);
}
$files = Directory::list(__DIR__ . '/../Localization/Defaults/Definitions');
$files = \glob(__DIR__ . '/../Localization/Defaults/Definitions/' . $langCode . '_' . $countryCode);
foreach ($files as $file) {
if (\stripos($file, $langCode) === 0) {
$fileContent = \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/' . $file);
$fileContent = \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/' . $file);
if ($fileContent === false) {
return;
}
$this->importLocale(\json_decode($fileContent, true));
return;
if ($fileContent === false) {
break;
}
$this->importLocale(\json_decode($fileContent, true));
return;
}
$fileContent = \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/en_US.json');

View File

@ -71,6 +71,22 @@ class View extends ViewAbstract
*/
protected $response = null;
/**
* Theme name.
*
* @var null|string
* @since 1.0.0
*/
protected $theme = null;
/**
* Module name.
*
* @var null|string
* @since 1.0.0
*/
protected $module = null;
/**
* Constructor.
*
@ -167,48 +183,80 @@ class View extends ViewAbstract
*
* @return string
*
* @throws InvalidModuleException Throws this exception if no data for the defined module could be found.
* @throws InvalidThemeException Throws this exception if no data for the defined theme could be found.
*
* @since 1.0.0
*/
public function getText($translation, string $module = null, string $theme = null) : string
{
if ($module === null) {
$match = '/Modules/';
if (($start = \strripos($this->template, $match)) === false) {
throw new InvalidModuleException($module ?? '');
}
$start = $start + \strlen($match);
$end = \strpos($this->template, '/', $start);
$module = \substr($this->template, $start, $end - $start);
if ($module === null && $this->module === null) {
$this->setModuleDynamically();
}
if ($module === false) {
$module = '0';
if ($theme === null && $this->theme === null) {
$this->setThemeDynamically();
}
if ($theme === null) {
$match = '/Theme/';
if (($start = \strripos($this->template, $match)) === false) {
throw new InvalidThemeException($theme ?? '');
}
$start = $start + \strlen($match);
$end = \strpos($this->template, '/', $start);
$theme = \substr($this->template, $start, $end - $start);
}
if ($theme === false) {
$theme = '0';
}
$module = $module ?? $this->module;
$theme = $theme ?? $this->theme;
return $this->app->l11nManager->getText($this->l11n->getLanguage(), $module, $theme, $translation);
}
/**
* Set the view module dynamically.
*
* Sets the view module based on the template path
*
* @return void
*
* @throws InvalidModuleException Throws this exception if no data for the defined module could be found.
*
* @since 1.0.0
*/
private function setModuleDynamically() : void
{
$match = '/Modules/';
if (($start = \strripos($this->template, $match)) === false) {
throw new InvalidModuleException('');
}
$start = $start + \strlen($match);
$end = \strpos($this->template, '/', $start);
$this->module = \substr($this->template, $start, $end - $start);
if ($this->module === false) {
$this->module = '0';
}
}
/**
* Set the view theme dynamically.
*
* Sets the view theme based on the template path
*
* @return void
*
* @throws InvalidThemeException Throws this exception if no data for the defined theme could be found.
*
* @since 1.0.0
*/
private function setThemeDynamically() : void
{
$match = '/Theme/';
if (($start = \strripos($this->template, $match)) === false) {
throw new InvalidThemeException('');
}
$start = $start + \strlen($match);
$end = \strpos($this->template, '/', $start);
$this->theme = \substr($this->template, $start, $end - $start);
if ($this->theme === false) {
$this->theme = '0';
}
}
/**
* Get translation.
*
@ -222,7 +270,7 @@ class View extends ViewAbstract
*/
public function getHtml($translation, string $module = null, string $theme = null) : string
{
return htmlspecialchars($this->getText($translation, $module, $theme));
return \htmlspecialchars($this->getText($translation, $module, $theme));
}
/**