From 33a75a72433dc8bc6bd9733960cf9bfa70402d2f Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Thu, 29 Feb 2024 23:37:59 +0100 Subject: [PATCH] Extract CSS providers --- config/services.php | 8 +++ src/Attributes/AsCssProvider.php | 9 +++ src/Controller/CssController.php | 62 ++----------------- src/CssProvider.php | 53 ++++++++++++++++ src/CssProvider/BezelColor.php | 24 +++++++ src/CssProvider/SidebarColor.php | 21 +++++++ src/CssProvider/Signet.php | 29 +++++++++ src/CssProvider/SignetConfig.php | 15 +++++ src/CssProvider/TabBarIcon.php | 29 +++++++++ src/CssProvider/TabBarIconConfig.php | 14 +++++ .../NeustaPimcoreBackendBrandingExtension.php | 9 +++ 11 files changed, 216 insertions(+), 57 deletions(-) create mode 100644 src/Attributes/AsCssProvider.php create mode 100644 src/CssProvider.php create mode 100644 src/CssProvider/BezelColor.php create mode 100644 src/CssProvider/SidebarColor.php create mode 100644 src/CssProvider/Signet.php create mode 100644 src/CssProvider/SignetConfig.php create mode 100644 src/CssProvider/TabBarIcon.php create mode 100644 src/CssProvider/TabBarIconConfig.php diff --git a/config/services.php b/config/services.php index cd57299..1e60189 100644 --- a/config/services.php +++ b/config/services.php @@ -5,6 +5,7 @@ use Neusta\Pimcore\BackendBrandingBundle\Controller\CssController; use Neusta\Pimcore\BackendBrandingBundle\Controller\JsController; +use Neusta\Pimcore\BackendBrandingBundle\CssProvider; use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendAssetsListener; use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendResponseListener; @@ -21,9 +22,16 @@ ->autoconfigure() ->arg('$env', '%kernel.runtime_environment%') ->arg('$config', param('neusta_pimcore_backend_branding.environments')) + ->arg('$cssProvider', service(CssProvider::class)) ->set(JsController::class) ->autoconfigure() ->arg('$env', '%kernel.runtime_environment%') ->arg('$config', param('neusta_pimcore_backend_branding.environments')) + ->set(CssProvider::class) + ->autoconfigure() + ->arg('$providers', tagged_iterator('neusta_pimcore_backend_branding.css_provider')) + ->load('Neusta\Pimcore\BackendBrandingBundle\CssProvider\\', '../src/CssProvider') + ->autoconfigure() + ->exclude('../src/CssProvider/*Config.php') ; }; diff --git a/src/Attributes/AsCssProvider.php b/src/Attributes/AsCssProvider.php new file mode 100644 index 0000000..18ac373 --- /dev/null +++ b/src/Attributes/AsCssProvider.php @@ -0,0 +1,9 @@ + $config + * @param array $config Configuration by environment */ public function __construct( private readonly string $env, private readonly array $config, + private readonly CssProvider $cssProvider, ) { } @@ -32,55 +27,8 @@ public function __invoke(): Response $config = $this->config[$this->env] ?? []; $css = new CssRuleList(); - 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 (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 (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($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 (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($config['tabBarIcon']['position'])) { - $tabBarIcon->setProperty(new CssProperty('background-position', $config['tabBarIcon']['position'])); - } - - $css->addRule($tabBarIcon); + foreach (($this->cssProvider)($config) as $rule) { + $css->addRule($rule); } return new Response($css->toString(), Response::HTTP_OK, ['Content-type' => 'text/css']); diff --git a/src/CssProvider.php b/src/CssProvider.php new file mode 100644 index 0000000..660ba1c --- /dev/null +++ b/src/CssProvider.php @@ -0,0 +1,53 @@ +providers as $provider) { + \assert(is_callable($provider), sprintf('Provider "%s" is not callable.', get_debug_type($provider))); + + $reflector = new \ReflectionFunction($provider(...)); + + $arguments = []; + foreach ($reflector->getParameters() as $parameter) { + $name = $parameter->getName(); + $type = $parameter->getType(); + + \assert( + $type instanceof \ReflectionNamedType, + sprintf('Parameter "%s" of provider "%s" has no or more than one type.', $name, $provider::class), + ); + + $typeName = $type->getName(); + + \assert( + in_array($typeName, ['array', 'bool', 'float', 'int', 'string']) || class_exists($typeName), + sprintf('Parameter "%s" of provider "%s" has an unsupported type "%s".', $name, $provider::class, $typeName), + ); + + if (isset($config[$name])) { + $argument = $config[$name]; + + if (class_exists($typeName)) { + $argument = new $typeName(...$argument); + } + + $arguments[$name] = $argument; + } + } + + if ($arguments || !$reflector->getNumberOfParameters()) { + yield from $provider(...$arguments); + } + } + } +} diff --git a/src/CssProvider/BezelColor.php b/src/CssProvider/BezelColor.php new file mode 100644 index 0000000..1776511 --- /dev/null +++ b/src/CssProvider/BezelColor.php @@ -0,0 +1,24 @@ + .x-panel-bodyWrap > .x-tab-bar', $backgroundColor); + yield new CssRule('#pimcore_loading.loaded', $backgroundColor); + yield new CssRule('.x-body .sf-minitoolbar', $backgroundColor); + } +} diff --git a/src/CssProvider/SidebarColor.php b/src/CssProvider/SidebarColor.php new file mode 100644 index 0000000..753f9d2 --- /dev/null +++ b/src/CssProvider/SidebarColor.php @@ -0,0 +1,21 @@ +url, isUrl: true), + new CssProperty('background-size', $signet->size), + new CssProperty('background-position', $signet->position), + ); + + if (isset($signet->color)) { + $signetRule->setProperty(new CssProperty('background-color', $signet->color, isImportant: true)); + + yield new CssRule('#pimcore_avatar', + new CssProperty('background-color', $signet->color, isImportant: true), + ); + } + } +} diff --git a/src/CssProvider/SignetConfig.php b/src/CssProvider/SignetConfig.php new file mode 100644 index 0000000..419ed7c --- /dev/null +++ b/src/CssProvider/SignetConfig.php @@ -0,0 +1,15 @@ + .x-panel-bodyWrap > .x-tab-bar', + new CssProperty('background-image', $tabBarIcon->url, isUrl: true), + ); + + if (isset($tabBarIcon->size)) { + $tabBarIconRule->setProperty(new CssProperty('background-size', $tabBarIcon->size)); + } + + if (isset($tabBarIcon->position)) { + $tabBarIconRule->setProperty(new CssProperty('background-position', $tabBarIcon->position)); + } + + yield $tabBarIconRule; + } +} diff --git a/src/CssProvider/TabBarIconConfig.php b/src/CssProvider/TabBarIconConfig.php new file mode 100644 index 0000000..2961ccd --- /dev/null +++ b/src/CssProvider/TabBarIconConfig.php @@ -0,0 +1,14 @@ +load('services.php'); $container->setParameter('neusta_pimcore_backend_branding.environments', $mergedConfig['environments']); + + $container->registerAttributeForAutoconfiguration( + AsCssProvider::class, + static function (ChildDefinition $definition, AsCssProvider $attribute): void { + $definition->addTag('neusta_pimcore_backend_branding.css_provider'); + }, + ); } }