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; $app = $app !== null ? \strtolower($app) : $app;
foreach ($this->permissions as $p) { foreach ($this->permissions as $p) {
if (($p->getUnit() === $unit || $p->getUnit() === null || $unit === null) if (($unit === null || $p->getUnit() === $unit || $p->getUnit() === null)
&& ($p->getApp() === $app || $p->getApp() === null || $app === null) && ($app === null || $p->getApp() === $app || $p->getApp() === null)
&& ($p->getModule() === $module || $p->getModule() === null || $module === null) && ($module === null || $p->getModule() === $module || $p->getModule() === null)
&& ($p->getType() === $type || $p->getType() === null || $type === null) && ($type === null || $p->getType() === $type || $p->getType() === null)
&& ($p->getElement() === $element || $p->getElement() === null || $element === null) && ($element === null || $p->getElement() === $element || $p->getElement() === null)
&& ($p->getComponent() === $component || $p->getComponent() === null || $component === null) && ($component === null || $p->getComponent() === $component || $p->getComponent() === null)
&& ($p->getPermission() | $permission) === $p->getPermission() && ($p->getPermission() | $permission) === $p->getPermission()
) { ) {
return true; return true;

View File

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

View File

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

View File

@ -71,6 +71,22 @@ class View extends ViewAbstract
*/ */
protected $response = null; 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. * Constructor.
* *
@ -167,48 +183,80 @@ class View extends ViewAbstract
* *
* @return string * @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 * @since 1.0.0
*/ */
public function getText($translation, string $module = null, string $theme = null) : string public function getText($translation, string $module = null, string $theme = null) : string
{ {
if ($module === null) { if ($module === null && $this->module === null) {
$match = '/Modules/'; $this->setModuleDynamically();
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 === false) { if ($theme === null && $this->theme === null) {
$module = '0'; $this->setThemeDynamically();
} }
if ($theme === null) { $module = $module ?? $this->module;
$match = '/Theme/'; $theme = $theme ?? $this->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';
}
return $this->app->l11nManager->getText($this->l11n->getLanguage(), $module, $theme, $translation); 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. * Get translation.
* *
@ -222,7 +270,7 @@ class View extends ViewAbstract
*/ */
public function getHtml($translation, string $module = null, string $theme = null) : string 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));
} }
/** /**