|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace League\Bundle\OAuth2ServerBundle\DependencyInjection\CompilerPass; |
| 4 | + |
| 5 | +use League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface; |
| 6 | +use League\OAuth2\Server\AuthorizationServer; |
| 7 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 9 | +use Symfony\Component\DependencyInjection\Definition; |
| 10 | +use Symfony\Component\DependencyInjection\Reference; |
| 11 | + |
| 12 | +class GrantTypePass implements CompilerPassInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @inheritDoc |
| 16 | + */ |
| 17 | + public function process(ContainerBuilder $container): void |
| 18 | + { |
| 19 | + // check if AuthorizationServer service is defined |
| 20 | + if (!$container->has(AuthorizationServer::class)) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + $definition = $container->findDefinition(AuthorizationServer::class); |
| 25 | + |
| 26 | + // find all service IDs with the league.oauth2_server.authorization_server.grant tag |
| 27 | + $taggedServices = $container->findTaggedServiceIds('league.oauth2_server.authorization_server.grant'); |
| 28 | + |
| 29 | + // enable grant type for each |
| 30 | + foreach ($taggedServices as $id => $tags) { |
| 31 | + // skip of custom grant using \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface |
| 32 | + // since there are handled by \League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantConfigurator |
| 33 | + // TODO remove code bloc when bundle interface and configurator will be deleted |
| 34 | + try { |
| 35 | + $grantDefinition = $container->findDefinition($id); |
| 36 | + $refGrantClass = new \ReflectionClass($grantDefinition->getClass()); |
| 37 | + if ($refGrantClass->implementsInterface(GrantTypeInterface::class)) { |
| 38 | + continue; |
| 39 | + } |
| 40 | + } catch (\ReflectionException) { |
| 41 | + // handling of this service as native one |
| 42 | + } |
| 43 | + |
| 44 | + foreach ($tags as $attributes) { |
| 45 | + $definition->addMethodCall('enableGrantType', [ |
| 46 | + new Reference($id), |
| 47 | + // use accessTokenTTL tag attribute if exists, otherwise use global bundle config |
| 48 | + new Definition(\DateInterval::class, [array_key_exists('accessTokenTTL', $attributes) |
| 49 | + ? $attributes['accessTokenTTL'] |
| 50 | + : $container->getParameter('league.oauth2_server.access_token_ttl.default')]), |
| 51 | + ]); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments