From 2b772ce47da75c644fdfab98161ee75d4ea0d7c2 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 | 63 +++++++++++++++++++ src/CssProvider/BezelColor.php | 27 ++++++++ src/CssProvider/SidebarColor.php | 24 +++++++ src/CssProvider/Signet.php | 32 ++++++++++ src/CssProvider/SignetConfig.php | 15 +++++ src/CssProvider/TabBarIcon.php | 32 ++++++++++ src/CssProvider/TabBarIconConfig.php | 14 +++++ .../NeustaPimcoreBackendBrandingExtension.php | 9 +++ 11 files changed, 238 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..e4299c1 --- /dev/null +++ b/src/CssProvider.php @@ -0,0 +1,63 @@ +> $providers + */ + public function __construct( + private readonly iterable $providers, + ) { + } + + /** + * @param array $config + * + * @return iterable + */ + public function __invoke(array $config): iterable + { + foreach ($this->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, get_debug_type($provider)), + ); + + $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, get_debug_type($provider), $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..3706bec --- /dev/null +++ b/src/CssProvider/BezelColor.php @@ -0,0 +1,27 @@ + + */ + public function __invoke(string $bezelColor): iterable + { + $backgroundColor = new CssProperty('background-color', $bezelColor); + + yield new CssRule('body.x-body #pimcore_body', $backgroundColor); + yield new CssRule('body.x-body #pimcore_loading.loaded', $backgroundColor); + yield new CssRule('body.x-body .sf-minitoolbar', $backgroundColor); + yield new CssRule('body.x-body #pimcore_panel_tabs > .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..0e31ef8 --- /dev/null +++ b/src/CssProvider/SidebarColor.php @@ -0,0 +1,24 @@ + + */ + public function __invoke(string $sidebarColor): iterable + { + $backgroundColor = new CssProperty('background-color', $sidebarColor); + + yield new CssRule('#pimcore_sidebar', $backgroundColor); + yield new CssRule('#pimcore_loading.loaded', $backgroundColor); + yield new CssRule('.x-body .sf-minitoolbar', $backgroundColor); + } +} diff --git a/src/CssProvider/Signet.php b/src/CssProvider/Signet.php new file mode 100644 index 0000000..9818b4a --- /dev/null +++ b/src/CssProvider/Signet.php @@ -0,0 +1,32 @@ + + */ + public function __invoke(SignetConfig $signet): iterable + { + yield $signetRule = new CssRule('#pimcore_signet', + new CssProperty('background-image', $signet->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 @@ + + */ + public function __invoke(TabBarIconConfig $tabBarIcon): iterable + { + $tabBarIconRule = new CssRule('#pimcore_panel_tabs > .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'); + }, + ); } }