-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
249 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\EventListener; | ||
|
||
use Sonata\AdminBundle\Request\AdminFetcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\KernelEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Twig\Environment; | ||
|
||
/** | ||
* @author Christian Gripp <mail@core23.de> | ||
*/ | ||
final class AdminEventListener implements EventSubscriberInterface | ||
{ | ||
public function __construct(private Environment $twig, private AdminFetcherInterface $adminFetcher) | ||
{ | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => [['onKernelRequest', -50]], | ||
]; | ||
} | ||
|
||
public function onKernelRequest(KernelEvent $event): void | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
try { | ||
$admin = $this->adminFetcher->get($request); | ||
} catch (\InvalidArgumentException) { | ||
return; | ||
} | ||
|
||
$this->addVariable('admin', $admin); | ||
|
||
$templateRegistry = $admin->getTemplateRegistry(); | ||
|
||
if ($this->isXmlHttpRequest($request)) { | ||
$baseTemplate = $templateRegistry->getTemplate('ajax'); | ||
} else { | ||
$baseTemplate = $templateRegistry->getTemplate('layout'); | ||
} | ||
|
||
$this->addVariable('base_template', $baseTemplate); | ||
} | ||
|
||
/** | ||
* Returns true if the request is a XMLHttpRequest. | ||
* | ||
* @return bool True if the request is an XMLHttpRequest, false otherwise | ||
*/ | ||
private function isXmlHttpRequest(Request $request): bool | ||
{ | ||
if ($request->isXmlHttpRequest()) { | ||
return true; | ||
} | ||
|
||
return null !== $request->attributes->get('_xml_http_request'); | ||
} | ||
|
||
private function addVariable(string $name, mixed $value): void | ||
{ | ||
try { | ||
$this->twig->addGlobal($name, $value); | ||
} catch (\LogicException) { | ||
} | ||
} | ||
} |
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\AdminBundle\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
use Sonata\AdminBundle\EventListener\AdminEventListener; | ||
use Sonata\AdminBundle\Request\AdminFetcherInterface; | ||
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\KernelEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Twig\Environment; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
final class AdminEventListenerTest extends TestCase | ||
{ | ||
private Environment $twig; | ||
|
||
/** | ||
* @var AdminFetcherInterface&MockObject | ||
*/ | ||
private AdminFetcherInterface $adminFetcher; | ||
|
||
private AdminEventListener $listener; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->twig = new Environment(new ArrayLoader([ | ||
])); | ||
$this->adminFetcher = $this->createMock(AdminFetcherInterface::class); | ||
|
||
$this->listener = new AdminEventListener( | ||
$this->twig, | ||
$this->adminFetcher | ||
); | ||
} | ||
|
||
public function testGetSubscribedEvents(): void | ||
{ | ||
static::assertSame([ | ||
KernelEvents::REQUEST => [['onKernelRequest', -50]], | ||
], AdminEventListener::getSubscribedEvents()); | ||
} | ||
|
||
public function testOnKernelRequest(): void | ||
{ | ||
$request = new Request(); | ||
|
||
$event = new KernelEvent(self::createStub(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST); | ||
|
||
$admin = self::createStub(AdminInterface::class); | ||
|
||
$this->adminFetcher->method('get')->willReturn($admin); | ||
|
||
$templateRegistry = $this->createMock(MutableTemplateRegistryInterface::class); | ||
$templateRegistry->expects(static::once())->method('getTemplate')->with('layout') | ||
->willReturn('layout.html.twig'); | ||
|
||
$admin->method('getTemplateRegistry')->willReturn($templateRegistry); | ||
|
||
$this->listener->onKernelRequest($event); | ||
|
||
$global = $this->twig->getGlobals(); | ||
|
||
static::assertSame($admin, $global['admin']); | ||
static::assertSame('layout.html.twig', $global['base_template']); | ||
} | ||
|
||
public function testOnAjaxKernelRequest(): void | ||
{ | ||
$request = new Request(); | ||
$request->attributes->set('_xml_http_request', true); | ||
|
||
$event = new KernelEvent(self::createStub(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST); | ||
|
||
$admin = self::createStub(AdminInterface::class); | ||
|
||
$this->adminFetcher->method('get')->willReturn($admin); | ||
|
||
$templateRegistry = $this->createMock(MutableTemplateRegistryInterface::class); | ||
$templateRegistry->expects(static::once())->method('getTemplate')->with('ajax') | ||
->willReturn('ajax.html.twig'); | ||
|
||
$admin->method('getTemplateRegistry')->willReturn($templateRegistry); | ||
|
||
$this->listener->onKernelRequest($event); | ||
|
||
$global = $this->twig->getGlobals(); | ||
|
||
static::assertSame($admin, $global['admin']); | ||
static::assertSame('ajax.html.twig', $global['base_template']); | ||
} | ||
} |