From fa7e3d7c6c11a50ab12245fe1217a47ac5150154 Mon Sep 17 00:00:00 2001
From: Jacob Dreesen <j.dreesen@neusta.de>
Date: Wed, 8 May 2024 19:37:22 +0200
Subject: [PATCH] Remove custom environment handling

Symfony environments should be used instead.
---
 README.md                                     | 56 +++++++-------
 config/services.php                           |  8 +-
 src/DependencyInjection/Configuration.php     | 77 ++++++++-----------
 .../NeustaPimcoreBackendBrandingExtension.php |  2 +-
 src/SettingsFactory.php                       | 20 -----
 5 files changed, 65 insertions(+), 98 deletions(-)
 delete mode 100644 src/SettingsFactory.php

diff --git a/README.md b/README.md
index 91c9be8..0b6254d 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
 # Pimcore Backend Branding Bundle
 
+This bundle allows you to configure the branding of thr Pimcore admin backend.
+
 ## Installation
 
 1.  **Require the bundle**
@@ -16,38 +18,36 @@
    Neusta\Pimcore\BackendBrandingBundle\NeustaPimcoreBackendBrandingBundle::class => ['all' => true],
    ```
 
-## Usage
-
-This bundle allows you to configure the backend branding per environment.
-The current environment is determined through Symfony's [`kernel.runtime_environment`](https://symfony.com/doc/6.4/reference/configuration/kernel.html#kernel-runtime-environment) parameter,
-which can be set via the `APP_RUNTIME_ENV` environment variable.
-If not set, it falls back to the [`kernel.environment`](https://symfony.com/doc/6.4/reference/configuration/kernel.html#kernel-environment), 
-which is set via the `APP_ENV` environment variable.
-
 ## Configuration
 
 ```yaml
 neusta_pimcore_backend_branding:
-    environments:
-        dev:
-            title: ACME
-            sidebarColor: '#fcc243'
-        staging:
-            sidebarColor: '#005ea1'
-        prod:
-            title:
-                login: Welcome to ACME!
-                backend: '{hostname} :: ACME'
-            favIcon: <url-of-your-fav-icon>
-            bezelColor: '#00a13a'
-            signet: # or just: <url-of-your-logo>
-                url: <url-of-your-logo>
-                size: 70%
-                position: center
-                color: '#000'
-            tabBarIcon:
-                url: <url-of-your-logo>
-                size: 40px
+    favIcon: <url-of-your-fav-icon>
+    signet: # or just: <url-of-your-logo>
+        url: <url-of-your-logo>
+        size: 70%
+        position: center
+        color: '#000'
+    tabBarIcon:
+        url: <url-of-your-logo>
+        size: 40px
+
+when@dev:
+    neusta_pimcore_backend_branding:
+        title: ACME Development
+        sidebarColor: '#fcc243'
+
+when@test:
+    neusta_pimcore_backend_branding:
+        title: ACME Testing
+        sidebarColor: '#005ea1'
+
+when@prod:
+    neusta_pimcore_backend_branding:
+        title:
+            login: Welcome to ACME!
+            backend: '{hostname} :: ACME'
+        bezelColor: '#00a13a'
 ```
 
 ## Contribution
diff --git a/config/services.php b/config/services.php
index eb1b9c0..38ded62 100644
--- a/config/services.php
+++ b/config/services.php
@@ -8,16 +8,12 @@
 use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendAssetsListener;
 use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendResponseListener;
 use Neusta\Pimcore\BackendBrandingBundle\Settings;
-use Neusta\Pimcore\BackendBrandingBundle\SettingsFactory;
 
 return static function (ContainerConfigurator $container) {
     $container->services()
         ->set(Settings::class)
-            ->factory([service(SettingsFactory::class), 'create'])
-            ->args(['%kernel.runtime_environment%'])
-        ->set(SettingsFactory::class)
-            ->arg('$config', param('neusta_pimcore_backend_branding.environments'))
-            ->arg('$denormalizer', service('serializer'))
+            ->factory([service('serializer'), 'denormalize'])
+            ->args([param('neusta_pimcore_backend_branding.config'), Settings::class])
         ->set(BackendAssetsListener::class)
             ->autoconfigure()
             ->arg('$urlGenerator', service('router'))
diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php
index 7703d93..8ad2207 100644
--- a/src/DependencyInjection/Configuration.php
+++ b/src/DependencyInjection/Configuration.php
@@ -13,50 +13,41 @@ public function getConfigTreeBuilder(): TreeBuilder
         $rootNode = $treeBuilder->getRootNode();
 
         $rootNode
-            ->fixXmlConfig('environment')
             ->children()
-                ->arrayNode('environments')
-                    ->normalizeKeys(false)
-                    ->useAttributeAsKey('name')
-                    ->arrayPrototype()
-                        ->children()
-                            ->arrayNode('title')
-                                ->beforeNormalization()
-                                    ->ifString()
-                                    ->then(fn (string $v) => ['login' => $v, 'backend' => $v])
-                                ->end()
-                                ->children()
-                                    ->scalarNode('login')->defaultNull()->end()
-                                    ->scalarNode('backend')->defaultNull()->end()
-                                ->end()
-                            ->end()
-                            ->scalarNode('favIcon')->end()
-                            ->scalarNode('bezelColor')->end()
-                            ->scalarNode('sidebarColor')->end()
-                            ->arrayNode('signet')
-                                ->beforeNormalization()
-                                    ->ifString()
-                                    ->then(fn (string $v) => ['url' => $v, 'size' => '70%', 'position' => 'center'])
-                                ->end()
-                                ->children()
-                                    ->scalarNode('url')->isRequired()->cannotBeEmpty()->end()
-                                    ->scalarNode('size')->defaultValue('70%')->end()
-                                    ->scalarNode('position')->defaultValue('center')->end()
-                                    ->scalarNode('color')->defaultNull()->end()
-                                ->end()
-                            ->end()
-                            ->arrayNode('tabBarIcon')
-                                ->beforeNormalization()
-                                    ->ifString()
-                                    ->then(fn (string $v) => ['url' => $v])
-                                ->end()
-                                ->children()
-                                    ->scalarNode('url')->isRequired()->cannotBeEmpty()->end()
-                                    ->scalarNode('size')->defaultNull()->end()
-                                    ->scalarNode('position')->defaultNull()->end()
-                                ->end()
-                            ->end()
-                        ->end()
+                ->arrayNode('title')
+                    ->beforeNormalization()
+                        ->ifString()
+                        ->then(fn (string $v) => ['login' => $v, 'backend' => $v])
+                    ->end()
+                    ->children()
+                        ->scalarNode('login')->defaultNull()->end()
+                        ->scalarNode('backend')->defaultNull()->end()
+                    ->end()
+                ->end()
+                ->scalarNode('favIcon')->end()
+                ->scalarNode('bezelColor')->end()
+                ->scalarNode('sidebarColor')->end()
+                ->arrayNode('signet')
+                    ->beforeNormalization()
+                        ->ifString()
+                        ->then(fn (string $v) => ['url' => $v, 'size' => '70%', 'position' => 'center'])
+                    ->end()
+                    ->children()
+                        ->scalarNode('url')->isRequired()->cannotBeEmpty()->end()
+                        ->scalarNode('size')->defaultValue('70%')->end()
+                        ->scalarNode('position')->defaultValue('center')->end()
+                        ->scalarNode('color')->defaultNull()->end()
+                    ->end()
+                ->end()
+                ->arrayNode('tabBarIcon')
+                    ->beforeNormalization()
+                        ->ifString()
+                        ->then(fn (string $v) => ['url' => $v])
+                    ->end()
+                    ->children()
+                        ->scalarNode('url')->isRequired()->cannotBeEmpty()->end()
+                        ->scalarNode('size')->defaultNull()->end()
+                        ->scalarNode('position')->defaultNull()->end()
                     ->end()
                 ->end()
             ->end()
diff --git a/src/DependencyInjection/NeustaPimcoreBackendBrandingExtension.php b/src/DependencyInjection/NeustaPimcoreBackendBrandingExtension.php
index b8d233b..70e75b7 100644
--- a/src/DependencyInjection/NeustaPimcoreBackendBrandingExtension.php
+++ b/src/DependencyInjection/NeustaPimcoreBackendBrandingExtension.php
@@ -20,7 +20,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
         $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__, 2) . '/config'));
         $loader->load('services.php');
 
-        $container->setParameter('neusta_pimcore_backend_branding.environments', $mergedConfig['environments']);
+        $container->setParameter('neusta_pimcore_backend_branding.config', $mergedConfig);
 
         $container->registerAttributeForAutoconfiguration(
             AsCssProvider::class,
diff --git a/src/SettingsFactory.php b/src/SettingsFactory.php
deleted file mode 100644
index 9cba505..0000000
--- a/src/SettingsFactory.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace Neusta\Pimcore\BackendBrandingBundle;
-
-use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
-
-final class SettingsFactory
-{
-    public function __construct(
-        private readonly array $config,
-        private readonly DenormalizerInterface $denormalizer,
-    ) {
-    }
-
-    public function create(string $env): Settings
-    {
-        return $this->denormalizer->denormalize($this->config[$env] ?? [], Settings::class);
-    }
-}