Skip to content

Commit

Permalink
Remove dependency to baraja-core/localization (#10)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Jan Barášek <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2022
1 parent 202ad65 commit 6262755
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": [
Expand Down
5 changes: 3 additions & 2 deletions src/AdminBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions src/AdminBarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ 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;
}


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,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down
3 changes: 2 additions & 1 deletion src/Menu/MenuLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 18 additions & 5 deletions src/Panel/BasicPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="%s" class="btn btn-primary">Home</a>', $baseUrl . $localeParam);
$buttons[] = sprintf('<a href="%s" class="btn btn-primary">Admin</a>', $baseUrl . '/admin' . $localeParam);
$buttons[] = sprintf('<a href="%s" class="btn btn-primary">Admin</a>', sprintf('%s/admin%s', $baseUrl, $localeParam));

$apiDoc = $this->processApiDocumentation($baseUrl);
if ($apiDoc !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/User/AdminIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Nette\Security\SimpleIdentity;
use Nette\Utils\Validators;

final class AdminIdentity extends SimpleIdentity
class AdminIdentity extends SimpleIdentity
{
private ?string $name;

Expand Down
3 changes: 3 additions & 0 deletions src/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 * {
Expand Down

0 comments on commit 6262755

Please sign in to comment.