-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow changing the page title & allow changing the favicon on the log…
…in page
- Loading branch information
Showing
5 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neusta\Pimcore\BackendBrandingBundle\EventListener; | ||
|
||
use Pimcore\Tool; | ||
use Pimcore\Tool\Session; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
|
||
#[AsEventListener] | ||
final class BackendResponseListener | ||
{ | ||
private const INTERCEPTED_ROUTES = [ | ||
'pimcore_admin_login', | ||
'pimcore_admin_login_fallback', | ||
'pimcore_admin_index', | ||
]; | ||
|
||
/** | ||
* @param array<string, array{ | ||
* favIcon: string, | ||
* title: array{login: string|null, backend: string|null}, | ||
* }> $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(); | ||
|
||
if (!$content = $response->getContent()) { | ||
return; | ||
} | ||
|
||
if ($titleConfig = $this->config[$this->env]['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>(.+)</title>#', | ||
"<title>{$title}</title>", | ||
$content, | ||
); | ||
|
||
if (null !== $replaced) { | ||
$content = $replaced; | ||
} | ||
} | ||
} | ||
|
||
if ($favIcon = $this->config[$this->env]['favIcon'] ?? null) { | ||
$replaced = preg_replace( | ||
'#<link rel="icon"[^>]+>#', | ||
sprintf('<link rel="shortcut icon" href="%s">', $favIcon), | ||
$content, | ||
); | ||
|
||
if (null !== $replaced) { | ||
$content = $replaced; | ||
} | ||
} | ||
|
||
$response->setContent($content); | ||
} | ||
} |