Skip to content

Commit

Permalink
Improve RefreshOAuthTokenCompilerPass class quality
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Jan 24, 2022
1 parent 0d0d231 commit dba7384
Showing 1 changed file with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,41 @@
use Symfony\Component\DependencyInjection\Reference;

/**
* For Symfony 4.4 only
* @deprecated For Symfony 4.4 only
*
* Adds already registered by OAuthFactory RefreshAccessTokenListenerOld to security.firewall.map.context.
* It's taking control immediately after token was passed from session to token storage.
*/
class RefreshOAuthTokenCompilerPass implements CompilerPassInterface
final class RefreshOAuthTokenCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $listenerId => $listenerDef) {
if (0 === strpos($listenerId, 'hwi_oauth.context_listener.token_refresher.')) {
$firewallName = substr($listenerId, 43);
$firewallMapContextId = 'security.firewall.map.context.'.$firewallName;

if ($container->has($firewallMapContextId)) {
$firewallMapContextDef = $container->getDefinition($firewallMapContextId);
/* @var IteratorArgument $listenersIter */
$listenerIter = $firewallMapContextDef->getArgument(0);

$listenerRefs = $listenerIter->getValues();
// add listener after security.context_listener.X
for ($pos = 0; $pos < \count($listenerRefs); ++$pos) {
if (0 === strpos($listenerRefs[$pos], 'security.context_listener.')) {
array_splice($listenerRefs, $pos + 1, 0, [new Reference($listenerId)]);
break;
}
}
$listenerIter->setValues($listenerRefs);
if (0 !== strpos($listenerId, 'hwi_oauth.context_listener.token_refresher.')) {
continue;
}

// Cut 'hwi_oauth.context_listener.token_refresher.'
$firewallName = substr($listenerId, 43);
$firewallMapContextId = 'security.firewall.map.context.'.$firewallName;

if (!$container->has($firewallMapContextId)) {
continue;
}

$firewallMapContextDef = $container->getDefinition($firewallMapContextId);
/* @var IteratorArgument $listenersIter */
$listenerIter = $firewallMapContextDef->getArgument(0);

$listenerRefs = $listenerIter->getValues();
// add listener after security.context_listener.X
foreach ($listenerRefs as $pos => $posValue) {
if (0 === strpos($posValue, 'security.context_listener.')) {
array_splice($listenerRefs, $pos + 1, 0, [new Reference($listenerId)]);
break;
}
}
$listenerIter->setValues($listenerRefs);
}
}
}

0 comments on commit dba7384

Please sign in to comment.