|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Config\ServiceConfiguration; |
|
8
|
|
|
use GraphQLAPI\GraphQLAPI\Container\CompilerPasses\RegisterAccessControlRuleBlockCompilerPass; |
|
9
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\UserSettingsManagerFacade; |
|
11
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\CacheFunctionalityModuleResolver; |
|
12
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\ClientFunctionalityModuleResolver; |
|
13
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\PerformanceFunctionalityModuleResolver; |
|
14
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfiguratorExecuters\EditingPersistedQuerySchemaConfiguratorExecuter; |
|
15
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfiguratorExecuters\EndpointSchemaConfiguratorExecuter; |
|
16
|
|
|
use GraphQLAPI\GraphQLAPI\SchemaConfiguratorExecuters\PersistedQuerySchemaConfiguratorExecuter; |
|
17
|
|
|
use PoP\CacheControl\DirectiveResolvers\CacheControlDirectiveResolver; |
|
18
|
|
|
use PoP\ComponentModel\ComponentConfiguration as ComponentModelComponentConfiguration; |
|
19
|
|
|
use PoP\ComponentModel\ComponentConfiguration\ComponentConfigurationHelpers; |
|
20
|
|
|
use PoP\ComponentModel\Environment as ComponentModelEnvironment; |
|
21
|
|
|
use PoP\ComponentModel\Facades\Engine\DataloadingEngineFacade; |
|
22
|
|
|
use PoP\Root\Component\AbstractComponent; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initialize component |
|
26
|
|
|
*/ |
|
27
|
|
|
class Component extends AbstractComponent |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Classes from PoP components that must be initialized before this component |
|
31
|
|
|
* |
|
32
|
|
|
* @return string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function getDependedComponentClasses(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
1 |
|
\PoPSchema\GenericCustomPosts\Component::class, |
|
38
|
|
|
\PoPSchema\CommentMetaWP\Component::class, |
|
39
|
|
|
\GraphQLByPoP\GraphQLServer\Component::class, |
|
40
|
1 |
|
\PoPSchema\MediaWP\Component::class, |
|
41
|
|
|
\PoPSchema\PostsWP\Component::class, |
|
42
|
|
|
\PoPSchema\PagesWP\Component::class, |
|
43
|
|
|
\PoPSchema\CustomPostMediaWP\Component::class, |
|
44
|
|
|
\PoPSchema\CustomPostMetaWP\Component::class, |
|
45
|
|
|
\PoPSchema\TaxonomyQueryWP\Component::class, |
|
46
|
|
|
\PoPSchema\PostTagsWP\Component::class, |
|
47
|
|
|
\PoPSchema\UserRolesAccessControl\Component::class, |
|
48
|
|
|
\PoPSchema\UserRolesWP\Component::class, |
|
49
|
|
|
\PoPSchema\UserStateWP\Component::class, |
|
50
|
|
|
\PoPSchema\UserMetaWP\Component::class, |
|
51
|
|
|
\PoPSchema\CustomPostMutationsWP\Component::class, |
|
52
|
|
|
\PoPSchema\PostMutations\Component::class, |
|
53
|
|
|
\PoPSchema\CustomPostMediaMutationsWP\Component::class, |
|
54
|
|
|
\PoPSchema\CommentMutationsWP\Component::class, |
|
55
|
|
|
\PoPSchema\UserStateMutationsWP\Component::class, |
|
56
|
|
|
\PoPSchema\BasicDirectives\Component::class, |
|
57
|
|
|
\GraphQLByPoP\GraphQLClientsForWP\Component::class, |
|
58
|
|
|
\GraphQLByPoP\GraphQLEndpointForWP\Component::class, |
|
59
|
|
|
\GraphQLAPI\MarkdownConvertor\Component::class, |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Initialize services |
|
65
|
|
|
* |
|
66
|
|
|
* @param array<string, mixed> $configuration |
|
67
|
|
|
* @param string[] $skipSchemaComponentClasses |
|
68
|
|
|
*/ |
|
69
|
|
|
protected static function doInitialize( |
|
70
|
|
|
array $configuration = [], |
|
71
|
|
|
bool $skipSchema = false, |
|
72
|
|
|
array $skipSchemaComponentClasses = [] |
|
73
|
|
|
): void { |
|
74
|
|
|
parent::doInitialize($configuration, $skipSchema, $skipSchemaComponentClasses); |
|
75
|
|
|
self::initYAMLServices(dirname(__DIR__)); |
|
76
|
|
|
self::initComponentConfiguration(); |
|
77
|
|
|
self::initPHPServices(dirname(__DIR__)); |
|
78
|
|
|
// Override DI services |
|
79
|
|
|
self::initPHPServices(dirname(__DIR__), '/Overrides'); |
|
80
|
|
|
// Conditional DI settings |
|
81
|
|
|
/** |
|
82
|
|
|
* FieldResolvers used to configure the services can also be accessed in the admin area |
|
83
|
|
|
*/ |
|
84
|
|
|
if (\is_admin()) { |
|
85
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/Admin'); |
|
86
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/Admin', 'schema-services.php'); |
|
87
|
|
|
} |
|
88
|
|
|
// Register the Cache services, if the module is not disabled |
|
89
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
90
|
|
|
if ($moduleRegistry->isModuleEnabled(CacheFunctionalityModuleResolver::CONFIGURATION_CACHE)) { |
|
91
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/ConfigurationCache/Overrides'); |
|
92
|
|
|
} |
|
93
|
|
|
// Maybe use GraphiQL with Explorer |
|
94
|
|
|
$userSettingsManager = UserSettingsManagerFacade::getInstance(); |
|
95
|
|
|
if ($moduleRegistry->isModuleEnabled(ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER)) { |
|
96
|
|
|
if ( |
|
97
|
|
|
$userSettingsManager->getSetting( |
|
98
|
|
|
ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER, |
|
99
|
|
|
ClientFunctionalityModuleResolver::OPTION_USE_IN_ADMIN_CLIENT |
|
100
|
|
|
) |
|
101
|
|
|
) { |
|
102
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/GraphiQLExplorerInAdminClient/Overrides'); |
|
103
|
|
|
} |
|
104
|
|
|
if ( |
|
105
|
|
|
$userSettingsManager->getSetting( |
|
106
|
|
|
ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER, |
|
107
|
|
|
ClientFunctionalityModuleResolver::OPTION_USE_IN_ADMIN_PERSISTED_QUERIES |
|
108
|
|
|
) |
|
109
|
|
|
) { |
|
110
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/GraphiQLExplorerInAdminPersistedQueries/Overrides'); |
|
111
|
|
|
} |
|
112
|
|
|
if ( |
|
113
|
|
|
$userSettingsManager->getSetting( |
|
114
|
|
|
ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER, |
|
115
|
|
|
ClientFunctionalityModuleResolver::OPTION_USE_IN_PUBLIC_CLIENT_FOR_SINGLE_ENDPOINT |
|
116
|
|
|
) |
|
117
|
|
|
) { |
|
118
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/GraphiQLExplorerInSingleEndpointPublicClient/Overrides'); |
|
119
|
|
|
} |
|
120
|
|
|
if ( |
|
121
|
|
|
$userSettingsManager->getSetting( |
|
122
|
|
|
ClientFunctionalityModuleResolver::GRAPHIQL_EXPLORER, |
|
123
|
|
|
ClientFunctionalityModuleResolver::OPTION_USE_IN_PUBLIC_CLIENT_FOR_CUSTOM_ENDPOINTS |
|
124
|
|
|
) |
|
125
|
|
|
) { |
|
126
|
|
|
self::initPHPServices(dirname(__DIR__), '/ConditionalOnEnvironment/GraphiQLExplorerInCustomEndpointPublicClient/Overrides'); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
ServiceConfiguration::initialize(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected static function initComponentConfiguration(): void |
|
133
|
|
|
{ |
|
134
|
|
|
/** |
|
135
|
|
|
* Enable the schema entity registries, as to retrieve the type/directive resolver classes |
|
136
|
|
|
* from the type/directive names, saved in the DB in the ACL/CCL Custom Post Types |
|
137
|
|
|
*/ |
|
138
|
|
|
$hookName = ComponentConfigurationHelpers::getHookName( |
|
139
|
|
|
ComponentModelComponentConfiguration::class, |
|
140
|
|
|
ComponentModelEnvironment::ENABLE_SCHEMA_ENTITY_REGISTRIES |
|
141
|
|
|
); |
|
142
|
|
|
\add_filter( |
|
143
|
|
|
$hookName, |
|
144
|
|
|
fn () => true, |
|
145
|
|
|
PHP_INT_MAX, |
|
146
|
|
|
1 |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Boot component |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function boot(): void |
|
156
|
|
|
{ |
|
157
|
|
|
parent::boot(); |
|
158
|
|
|
|
|
159
|
|
|
// Enable the CacheControl, if the module is not disabled |
|
160
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
161
|
|
|
if ($moduleRegistry->isModuleEnabled(PerformanceFunctionalityModuleResolver::CACHE_CONTROL)) { |
|
162
|
|
|
// Unless previewing the query |
|
163
|
|
|
if (!\is_preview()) { |
|
164
|
|
|
$dataloadingEngine = DataloadingEngineFacade::getInstance(); |
|
165
|
|
|
$dataloadingEngine->addMandatoryDirectives([ |
|
166
|
|
|
CacheControlDirectiveResolver::getDirectiveName(), |
|
167
|
|
|
]); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Configure the GraphQL query with Access/Cache Control Lists |
|
172
|
|
|
(new PersistedQuerySchemaConfiguratorExecuter())->init(); |
|
173
|
|
|
(new EndpointSchemaConfiguratorExecuter())->init(); |
|
174
|
|
|
(new EditingPersistedQuerySchemaConfiguratorExecuter())->init(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get all the compiler pass classes required to register on the container |
|
179
|
|
|
* |
|
180
|
|
|
* @return string[] |
|
181
|
|
|
*/ |
|
182
|
|
|
public static function getContainerCompilerPassClasses(): array |
|
183
|
|
|
{ |
|
184
|
|
|
return [ |
|
185
|
|
|
RegisterAccessControlRuleBlockCompilerPass::class, |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|