Skip to content

Commit

Permalink
Build CSS rules with objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 1, 2024
1 parent 3343829 commit 1fdd5e7
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 60 deletions.
104 changes: 44 additions & 60 deletions src/Controller/CssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Neusta\Pimcore\BackendBrandingBundle\Controller;

use Neusta\Pimcore\BackendBrandingBundle\Css\CssProperty;
use Neusta\Pimcore\BackendBrandingBundle\Css\CssRule;
use Neusta\Pimcore\BackendBrandingBundle\Css\CssRuleList;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -27,78 +30,59 @@ public function __construct(
public function __invoke(): Response
{
$config = $this->config[$this->env] ?? [];
$css = [];
$css = new CssRuleList();

if ($bezelColor = $config['bezelColor'] ?? null) {
$css[] = <<<CSS
body.x-body #pimcore_body {
background-color: {$bezelColor};
}
body.x-body #pimcore_loading.loaded {
background-color: {$bezelColor};
}
body.x-body .sf-minitoolbar {
background-color: {$bezelColor};
}
body.x-body #pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar {
background-color: {$bezelColor};
}
#pimcore_loading.loaded {
background-color: {$bezelColor};
}
.x-body .sf-minitoolbar {
background-color: {$bezelColor};
}
CSS;
if (isset($config['bezelColor'])) {
$bezelColor = new CssProperty('background-color', $config['bezelColor']);
$css->addRule(new CssRule('body.x-body #pimcore_body', $bezelColor));
$css->addRule(new CssRule('body.x-body #pimcore_loading.loaded', $bezelColor));
$css->addRule(new CssRule('body.x-body .sf-minitoolbar', $bezelColor));
$css->addRule(new CssRule('body.x-body #pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar', $bezelColor));
$css->addRule(new CssRule('#pimcore_loading.loaded', $bezelColor));
$css->addRule(new CssRule('.x-body .sf-minitoolbar', $bezelColor));
}

if ($sidebarColor = $config['sidebarColor'] ?? null) {
$css[] = <<<CSS
#pimcore_sidebar {
background-color: {$sidebarColor};
}
#pimcore_loading.loaded {
background-color: {$sidebarColor};
}
.x-body .sf-minitoolbar {
background-color: {$sidebarColor};
}
CSS;
if (isset($config['sidebarColor'])) {
$sidebarColor = new CssProperty('background-color', $config['sidebarColor']);
$css->addRule(new CssRule('#pimcore_sidebar', $sidebarColor));
$css->addRule(new CssRule('#pimcore_loading.loaded', $sidebarColor));
$css->addRule(new CssRule('.x-body .sf-minitoolbar', $sidebarColor));
}

if ($signet = $config['signet'] ?? null) {
$css[] = <<<CSS
#pimcore_signet {
background-image: url({$signet['url']});
background-size: {$signet['size']};
background-position: {$signet['position']};
}
CSS;
if (isset($config['signet'])) {
$signet = new CssRule('#pimcore_signet',
new CssProperty('background-image', $config['signet']['url'], isUrl: true),
new CssProperty('background-size', $config['signet']['size']),
new CssProperty('background-position', $config['signet']['position']),
);

if (isset($signet['color'])) {
$css[] = <<<CSS
#pimcore_avatar {
background-color: {$signet['color']} !important;
}
#pimcore_signet {
background-color: {$signet['color']} !important;
}
CSS;
if (isset($config['signet']['color'])) {
$signet->setProperty(new CssProperty('background-color', $config['signet']['color'], isImportant: true));

$css->addRule(new CssRule('#pimcore_avatar',
new CssProperty('background-color', $config['signet']['color'], isImportant: true),
));
}

$css->addRule($signet);
}

if ($tabBarIcon = $config['tabBarIcon'] ?? null) {
$css[] = '#pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar {';
$css[] = " background-image: url({$tabBarIcon['url']});";
if (isset($tabBarIcon['size'])) {
$css[] = " background-size: {$tabBarIcon['size']};";
if (isset($config['tabBarIcon'])) {
$tabBarIcon = new CssRule('#pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar',
new CssProperty('background-image', $config['tabBarIcon']['url'], isUrl: true),
);

if (isset($config['tabBarIcon']['size'])) {
$tabBarIcon->setProperty(new CssProperty('background-size', $config['tabBarIcon']['size']));
}
if (isset($tabBarIcon['position'])) {
$css[] = " background-position: {$tabBarIcon['position']};";

if (isset($config['tabBarIcon']['position'])) {
$tabBarIcon->setProperty(new CssProperty('background-position', $config['tabBarIcon']['position']));
}
$css[] = '}';

$css->addRule($tabBarIcon);
}

return new Response(implode("\n", $css), Response::HTTP_OK, ['Content-type' => 'text/css']);
return new Response($css->toString(), Response::HTTP_OK, ['Content-type' => 'text/css']);
}
}
28 changes: 28 additions & 0 deletions src/Css/CssProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Css;

final class CssProperty
{
public function __construct(
public readonly string $name,
private readonly string $value,
private readonly bool $isUrl = false,
private readonly bool $isImportant = false,
) {
}

public function __toString(): string
{
$property = $this->isUrl
? sprintf('%s: url("%s")', $this->name, $this->value)
: sprintf("%s: %s", $this->name, $this->value);

if ($this->isImportant) {
$property .= ' !important';
}

return $property;
}
}
38 changes: 38 additions & 0 deletions src/Css/CssRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Css;

final class CssRule
{
private readonly string $selector;
/** @var list<CssProperty> */
private array $properties;

public function __construct(string $selector, CssProperty ...$properties)
{
$this->selector = $selector;

foreach ($properties as $property) {
$this->setProperty($property);
}
}

public function setProperty(CssProperty $property): self
{
$this->properties[$property->name] = $property;

return $this;
}

public function __toString(): string
{
$css = $this->selector . " {\n";
foreach ($this->properties as $property) {
$css .= "\t{$property};\n";
}
$css .= "}\n";

return $css;
}
}
19 changes: 19 additions & 0 deletions src/Css/CssRuleList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\BackendBrandingBundle\Css;

final class CssRuleList
{
private array $rules = [];

public function addRule(CssRule $rule): void
{
$this->rules[] = $rule;
}

public function toString(): string
{
return implode("\n", array_map(strval(...), $this->rules));
}
}

0 comments on commit 1fdd5e7

Please sign in to comment.