-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] A compiler pass for aliased userland events #33793
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
Merged
nicolas-grekas
merged 1 commit into
symfony:4.4
from
derrabus:improvement/custom-aliased-events
Oct 4, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
...SecurityBundle/Tests/Functional/Bundle/EventBundle/DependencyInjection/EventExtension.php
This file contains hidden or 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,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\EventBundle\DependencyInjection; | ||
|
||
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\EventBundle\EventSubscriber\TestSubscriber; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
|
||
final class EventExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container): void | ||
{ | ||
$container->register('test_subscriber', TestSubscriber::class) | ||
->setPublic(true) | ||
->addTag('kernel.event_subscriber'); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/EventBundle/EventBundle.php
This file contains hidden or 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,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\EventBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
final class EventBundle extends Bundle | ||
{ | ||
} |
38 changes: 38 additions & 0 deletions
38
...dle/SecurityBundle/Tests/Functional/Bundle/EventBundle/EventSubscriber/TestSubscriber.php
This file contains hidden or 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,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\EventBundle\EventSubscriber; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; | ||
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent; | ||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
use Symfony\Component\Security\Http\Event\SwitchUserEvent; | ||
|
||
final class TestSubscriber implements EventSubscriberInterface | ||
{ | ||
public $calledMethods = []; | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
AuthenticationSuccessEvent::class => 'onAuthenticationSuccess', | ||
AuthenticationFailureEvent::class => 'onAuthenticationFailure', | ||
InteractiveLoginEvent::class => 'onInteractiveLogin', | ||
SwitchUserEvent::class => 'onSwitchUser', | ||
]; | ||
} | ||
|
||
public function __call(string $name, array $arguments) | ||
{ | ||
$this->calledMethods[$name] = ($this->calledMethods[$name] ?? 0) + 1; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bundle/SecurityBundle/Tests/Functional/EventAliasTest.php
This file contains hidden or 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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\AuthenticationEvents; | ||
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; | ||
use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | ||
use Symfony\Component\Security\Http\Event\SwitchUserEvent; | ||
use Symfony\Component\Security\Http\SecurityEvents; | ||
|
||
final class EventAliasTest extends AbstractWebTestCase | ||
{ | ||
public function testAliasedEvents(): void | ||
{ | ||
$client = $this->createClient(['test_case' => 'AliasedEvents', 'root_config' => 'config.yml']); | ||
$container = $client->getContainer(); | ||
$dispatcher = $container->get('event_dispatcher'); | ||
|
||
$dispatcher->dispatch(new AuthenticationSuccessEvent($this->createMock(TokenInterface::class)), AuthenticationEvents::AUTHENTICATION_SUCCESS); | ||
$dispatcher->dispatch(new AuthenticationFailureEvent($this->createMock(TokenInterface::class), new AuthenticationException()), AuthenticationEvents::AUTHENTICATION_FAILURE); | ||
$dispatcher->dispatch(new InteractiveLoginEvent($this->createMock(Request::class), $this->createMock(TokenInterface::class)), SecurityEvents::INTERACTIVE_LOGIN); | ||
$dispatcher->dispatch(new SwitchUserEvent($this->createMock(Request::class), $this->createMock(UserInterface::class), $this->createMock(TokenInterface::class)), SecurityEvents::SWITCH_USER); | ||
|
||
$this->assertEquals( | ||
[ | ||
'onAuthenticationSuccess' => 1, | ||
'onAuthenticationFailure' => 1, | ||
'onInteractiveLogin' => 1, | ||
'onSwitchUser' => 1, | ||
], | ||
$container->get('test_subscriber')->calledMethods | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AliasedEvents/bundles.php
This file contains hidden or 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,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\SecurityBundle\SecurityBundle; | ||
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\EventBundle\EventBundle; | ||
|
||
return [ | ||
new FrameworkBundle(), | ||
new SecurityBundle(), | ||
new EventBundle(), | ||
]; |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AliasedEvents/config.yml
This file contains hidden or 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,2 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } |
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/EventDispatcher/DependencyInjection/AddEventAliasesPass.php
This file contains hidden or 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,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* This pass allows bundles to extend the list of event aliases. | ||
* | ||
* @author Alexander M. Turek <me@derrabus.de> | ||
*/ | ||
class AddEventAliasesPass implements CompilerPassInterface | ||
{ | ||
private $eventAliases; | ||
private $eventAliasesParameter; | ||
|
||
public function __construct(array $eventAliases, string $eventAliasesParameter = 'event_dispatcher.event_aliases') | ||
{ | ||
$this->eventAliases = $eventAliases; | ||
$this->eventAliasesParameter = $eventAliasesParameter; | ||
} | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
$eventAliases = $container->hasParameter($this->eventAliasesParameter) ? $container->getParameter($this->eventAliasesParameter) : []; | ||
|
||
$container->setParameter( | ||
$this->eventAliasesParameter, | ||
array_merge($eventAliases, $this->eventAliases) | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
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.
do we actually need such conflict rule ?
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.
If you have a Symfony 4.3 application with an event subscriber registered to, let's say,
SwitchUserEvent::class
, that application would break if you upgraded FrameworkBundle to 4.4 without upgrading SecurityBundle as well.