forked from nlemoine/page-for-custom-post-type
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.php
More file actions
209 lines (172 loc) · 7.6 KB
/
Copy pathContainer.php
File metadata and controls
209 lines (172 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace n5s\PageForCustomPostType;
use n5s\PageForCustomPostType\Admin\Admin;
use n5s\PageForCustomPostType\Admin\SettingsValidator;
use n5s\PageForCustomPostType\Core\Api;
use n5s\PageForCustomPostType\Core\RewriteManager;
use n5s\PageForCustomPostType\Frontend\Handler;
use n5s\PageForCustomPostType\Frontend\QueryFilter;
use n5s\PageForCustomPostType\Integration\AdvancedCustomFields;
use n5s\PageForCustomPostType\Integration\Autodescription;
use n5s\PageForCustomPostType\Integration\Polylang;
use n5s\PageForCustomPostType\Integration\WordPressSeo;
use n5s\PageForCustomPostType\Integration\Wpml;
use n5s\PageForCustomPostType\Lifecycle\LifecycleManager;
use n5s\PageForCustomPostType\Lifecycle\Migrator;
use n5s\PageForCustomPostType\PostType\PostType;
use WP_Query;
use wpdb;
/**
* Dependency injection container.
*/
final class Container
{
/** @var array<class-string, object> */
private array $services = [];
/** @var array<class-string, callable(): object> */
private array $factories;
public function __construct()
{
$this->factories = [
// WP
wpdb::class => static function (): wpdb {
$wpdb = $GLOBALS['wpdb'];
if (!$wpdb instanceof wpdb) {
throw new \RuntimeException('Global $wpdb is not available.');
}
return $wpdb;
},
WP_Query::class => static function (): WP_Query {
$wpQuery = $GLOBALS['wp_query'] ?? null;
if (!$wpQuery instanceof WP_Query) {
throw new \RuntimeException('Global $wp_query is not available.');
}
return $wpQuery;
},
// Core services (no dependencies)
Api::class => static fn (): Api => new Api(),
// Services with single dependency
RewriteManager::class => fn (): RewriteManager => new RewriteManager(
$this->get(Api::class)
),
SettingsValidator::class => static fn (): SettingsValidator => new SettingsValidator(),
Migrator::class => static fn (): Migrator => new Migrator(),
Handler::class => fn (): Handler => new Handler(
$this->get(Api::class)
),
QueryFilter::class => fn (): QueryFilter => new QueryFilter(
$this->get(Api::class),
$this->get(wpdb::class)
),
// Services with multiple dependencies
PostType::class => fn (): PostType => new PostType(
$this->get(Api::class),
$this->get(RewriteManager::class)
),
LifecycleManager::class => fn (): LifecycleManager => new LifecycleManager(
$this->get(Api::class),
$this->get(RewriteManager::class),
$this->get(SettingsValidator::class)
),
Admin::class => fn (): Admin => new Admin(
$this->get(Api::class),
$this->get(PostType::class)
),
// Polylang integrations
Polylang\UrlTranslation::class => static fn (): Polylang\UrlTranslation => new Polylang\UrlTranslation(),
Polylang\Translation::class => static fn (): Polylang\Translation => new Polylang\Translation(),
Polylang\SlugTranslation::class => fn (): Polylang\SlugTranslation => new Polylang\SlugTranslation(
$this->get(Api::class),
$this->get(RewriteManager::class)
),
Polylang\Admin::class => static fn (): Polylang\Admin => new Polylang\Admin(),
Polylang\Lifecycle::class => fn (): Polylang\Lifecycle => new Polylang\Lifecycle(
$this->get(Api::class),
$this->get(RewriteManager::class)
),
// WPML integrations
Wpml\Translation::class => static fn (): Wpml\Translation => new Wpml\Translation(),
Wpml\UrlTranslation::class => fn (): Wpml\UrlTranslation => new Wpml\UrlTranslation(
$this->get(Api::class)
),
Wpml\Admin::class => static fn (): Wpml\Admin => new Wpml\Admin(),
Wpml\Lifecycle::class => fn (): Wpml\Lifecycle => new Wpml\Lifecycle(
$this->get(Api::class),
$this->get(RewriteManager::class),
$this->get(Wpml\Translation::class)
),
Wpml\Wpml::class => fn (): Wpml\Wpml => new Wpml\Wpml(
$this->get(Wpml\Translation::class),
$this->get(Wpml\UrlTranslation::class),
$this->get(Wpml\Admin::class),
$this->get(Wpml\Lifecycle::class)
),
// WordPressSeo integrations
WordPressSeo\Schema::class => fn (): WordPressSeo\Schema => new WordPressSeo\Schema(
$this->get(Api::class)
),
WordPressSeo\Breadcrumbs::class => fn (): WordPressSeo\Breadcrumbs => new WordPressSeo\Breadcrumbs(
$this->get(Api::class)
),
WordPressSeo\Indexables::class => fn (): WordPressSeo\Indexables => new WordPressSeo\Indexables(
$this->get(Api::class)
),
// Integration composites
AdvancedCustomFields\AdvancedCustomFields::class => fn (): AdvancedCustomFields\AdvancedCustomFields => new AdvancedCustomFields\AdvancedCustomFields(
$this->get(Api::class)
),
Polylang\Polylang::class => fn (): Polylang\Polylang => new Polylang\Polylang(
$this->get(Polylang\UrlTranslation::class),
$this->get(Polylang\Translation::class),
$this->get(Polylang\SlugTranslation::class),
$this->get(Polylang\Admin::class),
$this->get(Polylang\Lifecycle::class)
),
WordPressSeo\WordPressSeo::class => fn (): WordPressSeo\WordPressSeo => new WordPressSeo\WordPressSeo(
$this->get(WordPressSeo\Schema::class),
$this->get(WordPressSeo\Breadcrumbs::class),
$this->get(WordPressSeo\Indexables::class)
),
// Autodescription (The SEO Framework) integrations
Autodescription\QueryType::class => fn (): Autodescription\QueryType => new Autodescription\QueryType(
$this->get(Api::class)
),
Autodescription\Breadcrumbs::class => fn (): Autodescription\Breadcrumbs => new Autodescription\Breadcrumbs(
$this->get(Api::class)
),
Autodescription\Autodescription::class => fn (): Autodescription\Autodescription => new Autodescription\Autodescription(
$this->get(Autodescription\QueryType::class),
$this->get(Autodescription\Breadcrumbs::class)
),
];
}
/**
* Get a service by class name.
*
* @template T of object
* @param class-string<T> $name
* @return T
*/
public function get(string $name): object
{
if (!isset($this->services[$name])) {
if (!isset($this->factories[$name])) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
throw new \InvalidArgumentException("Unknown service: {$name}");
}
$this->services[$name] = $this->factories[$name]();
}
/** @var T */
return $this->services[$name];
}
/**
* Check if a service exists.
*
* @param class-string $name
*/
public function has(string $name): bool
{
return isset($this->factories[$name]);
}
}