-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autoinject all admin variables #8116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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); | ||
VincentLanglet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} 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']); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A NEXT_MAJOR replace with
render()
should be added (?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
NEXT_MAJOR
comment is pedantic because the annotation should provide enough informationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we had to resolve all the 4.x NEXT_MAJOR some of them were not obvious, so there is never too much comment.
@psalm-suppress
just tell to ignore the deprecation but does not explain how to solve it.At least
above
renderWithExtraParams
definition might be more useful thanremove this method
.