Skip to content

Commit 00edc1e

Browse files
committed
Configure services/routes using PHP
1 parent a00d679 commit 00edc1e

File tree

13 files changed

+451
-290
lines changed

13 files changed

+451
-290
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ For implementation into Symfony projects, please see [bundle documentation](docs
116116

117117
```yaml
118118
oauth2:
119-
resource: '@LeagueOAuth2ServerBundle/Resources/config/routes.xml'
119+
resource: '@LeagueOAuth2ServerBundle/Resources/config/routes.php'
120120
```
121121

122122
You can verify that everything is working by issuing a `POST` request to the `/token` endpoint.

psalm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@
4545
<RawObjectIteration errorLevel="error"/>
4646
<InvalidStringClass errorLevel="error"/>
4747
<UnresolvableInclude errorLevel="error"/>
48+
<ImplicitToStringCast errorLevel="error">
49+
<errorLevel type="suppress">
50+
<file name="src/Resources/config/services.php" />
51+
</errorLevel>
52+
</ImplicitToStringCast>
53+
<MixedInferredReturnType errorLevel="error">
54+
<errorLevel type="suppress">
55+
<file name="src/Resources/config/services.php" />
56+
</errorLevel>
57+
</MixedInferredReturnType>
58+
<MixedReturnStatement errorLevel="error">
59+
<errorLevel type="suppress">
60+
<file name="src/Resources/config/services.php" />
61+
</errorLevel>
62+
</MixedReturnStatement>
4863
</issueHandlers>
4964

5065
<plugins>

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
use Symfony\Component\DependencyInjection\Definition;
3636
use Symfony\Component\DependencyInjection\Extension\Extension;
3737
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
38-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
38+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
3939
use Symfony\Component\DependencyInjection\Reference;
4040
use Symfony\Component\HttpKernel\KernelEvents;
4141

@@ -50,8 +50,8 @@ final class LeagueOAuth2ServerExtension extends Extension implements PrependExte
5050
*/
5151
public function load(array $configs, ContainerBuilder $container)
5252
{
53-
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
54-
$loader->load('services.xml');
53+
$loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
54+
$loader->load('services.php');
5555

5656
$config = $this->processConfiguration(new Configuration(), $configs);
5757

@@ -129,14 +129,14 @@ private function configureAuthorizationServer(ContainerBuilder $container, array
129129
{
130130
$authorizationServer = $container
131131
->getDefinition(AuthorizationServer::class)
132-
->replaceArgument('$privateKey', new Definition(CryptKey::class, [
132+
->replaceArgument(3, new Definition(CryptKey::class, [
133133
$config['private_key'],
134134
$config['private_key_passphrase'],
135135
false,
136136
]));
137137

138138
if ('plain' === $config['encryption_key_type']) {
139-
$authorizationServer->replaceArgument('$encryptionKey', $config['encryption_key']);
139+
$authorizationServer->replaceArgument(4, $config['encryption_key']);
140140
} elseif ('defuse' === $config['encryption_key_type']) {
141141
if (!class_exists(Key::class)) {
142142
throw new \RuntimeException('You must install the "defuse/php-encryption" package to use "encryption_key_type: defuse".');
@@ -147,7 +147,7 @@ private function configureAuthorizationServer(ContainerBuilder $container, array
147147
->addArgument($config['encryption_key']);
148148
$container->setDefinition('league.oauth2-server.defuse_key', $keyDefinition);
149149

150-
$authorizationServer->replaceArgument('$encryptionKey', new Reference('league.oauth2-server.defuse_key'));
150+
$authorizationServer->replaceArgument(4, new Reference('league.oauth2-server.defuse_key'));
151151
}
152152

153153
if ($config['enable_client_credentials_grant']) {
@@ -205,7 +205,7 @@ private function configureGrants(ContainerBuilder $container, array $config): vo
205205
;
206206

207207
$authCodeGrantDefinition = $container->getDefinition(AuthCodeGrant::class);
208-
$authCodeGrantDefinition->replaceArgument('$authCodeTTL', new Definition(\DateInterval::class, [$config['auth_code_ttl']]))
208+
$authCodeGrantDefinition->replaceArgument(2, new Definition(\DateInterval::class, [$config['auth_code_ttl']]))
209209
->addMethodCall('setRefreshTokenTTL', [
210210
new Definition(\DateInterval::class, [$config['refresh_token_ttl']]),
211211
])
@@ -217,7 +217,7 @@ private function configureGrants(ContainerBuilder $container, array $config): vo
217217

218218
$container
219219
->getDefinition(ImplicitGrant::class)
220-
->replaceArgument('$accessTokenTTL', new Definition(\DateInterval::class, [$config['access_token_ttl']]))
220+
->replaceArgument(0, new Definition(\DateInterval::class, [$config['access_token_ttl']]))
221221
;
222222
}
223223

@@ -235,11 +235,11 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder
235235

236236
switch ($persistenceMethod) {
237237
case 'in_memory':
238-
$loader->load('storage/in_memory.xml');
238+
$loader->load('storage/in_memory.php');
239239
$this->configureInMemoryPersistence($container);
240240
break;
241241
case 'doctrine':
242-
$loader->load('storage/doctrine.xml');
242+
$loader->load('storage/doctrine.php');
243243
$this->configureDoctrinePersistence($container, $persistenceConfiguration);
244244
break;
245245
}
@@ -255,27 +255,27 @@ private function configureDoctrinePersistence(ContainerBuilder $container, array
255255

256256
$container
257257
->getDefinition(AccessTokenManager::class)
258-
->replaceArgument('$entityManager', $entityManager)
258+
->replaceArgument(0, $entityManager)
259259
;
260260

261261
$container
262262
->getDefinition(ClientManager::class)
263-
->replaceArgument('$entityManager', $entityManager)
263+
->replaceArgument(0, $entityManager)
264264
;
265265

266266
$container
267267
->getDefinition(RefreshTokenManager::class)
268-
->replaceArgument('$entityManager', $entityManager)
268+
->replaceArgument(0, $entityManager)
269269
;
270270

271271
$container
272272
->getDefinition(AuthorizationCodeManager::class)
273-
->replaceArgument('$entityManager', $entityManager)
273+
->replaceArgument(0, $entityManager)
274274
;
275275

276276
$container
277277
->getDefinition(DoctrineCredentialsRevoker::class)
278-
->replaceArgument('$entityManager', $entityManager)
278+
->replaceArgument(0, $entityManager)
279279
;
280280

281281
$container->setParameter('league.oauth2-server.persistence.doctrine.enabled', true);
@@ -291,7 +291,7 @@ private function configureResourceServer(ContainerBuilder $container, array $con
291291
{
292292
$container
293293
->getDefinition(ResourceServer::class)
294-
->replaceArgument('$publicKey', new Definition(CryptKey::class, [
294+
->replaceArgument(1, new Definition(CryptKey::class, [
295295
$config['public_key'],
296296
null,
297297
false,

src/DependencyInjection/Security/OAuth2Factory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
2323
$providerId = 'security.authentication.provider.oauth2.' . $id;
2424
$container
2525
->setDefinition($providerId, new ChildDefinition(OAuth2Provider::class))
26-
->replaceArgument('$userProvider', new Reference($userProvider))
27-
->replaceArgument('$providerKey', $id);
26+
->replaceArgument(0, new Reference($userProvider))
27+
->replaceArgument(3, $id);
2828

2929
$listenerId = 'security.authentication.listener.oauth2.' . $id;
3030
$container
3131
->setDefinition($listenerId, new ChildDefinition(OAuth2Listener::class))
32-
->replaceArgument('$providerKey', $id);
32+
->replaceArgument(4, $id);
3333

3434
return [$providerId, $listenerId, OAuth2EntryPoint::class];
3535
}

src/Resources/config/routes.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use League\Bundle\OAuth2ServerBundle\Controller\AuthorizationController;
6+
use League\Bundle\OAuth2ServerBundle\Controller\TokenController;
7+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
8+
9+
return function (RoutingConfigurator $routes) {
10+
$routes
11+
->add('oauth2_authorize', '/authorize')
12+
->controller([AuthorizationController::class, 'indexAction'])
13+
->methods(['GET'])
14+
15+
->add('oauth2_token', '/token')
16+
->controller([TokenController::class, 'indexAction'])
17+
->methods(['POST'])
18+
;
19+
};

src/Resources/config/routes.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)