diff --git a/README.md b/README.md index 66bc1b3..074aced 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,14 @@ which is set via the `APP_ENV` environment variable. neusta_pimcore_backend_branding: environments: dev: + title: ACME bezelColor: '#fcc243' staging: bezelColor: '#005ea1' prod: + title: + login: Welcome to ACME! + backend: '{hostname} :: ACME' favIcon: bezelColor: '#00a13a' signet: # or just: diff --git a/config/services.php b/config/services.php index fbef015..cd57299 100644 --- a/config/services.php +++ b/config/services.php @@ -6,12 +6,17 @@ use Neusta\Pimcore\BackendBrandingBundle\Controller\CssController; use Neusta\Pimcore\BackendBrandingBundle\Controller\JsController; use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendAssetsListener; +use Neusta\Pimcore\BackendBrandingBundle\EventListener\BackendResponseListener; return static function (ContainerConfigurator $container) { $container->services() ->set(BackendAssetsListener::class) ->autoconfigure() ->arg('$urlGenerator', service('router')) + ->set(BackendResponseListener::class) + ->autoconfigure() + ->arg('$env', '%kernel.runtime_environment%') + ->arg('$config', param('neusta_pimcore_backend_branding.environments')) ->set(CssController::class) ->autoconfigure() ->arg('$env', '%kernel.runtime_environment%') diff --git a/src/Controller/CssController.php b/src/Controller/CssController.php index f04dc2f..9ca0196 100644 --- a/src/Controller/CssController.php +++ b/src/Controller/CssController.php @@ -25,9 +25,10 @@ public function __construct( public function __invoke(): Response { + $config = $this->config[$this->env] ?? []; $css = []; - if ($bezelColor = $this->config[$this->env]['bezelColor'] ?? null) { + if ($bezelColor = $config['bezelColor'] ?? null) { $css[] = <<config[$this->env]['signet'] ?? null) { + if ($signet = $config['signet'] ?? null) { $css[] = <<config[$this->env]['tabBarIcon'] ?? null) { + if ($tabBarIcon = $config['tabBarIcon'] ?? null) { $css[] = '#pimcore_panel_tabs > .x-panel-bodyWrap > .x-tab-bar {'; $css[] = " background-image: url({$tabBarIcon['url']});"; if ($tabBarIcon['size']) { diff --git a/src/Controller/JsController.php b/src/Controller/JsController.php index 1585617..5ede5b7 100644 --- a/src/Controller/JsController.php +++ b/src/Controller/JsController.php @@ -3,6 +3,7 @@ namespace Neusta\Pimcore\BackendBrandingBundle\Controller; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\Routing\Annotation\Route; @@ -11,7 +12,10 @@ final class JsController { /** - * @param array $config + * @param array $config */ public function __construct( private readonly string $env, @@ -19,16 +23,11 @@ public function __construct( ) { } - public function __invoke(): Response + public function __invoke(Request $request): Response { + $config = $this->config[$this->env] ?? []; $js = []; - if ($favIcon = $this->config[$this->env]['favIcon'] ?? null) { - $js[] = << 'text/javascript']); } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2616204..9aa1291 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -21,6 +21,16 @@ public function getConfigTreeBuilder(): TreeBuilder ->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() ->append($this->createBackgroundImageNode('signet', '70%', 'center')) diff --git a/src/EventListener/BackendResponseListener.php b/src/EventListener/BackendResponseListener.php new file mode 100644 index 0000000..85c8a31 --- /dev/null +++ b/src/EventListener/BackendResponseListener.php @@ -0,0 +1,84 @@ + $config + */ + public function __construct( + private readonly string $env, + private readonly array $config, + ) { + } + + public function __invoke(ResponseEvent $event): void + { + if (!$event->isMainRequest()) { + return; + } + + $request = $event->getRequest(); + + if (!\in_array($request->attributes->get('_route'), self::INTERCEPTED_ROUTES, true)) { + return; + } + + $response = $event->getResponse(); + $content = $response->getContent(); + $config = $this->config[$this->env] ?? null; + + if (!$content || !$config) { + return; + } + + if ($titleConfig = $config['title'] ?? null) { + $loggedIn = null !== Session::getSessionBag($request->getSession(), 'pimcore_admin')?->get('user'); + $title = $loggedIn ? $titleConfig['backend'] : $titleConfig['login']; + + if ($title) { + $title = strtr($title, ['{hostname}' => htmlentities((string) Tool::getHostname(), \ENT_QUOTES, 'UTF-8')]); + $replaced = preg_replace( + '#(.+)#', + "{$title}", + $content, + ); + + if (null !== $replaced) { + $content = $replaced; + } + } + } + + if ($favIcon = $config['favIcon'] ?? null) { + $replaced = preg_replace( + '#]+>#', + sprintf('', $favIcon), + $content, + ); + + if (null !== $replaced) { + $content = $replaced; + } + } + + $response->setContent($content); + } +}