diff --git a/src/Controller/CssController.php b/src/Controller/CssController.php index 2120ea0..848fa38 100644 --- a/src/Controller/CssController.php +++ b/src/Controller/CssController.php @@ -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; @@ -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[] = << .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[] = <<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[] = <<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']); } } diff --git a/src/Css/CssProperty.php b/src/Css/CssProperty.php new file mode 100644 index 0000000..bd3e016 --- /dev/null +++ b/src/Css/CssProperty.php @@ -0,0 +1,28 @@ +isUrl + ? sprintf('%s: url("%s")', $this->name, $this->value) + : sprintf("%s: %s", $this->name, $this->value); + + if ($this->isImportant) { + $property .= ' !important'; + } + + return $property; + } +} diff --git a/src/Css/CssRule.php b/src/Css/CssRule.php new file mode 100644 index 0000000..f7952a1 --- /dev/null +++ b/src/Css/CssRule.php @@ -0,0 +1,38 @@ + */ + 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; + } +} diff --git a/src/Css/CssRuleList.php b/src/Css/CssRuleList.php new file mode 100644 index 0000000..7df9016 --- /dev/null +++ b/src/Css/CssRuleList.php @@ -0,0 +1,19 @@ +rules[] = $rule; + } + + public function toString(): string + { + return implode("\n", array_map(strval(...), $this->rules)); + } +}