1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/******************************************************************************* |
4
|
|
|
* This file is part of the GraphQL Bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) YnloUltratech <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
******************************************************************************/ |
11
|
|
|
|
12
|
|
|
namespace Ynlo\GraphQLBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
20
|
|
|
use Ynlo\GraphQLBundle\Cache\DefinitionCacheWarmer; |
21
|
|
|
use Ynlo\GraphQLBundle\Controller\GraphQLEndpointController; |
22
|
|
|
use Ynlo\GraphQLBundle\Encoder\IDEncoderManager; |
23
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\JWTGraphiQLAuthentication; |
24
|
|
|
use Ynlo\GraphQLBundle\GraphiQL\LexikJWTGraphiQLAuthenticator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class YnloGraphQLExtension |
28
|
|
|
*/ |
29
|
|
|
class YnloGraphQLExtension extends Extension |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function load(array $configs, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
$configuration = new Configuration(); |
37
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
38
|
|
|
|
39
|
|
|
if (!isset($config['namespaces']['bundles']['aliases']['GraphQLBundle'])) { |
40
|
|
|
$config['namespaces']['bundles']['aliases']['GraphQLBundle'] = 'AppBundle'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$container->setParameter('graphql.config', $config); |
44
|
|
|
$container->setParameter('graphql.pagination', $config['pagination'] ?? []); |
45
|
|
|
$container->setParameter('graphql.error_handling', $config['error_handling'] ?? []); |
46
|
|
|
$container->setParameter('graphql.error_handling.controlled_errors', $config['error_handling']['controlled_errors'] ?? []); |
47
|
|
|
$container->setParameter('graphql.error_handling.jwt_auth_failure_compatibility', $config['error_handling']['jwt_auth_failure_compatibility'] ?? false); |
48
|
|
|
$container->setParameter('graphql.namespaces', $config['namespaces'] ?? []); |
49
|
|
|
$container->setParameter('graphql.cors_config', $config['cors'] ?? []); |
50
|
|
|
$container->setParameter('graphql.graphiql', $config['graphiql'] ?? []); |
51
|
|
|
$container->setParameter('graphql.graphiql_auth_jwt', $config['graphiql']['authentication']['provider']['jwt'] ?? []);//DEPRECATED |
52
|
|
|
$container->setParameter('graphql.graphiql_auth_lexik_jwt', $config['graphiql']['authentication']['provider']['lexik_jwt'] ?? []); |
53
|
|
|
$container->setParameter('graphql.security.validation_rules', $config['security']['validation_rules'] ?? []); |
54
|
|
|
|
55
|
|
|
$endpointsConfig = []; |
56
|
|
|
$endpointsConfig['endpoints'] = $config['endpoints'] ?? []; |
57
|
|
|
$endpointsConfig['default'] = $config['endpoint_default'] ?? null; |
58
|
|
|
$endpointsConfig['alias'] = $config['endpoint_alias'] ?? []; |
59
|
|
|
|
60
|
|
|
$container->setParameter('graphql.endpoints', $endpointsConfig); |
61
|
|
|
$container->setParameter('graphql.endpoints_list', array_keys($endpointsConfig['endpoints'])); |
62
|
|
|
|
63
|
|
|
$graphiQLAuthProvider = null; |
64
|
|
|
|
65
|
|
|
//DEPRECATED since v1.1 |
66
|
|
|
if ($config['graphiql']['authentication']['provider']['jwt']['enabled'] ?? false) { |
67
|
|
|
$graphiQLAuthProvider = JWTGraphiQLAuthentication::class; |
68
|
|
|
@trigger_error('The option `graphql.graphiql.authentication.provider.jwt` has been deprecated use `graphql.graphiql.authentication.provider.lexik_jwt` instead'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($config['graphiql']['authentication']['provider']['lexik_jwt']['enabled'] ?? false) { |
72
|
|
|
$graphiQLAuthProvider = LexikJWTGraphiQLAuthenticator::class; |
73
|
|
|
if (!interface_exists(JWTTokenManagerInterface::class)) { |
74
|
|
|
throw new \InvalidArgumentException('In order to use `lexik_jwt` authentication in GraphiQL Explorer must install LexikJWTAuthenticationBundle.'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($config['graphiql']['authentication']['provider']['custom'] ?? false) { |
79
|
|
|
$graphiQLAuthProvider = $config['graphiql']['authentication']['provider']['custom']; |
80
|
|
|
} |
81
|
|
|
$container->setParameter('graphql.graphiql_auth_provider', $graphiQLAuthProvider); |
82
|
|
|
|
83
|
|
|
$configDir = __DIR__.'/../Resources/config'; |
84
|
|
|
$loader = new YamlFileLoader($container, new FileLocator($configDir)); |
85
|
|
|
$loader->load('services.yml'); |
86
|
|
|
|
87
|
|
|
if ($container->getParameter('kernel.environment') !== 'dev') { |
88
|
|
|
$container->getDefinition(DefinitionCacheWarmer::class)->clearTag('kernel.event_subscriber'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
//configure LexikJWTGraphiQLAuthenticator definition |
92
|
|
|
if ($config['graphiql']['authentication']['provider']['lexik_jwt']['enabled'] ?? false) { |
93
|
|
|
$providerName = sprintf('security.user.provider.concrete.%s', $config['graphiql']['authentication']['provider']['lexik_jwt']['user_provider']); |
94
|
|
|
$container->getDefinition(LexikJWTGraphiQLAuthenticator::class) |
95
|
|
|
->setArgument(1, new Reference($providerName)); |
96
|
|
|
} else { |
97
|
|
|
$container->removeDefinition(LexikJWTGraphiQLAuthenticator::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->setBackwardCompatibilitySettings($container, $config['bc'] ?? []); |
101
|
|
|
|
102
|
|
|
//build the ID encoder manager with configured encoder |
103
|
|
|
$container->getDefinition(IDEncoderManager::class) |
104
|
|
|
->setPublic(true) |
105
|
|
|
->replaceArgument(0, $container->getDefinition($config['id_encoder'])); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
//endpoint definition |
109
|
|
|
$container->getDefinition(GraphQLEndpointController::class) |
110
|
|
|
->addMethodCall('setErrorFormatter', [$container->getDefinition($config['error_handling']['formatter'])]) |
111
|
|
|
->addMethodCall('setErrorHandler', [$container->getDefinition($config['error_handling']['handler'])]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ContainerBuilder $container |
116
|
|
|
* @param array $config |
117
|
|
|
* |
118
|
|
|
* @throws \ReflectionException |
119
|
|
|
*/ |
120
|
|
|
public function setBackwardCompatibilitySettings(ContainerBuilder $container, array $config): void |
121
|
|
|
{ |
122
|
|
|
foreach ($container->getDefinitions() as $class => $definition) { |
123
|
|
|
if ($definition->getClass()) { |
124
|
|
|
$class = $definition->getClass(); |
125
|
|
|
} |
126
|
|
|
if (class_exists($class)) { |
127
|
|
|
$ref = new \ReflectionClass($class); |
128
|
|
|
if ($ref->implementsInterface(BackwardCompatibilityAwareInterface::class)) { |
129
|
|
|
$definition->addMethodCall('setBCConfig', [$config]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
|
|
public function getAlias() |
139
|
|
|
{ |
140
|
|
|
return 'graphql'; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|