1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Exception; |
17
|
|
|
use RuntimeException; |
18
|
|
|
use Silverback\ApiComponentBundle\Doctrine\Extension\TablePrefixExtension; |
19
|
|
|
use Silverback\ApiComponentBundle\Entity\Core\ComponentInterface; |
20
|
|
|
use Silverback\ApiComponentBundle\EventListener\Doctrine\UserListener; |
21
|
|
|
use Silverback\ApiComponentBundle\Extension\PublishableExtension; |
22
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\ChangeEmailVerificationEmailFactory; |
23
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordChangedEmailFactory; |
24
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\PasswordResetEmailFactory; |
25
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\UserEnabledEmailFactory; |
26
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\UsernameChangedEmailFactory; |
27
|
|
|
use Silverback\ApiComponentBundle\Factory\Mailer\User\WelcomeEmailFactory; |
28
|
|
|
use Silverback\ApiComponentBundle\Factory\User\UserFactory; |
29
|
|
|
use Silverback\ApiComponentBundle\Form\FormTypeInterface; |
30
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\ChangePasswordType; |
31
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\NewEmailAddressType; |
32
|
|
|
use Silverback\ApiComponentBundle\Form\Type\User\UserRegisterType; |
33
|
|
|
use Silverback\ApiComponentBundle\Mailer\UserMailer; |
34
|
|
|
use Silverback\ApiComponentBundle\Manager\User\PasswordManager; |
35
|
|
|
use Silverback\ApiComponentBundle\Repository\User\UserRepository; |
36
|
|
|
use Silverback\ApiComponentBundle\Security\TokenAuthenticator; |
37
|
|
|
use Silverback\ApiComponentBundle\Security\UserChecker; |
38
|
|
|
use Symfony\Component\Config\FileLocator; |
39
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
40
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
41
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
42
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @author Daniel West <[email protected]> |
46
|
|
|
*/ |
47
|
|
|
class SilverbackApiComponentExtension extends Extension implements PrependExtensionInterface |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
1 |
|
public function load(array $configs, ContainerBuilder $container): void |
53
|
|
|
{ |
54
|
1 |
|
$configuration = new Configuration(); |
55
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
56
|
|
|
|
57
|
1 |
|
$this->loadServiceConfig($container); |
58
|
|
|
|
59
|
1 |
|
$definition = $container->getDefinition(TablePrefixExtension::class); |
60
|
1 |
|
$definition->setArgument('$prefix', $config['table_prefix']); |
61
|
|
|
|
62
|
1 |
|
$definition = $container->getDefinition(TokenAuthenticator::class); |
63
|
1 |
|
$definition->setArgument('$tokens', $config['security']['tokens']); |
64
|
|
|
|
65
|
1 |
|
$definition = $container->getDefinition(PasswordManager::class); |
66
|
1 |
|
$definition->setArgument('$tokenTtl', $config['user']['password_reset']['repeat_ttl_seconds']); |
67
|
|
|
|
68
|
1 |
|
$definition = $container->getDefinition(UserRepository::class); |
69
|
1 |
|
$definition->setArgument('$passwordRequestTimeout', $config['user']['password_reset']['request_timeout_seconds']); |
70
|
1 |
|
$definition->setArgument('$entityClass', $config['user']['class_name']); |
71
|
|
|
|
72
|
1 |
|
$definition = $container->getDefinition(PublishableExtension::class); |
73
|
1 |
|
$definition->setArgument('$permission', $config['publishable']['permission']); |
74
|
|
|
|
75
|
1 |
|
$this->setEmailVerificationArguments($container, $config['user']['email_verification']); |
76
|
1 |
|
$this->setUserClassArguments($container, $config['user']['class_name']); |
77
|
1 |
|
$this->setMailerServiceArguments($container, $config); |
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
private function setEmailVerificationArguments(ContainerBuilder $container, array $emailVerificationConfig): void |
81
|
|
|
{ |
82
|
1 |
|
$definition = $container->getDefinition(UserChecker::class); |
83
|
1 |
|
$definition->setArgument('$denyUnverifiedLogin', $emailVerificationConfig['deny_unverified_login']); |
84
|
|
|
|
85
|
1 |
|
$definition = $container->getDefinition(UserListener::class); |
86
|
1 |
|
$definition->setArgument('$initialEmailVerifiedState', $emailVerificationConfig['default_value']); |
87
|
1 |
|
$definition->setArgument('$verifyEmailOnRegister', $emailVerificationConfig['verify_on_register']); |
88
|
1 |
|
$definition->setArgument('$verifyEmailOnChange', $emailVerificationConfig['verify_on_change']); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
1 |
|
private function setUserClassArguments(ContainerBuilder $container, string $userClass): void |
92
|
|
|
{ |
93
|
1 |
|
$definition = $container->getDefinition(UserFactory::class); |
94
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
95
|
|
|
|
96
|
1 |
|
$definition = $container->getDefinition(ChangePasswordType::class); |
97
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
98
|
|
|
|
99
|
1 |
|
$definition = $container->getDefinition(NewEmailAddressType::class); |
100
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
101
|
|
|
|
102
|
1 |
|
$definition = $container->getDefinition(UserRegisterType::class); |
103
|
1 |
|
$definition->setArgument('$userClass', $userClass); |
104
|
1 |
|
} |
105
|
|
|
|
106
|
1 |
|
private function setMailerServiceArguments(ContainerBuilder $container, array $config): void |
107
|
|
|
{ |
108
|
1 |
|
$definition = $container->getDefinition(UserMailer::class); |
109
|
1 |
|
$definition->setArgument('$context', [ |
110
|
1 |
|
'website_name' => $config['website_name'], |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$mapping = [ |
114
|
1 |
|
PasswordChangedEmailFactory::class => 'password_changed', |
115
|
|
|
UserEnabledEmailFactory::class => 'user_enabled', |
116
|
|
|
UsernameChangedEmailFactory::class => 'username_changed', |
117
|
|
|
WelcomeEmailFactory::class => 'welcome', |
118
|
|
|
]; |
119
|
1 |
|
foreach ($mapping as $class => $key) { |
120
|
1 |
|
$definition = $container->getDefinition($class); |
121
|
1 |
|
$definition->setArgument('$subject', $config['user']['emails'][$key]['subject']); |
122
|
1 |
|
$definition->setArgument('$enabled', $config['user']['emails'][$key]['enabled']); |
123
|
1 |
|
if (WelcomeEmailFactory::class === $class) { |
124
|
1 |
|
$definition->setArgument('$defaultRedirectPath', $config['user']['email_verification']['email']['default_redirect_path']); |
125
|
1 |
|
$definition->setArgument('$redirectPathQueryKey', $config['user']['email_verification']['email']['redirect_path_query']); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$mapping = [ |
130
|
1 |
|
ChangeEmailVerificationEmailFactory::class => 'email_verification', |
131
|
|
|
PasswordResetEmailFactory::class => 'password_reset', |
132
|
|
|
]; |
133
|
1 |
|
foreach ($mapping as $class => $key) { |
134
|
1 |
|
$definition = $container->getDefinition($class); |
135
|
1 |
|
$definition->setArgument('$subject', $config['user'][$key]['email']['subject']); |
136
|
1 |
|
$definition->setArgument('$enabled', true); |
137
|
1 |
|
$definition->setArgument('$defaultRedirectPath', $config['user'][$key]['email']['default_redirect_path']); |
138
|
1 |
|
$definition->setArgument('$redirectPathQueryKey', $config['user'][$key]['email']['redirect_path_query']); |
139
|
|
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws Exception |
144
|
|
|
*/ |
145
|
1 |
|
private function loadServiceConfig(ContainerBuilder $container): void |
146
|
|
|
{ |
147
|
1 |
|
$container->registerForAutoconfiguration(FormTypeInterface::class) |
148
|
1 |
|
->addTag('silverback_api_component.form_type'); |
149
|
|
|
|
150
|
1 |
|
$container->registerForAutoconfiguration(ComponentInterface::class) |
151
|
1 |
|
->addTag('silverback_api_component.entity.component'); |
152
|
|
|
|
153
|
1 |
|
$loader = new PhpFileLoader( |
154
|
1 |
|
$container, |
155
|
1 |
|
new FileLocator(__DIR__ . '/../Resources/config') |
156
|
|
|
); |
157
|
1 |
|
$loader->load('services.php'); |
158
|
1 |
|
} |
159
|
|
|
|
160
|
1 |
|
public function prepend(ContainerBuilder $container): void |
161
|
|
|
{ |
162
|
1 |
|
$configs = $container->getExtensionConfig($this->getAlias()); |
163
|
1 |
|
$configuration = new Configuration(); |
164
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
165
|
1 |
|
$srcBase = __DIR__ . '/..'; |
166
|
1 |
|
$configBasePath = $srcBase . '/Resources/config/api_platform'; |
167
|
1 |
|
$mappingPaths = [$srcBase . '/Entity/Core']; |
168
|
1 |
|
foreach ($config['enabled_components'] as $key => $enabled_component) { |
169
|
1 |
|
if (true === $enabled_component) { |
170
|
1 |
|
$mappingPaths[] = sprintf('%s/%s.yaml', $configBasePath, $key); |
171
|
|
|
} |
172
|
|
|
} |
173
|
1 |
|
$websiteName = $config['website_name']; |
174
|
1 |
|
$container->prependExtensionConfig( |
175
|
1 |
|
'api_platform', |
176
|
|
|
[ |
177
|
1 |
|
'title' => $websiteName, |
178
|
1 |
|
'description' => sprintf('API for %s', $websiteName), |
179
|
|
|
'collection' => [ |
180
|
|
|
'pagination' => [ |
181
|
|
|
'client_items_per_page' => true, |
182
|
|
|
'items_per_page_parameter_name' => 'perPage', |
183
|
|
|
'maximum_items_per_page' => 100, |
184
|
|
|
], |
185
|
|
|
], |
186
|
|
|
'mapping' => [ |
187
|
1 |
|
'paths' => $mappingPaths, |
188
|
|
|
], |
189
|
|
|
'swagger' => [ |
190
|
|
|
'api_keys' => [ |
191
|
|
|
'API Token' => [ |
192
|
|
|
'name' => 'X-AUTH-TOKEN', |
193
|
|
|
'type' => 'header', |
194
|
|
|
], |
195
|
|
|
'JWT (use prefix `Bearer`)' => [ |
196
|
|
|
'name' => 'Authorization', |
197
|
|
|
'type' => 'header', |
198
|
|
|
], |
199
|
|
|
], |
200
|
|
|
], |
201
|
|
|
] |
202
|
|
|
); |
203
|
|
|
|
204
|
1 |
|
$bundles = $container->getParameter('kernel.bundles'); |
205
|
1 |
|
if (isset($bundles['DoctrineBundle'])) { |
206
|
1 |
|
$this->prependDoctrineConfig($container); |
207
|
|
|
} |
208
|
1 |
|
if (isset($bundles['LiipImagineBundle'])) { |
209
|
1 |
|
$this->prependLiipConfig($container); |
210
|
|
|
} |
211
|
1 |
|
} |
212
|
|
|
|
213
|
1 |
|
private function prependDoctrineConfig(ContainerBuilder $container) |
214
|
|
|
{ |
215
|
1 |
|
$container->prependExtensionConfig( |
216
|
1 |
|
'doctrine', |
217
|
|
|
[ |
218
|
|
|
// 'orm' => [ |
219
|
|
|
// 'filters' => [ |
220
|
|
|
// 'publishable' => [ |
221
|
|
|
// 'class' => PublishableFilter::class, |
222
|
|
|
// 'enabled' => false |
223
|
|
|
// ] |
224
|
|
|
// ] |
225
|
|
|
// ] |
226
|
1 |
|
] |
227
|
|
|
); |
228
|
1 |
|
} |
229
|
|
|
|
230
|
1 |
|
private function prependLiipConfig(ContainerBuilder $container) |
231
|
|
|
{ |
232
|
1 |
|
$projectDir = $container->getParameter('kernel.project_dir'); |
233
|
1 |
|
$uploadsDir = $projectDir . '/var/uploads'; |
234
|
1 |
|
if (!@mkdir($uploadsDir) && !is_dir($uploadsDir)) { |
235
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $uploadsDir)); |
236
|
|
|
} |
237
|
1 |
|
$container->prependExtensionConfig( |
238
|
1 |
|
'liip_imagine', |
239
|
|
|
[ |
240
|
|
|
'loaders' => [ |
241
|
|
|
'default' => [ |
242
|
|
|
'filesystem' => [ |
243
|
|
|
'data_root' => [ |
244
|
1 |
|
'uploads' => $uploadsDir, |
245
|
1 |
|
'default' => $projectDir . '/public', |
246
|
|
|
], |
247
|
|
|
], |
248
|
|
|
], |
249
|
|
|
], |
250
|
|
|
'filter_sets' => [ |
251
|
|
|
'placeholder_square' => [ |
252
|
|
|
'jpeg_quality' => 10, |
253
|
|
|
'png_compression_level' => 9, |
254
|
|
|
'filters' => [ |
255
|
|
|
'thumbnail' => [ |
256
|
|
|
'size' => [80, 80], |
257
|
|
|
'mode' => 'outbound', |
258
|
|
|
], |
259
|
|
|
], |
260
|
|
|
], |
261
|
|
|
'placeholder' => [ |
262
|
|
|
'jpeg_quality' => 10, |
263
|
|
|
'png_compression_level' => 9, |
264
|
|
|
'filters' => [ |
265
|
|
|
'thumbnail' => [ |
266
|
|
|
'size' => [100, 100], |
267
|
|
|
'mode' => 'inset', |
268
|
|
|
], |
269
|
|
|
], |
270
|
|
|
], |
271
|
|
|
'thumbnail' => [ |
272
|
|
|
'jpeg_quality' => 100, |
273
|
|
|
'png_compression_level' => 0, |
274
|
|
|
'filters' => [ |
275
|
|
|
'upscale' => [ |
276
|
|
|
'min' => [636, 636], |
277
|
|
|
], |
278
|
|
|
'thumbnail' => [ |
279
|
|
|
'size' => [636, 636], |
280
|
|
|
'mode' => 'inset', |
281
|
|
|
'allow_upscale' => true, |
282
|
|
|
], |
283
|
|
|
], |
284
|
|
|
], |
285
|
|
|
], |
286
|
|
|
] |
287
|
|
|
); |
288
|
1 |
|
} |
289
|
|
|
} |
290
|
|
|
|