From 6262755a4f9953149f08f9245f29f58ae33aa4f8 Mon Sep 17 00:00:00 2001 From: MartinSzollos2016 Date: Wed, 23 Mar 2022 09:51:55 +0100 Subject: [PATCH] Remove dependency to baraja-core/localization (#10) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove localization dependecy * php-stan lvl 8, BasicPanel to BasicLocalePanel, baraja/localization in require-dev * ECS, code-checker * Code review update * Suggest typo * Fix Direct use of $_SERVER Superglobal detected. * Use filter_input * Server in constructor * use filter input * localization passed by setter not constructor * Fix Avoid using static access to class '\Nette\Schema\Expect' in method 'getConfigSchema'. * use FILTER_SANITIZE_STRING * Code review update * Add Localitzation condition * Set default by setter only * Update src/Helpers.php * Apply suggestions from code review * BasicPanel: Fix codereview. Co-authored-by: Martin Szollos Co-authored-by: Jan Barášek --- README.md | 2 ++ composer.json | 7 +++++-- src/AdminBar.php | 5 +++-- src/AdminBarExtension.php | 24 ++++++++++++++++++++++-- src/Bar.php | 2 +- src/Helpers.php | 9 ++++++--- src/Menu/Menu.php | 2 +- src/Menu/MenuLink.php | 3 ++- src/Panel/BasicPanel.php | 23 ++++++++++++++++++----- src/User/AdminIdentity.php | 2 +- src/assets/style.css | 3 +++ 11 files changed, 64 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b16e0cb..4cea9de 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ $ composer require baraja-core/admin-bar You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework. +To use the AdminBarExtension, implement AdminIdentity.php into your project. + AdminBar will adapt to what you are doing ----------------------------------------- diff --git a/composer.json b/composer.json index 1c21716..da5bc4e 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,6 @@ "nette/utils": "^3.0", "nette/security": "^3.0", "nette/di": "^3.0", - "baraja-core/localization": "^2.0", "baraja-core/url": "^1.1" }, "require-dev": { @@ -24,7 +23,11 @@ "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-strict-rules": "^1.0", "spaze/phpstan-disallowed-calls": "^2.0", - "roave/security-advisories": "dev-master" + "roave/security-advisories": "dev-master", + "baraja-core/localization": "^2.0" + }, + "suggest": { + "baraja-core/localization": "Global localization resolver by current context." }, "autoload": { "classmap": [ diff --git a/src/AdminBar.php b/src/AdminBar.php index dd908b9..5e4fc51 100644 --- a/src/AdminBar.php +++ b/src/AdminBar.php @@ -36,11 +36,12 @@ final class AdminBar */ public static function enable(?bool $enabled = self::MODE_AUTODETECT): void { + $httpXRequestedWith = filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH', FILTER_UNSAFE_RAW); if ( PHP_SAPI === 'cli' // cli mode || ( // ajax request - isset($_SERVER['HTTP_X_REQUESTED_WITH']) - && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' + is_string($httpXRequestedWith) + && strtolower($httpXRequestedWith) === 'xmlhttprequest' ) || ( // api request class_exists(Url::class) diff --git a/src/AdminBarExtension.php b/src/AdminBarExtension.php index 64beff2..ae7b131 100644 --- a/src/AdminBarExtension.php +++ b/src/AdminBarExtension.php @@ -4,22 +4,42 @@ namespace Baraja\AdminBar; - use Baraja\AdminBar\Panel\BasicPanel; +use Baraja\Localization\Localization; use Nette\DI\CompilerExtension; use Nette\DI\Definitions\ServiceDefinition; use Nette\PhpGenerator\ClassType; +use Nette\Schema\Elements\Structure; +use Nette\Schema\Elements\Type; +use Nette\Schema\Schema; use Nette\Security\User; +use stdClass; final class AdminBarExtension extends CompilerExtension { + public function getConfigSchema(): Schema + { + return new Structure( + ['defaultLocale' => (new Type('string'))->default(null)], + ); + } + + public function beforeCompile(): void { + /** @var stdClass{defaultLocale: string} $config */ + $config = $this->config; + $builder = $this->getContainerBuilder(); - $builder->addDefinition($this->prefix('basicPanel')) + $basicPanel = $builder->addDefinition($this->prefix('basicPanel')) ->setFactory(BasicPanel::class) ->setAutowired(BasicPanel::class); + $defaultLocale = $config->defaultLocale; + + if ($defaultLocale !== null && !class_exists(Localization::class)) { + $basicPanel->addSetup('setDefaultLocale', [$defaultLocale]); + } } diff --git a/src/Bar.php b/src/Bar.php index cfde04b..3f0dc9c 100644 --- a/src/Bar.php +++ b/src/Bar.php @@ -41,7 +41,7 @@ public function addPanel(Panel $panel, ?string $id = null): self if ($id === null) { $c = 0; do { - $id = $panel::class . ($c++ ? '-' . $c : ''); + $id = $panel::class . ($c++ > 0 ? '-' . $c : ''); } while (isset($this->panels[$id])); } $this->panels[$id] = $panel; diff --git a/src/Helpers.php b/src/Helpers.php index a0d4c8d..6d1acb8 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -15,9 +15,10 @@ public static function escapeHtml(string $s): string public static function isHtmlMode(): bool { - return empty($_SERVER['HTTP_X_REQUESTED_WITH']) && empty($_SERVER['HTTP_X_TRACY_AJAX']) + return ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === '' + && ($_SERVER['HTTP_X_TRACY_AJAX'] ?? '') === '' && PHP_SAPI !== 'cli' - && !preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list())); + && preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list())) !== 1; } @@ -25,7 +26,9 @@ public static function minifyHtml(string $haystack): string { return (string) preg_replace_callback( '#[ \t\r\n]+|<(/)?(textarea|pre)(?=\W)#i', - static fn(array $match) => empty($match[2]) ? ' ' : $match[0], + static fn(array $match) => !isset($match[2]) || $match[2] === '' + ? ' ' + : $match[0], $haystack, ); } diff --git a/src/Menu/Menu.php b/src/Menu/Menu.php index dd57230..86bbdaa 100644 --- a/src/Menu/Menu.php +++ b/src/Menu/Menu.php @@ -50,7 +50,7 @@ public function getItems(): array private function registerGroup(?string $group): string { - $group = $group ?: 'default'; + $group = $group !== null && $group !== '' ? $group : 'default'; if (isset($this->items[$group]) === false) { $this->items[$group] = []; } diff --git a/src/Menu/MenuLink.php b/src/Menu/MenuLink.php index 6cb2557..d3d74af 100644 --- a/src/Menu/MenuLink.php +++ b/src/Menu/MenuLink.php @@ -21,7 +21,8 @@ public function __construct(string $label, string $url) $url = trim((string) preg_replace('/\s+/', '', $url)); if (Validators::isUrl($url) === false) { throw new \InvalidArgumentException( - sprintf('URL is not valid, because "%s" (length: %d bytes, base64: "%s") given.', + sprintf( + 'URL is not valid, because "%s" (length: %d bytes, base64: "%s") given.', $url, strlen($url), base64_encode($url), diff --git a/src/Panel/BasicPanel.php b/src/Panel/BasicPanel.php index cde474e..06d1d35 100644 --- a/src/Panel/BasicPanel.php +++ b/src/Panel/BasicPanel.php @@ -12,23 +12,36 @@ final class BasicPanel implements Panel { + private ?string $defaultLocale = null; + + public function __construct( - private Localization $localization, private User $user, + private ?Localization $localization = null, ) { } + public function setDefaultLocale(string $locale): void + { + $this->defaultLocale = $locale; + } + + public function getTab(): string { - $default = $this->localization->getDefaultLocale(); - $current = $this->localization->getLocale() ?? $default; $baseUrl = Url::get()->getBaseUrl(); - $localeParam = $default !== $current ? '?locale=' . $current : ''; + if ($this->localization !== null) { + $default = $this->localization->getDefaultLocale(); + $current = $this->localization->getLocale(); + $localeParam = $default !== $current ? sprintf('?locale=%s', urlencode($current)) : ''; + } else { + $localeParam = $this->defaultLocale !== null ? sprintf('?locale=%s', urlencode($this->defaultLocale)) : ''; + } $buttons = []; $buttons[] = sprintf('Home', $baseUrl . $localeParam); - $buttons[] = sprintf('Admin', $baseUrl . '/admin' . $localeParam); + $buttons[] = sprintf('Admin', sprintf('%s/admin%s', $baseUrl, $localeParam)); $apiDoc = $this->processApiDocumentation($baseUrl); if ($apiDoc !== null) { diff --git a/src/User/AdminIdentity.php b/src/User/AdminIdentity.php index 91e8a3a..2feb2c9 100644 --- a/src/User/AdminIdentity.php +++ b/src/User/AdminIdentity.php @@ -8,7 +8,7 @@ use Nette\Security\SimpleIdentity; use Nette\Utils\Validators; -final class AdminIdentity extends SimpleIdentity +class AdminIdentity extends SimpleIdentity { private ?string $name; diff --git a/src/assets/style.css b/src/assets/style.css index 712eeec..0b11510 100644 --- a/src/assets/style.css +++ b/src/assets/style.css @@ -20,6 +20,8 @@ body, #tracy-bs { will-change: opacity, top, left; line-height: 1 !important; color: black !important; + background: #1b1f23; + border: 0 !important; } .admin-bar-container-debugMode { @@ -79,6 +81,7 @@ body, #tracy-bs { #admin-bar-container table tr td { padding: 0 !important; margin: 0 !important; + border: 0 !important; } #admin-bar-container .admin-bar-user * {