-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Nyholm/contracts
Use EventDispatcher from SymfonyContracts
- Loading branch information
Showing
9 changed files
with
96 additions
and
24 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
38 changes: 38 additions & 0 deletions
38
DependencyInjection/CompilerPass/EventDispatcherCompilerPass.php
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Controller\AuthorizationController; | ||
use Trikoder\Bundle\OAuth2Bundle\League\Repository\ScopeRepository; | ||
use Trikoder\Bundle\OAuth2Bundle\League\Repository\UserRepository; | ||
use Trikoder\Bundle\OAuth2Bundle\Service\BCEventDispatcher; | ||
|
||
class EventDispatcherCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->has(EventDispatcherInterface::class)) { | ||
return; | ||
} | ||
|
||
// Register a new service | ||
$definition = new Definition(BCEventDispatcher::class); | ||
$definition->addArgument(new Reference(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class)); | ||
$container->setDefinition(BCEventDispatcher::class, $definition); | ||
|
||
// Use our new service | ||
$container->getDefinition(ScopeRepository::class) | ||
->replaceArgument(3, new Reference(BCEventDispatcher::class)); | ||
$container->getDefinition(UserRepository::class) | ||
->replaceArgument(1, new Reference(BCEventDispatcher::class)); | ||
$container->getDefinition(AuthorizationController::class) | ||
->replaceArgument(1, new Reference(BCEventDispatcher::class)); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Service; | ||
|
||
use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyEventDispatcher; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcher; | ||
|
||
/** | ||
* This event dispatcher works as a BC layer for Symfony < 4.3. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class BCEventDispatcher implements ContractsEventDispatcher | ||
{ | ||
private $eventDispatcher; | ||
|
||
public function __construct(LegacyEventDispatcher $eventDispatcher) | ||
{ | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
/** | ||
* This method is only used in this bundle. We will always call dispatch(object, string) | ||
*/ | ||
public function dispatch($event/*, string $eventName = null*/) | ||
{ | ||
$eventName = 1 < \func_num_args() ? func_get_arg(1) : null; | ||
$eventName = $eventName ?? \get_class($event); | ||
|
||
return $this->eventDispatcher->dispatch($eventName, $event); | ||
} | ||
|
||
public function addListener($eventName, $listener, $priority = 0) | ||
{ | ||
return $this->eventDispatcher->addListener($eventName, $listener, $priority); | ||
} | ||
} |
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