88use InvalidArgumentException ;
99use Symfony \Component \Config \FileLocator ;
1010use Symfony \Component \DependencyInjection \ContainerBuilder ;
11+ use Symfony \Component \DependencyInjection \Extension \PrependExtensionInterface ;
1112use Symfony \Component \DependencyInjection \Loader \YamlFileLoader ;
1213use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
1314
1617 *
1718 * @author Oliver Klee <oliver@phplist.com>
1819 */
19- class PhpListRestExtension extends Extension
20+ class PhpListRestExtension extends Extension implements PrependExtensionInterface
2021{
2122 /**
2223 * Loads a specific configuration.
@@ -35,4 +36,34 @@ public function load(array $configs, ContainerBuilder $container): void
3536 $ loader = new YamlFileLoader ($ container , new FileLocator (__DIR__ . '/../../config ' ));
3637 $ loader ->load ('services.yml ' );
3738 }
39+
40+ /**
41+ * @param ContainerBuilder $container
42+ *
43+ * @return void
44+ */
45+ public function prepend (ContainerBuilder $ container ): void
46+ {
47+ $ frontendBaseUrl = $ container ->getParameter ('app.frontend_base_url ' );
48+
49+ $ container ->prependExtensionConfig ('nelmio_cors ' , [
50+ 'defaults ' => [
51+ 'allow_origin ' => [$ frontendBaseUrl ],
52+ 'allow_methods ' => ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' , 'OPTIONS ' ],
53+ 'allow_headers ' => ['Content-Type ' , 'Authorization ' , 'Origin ' , 'Accept ' , 'php-auth-pw ' ],
54+ 'expose_headers ' => ['X-Content-Type-Options ' , 'Content-Security-Policy ' , 'X-Frame-Options ' ],
55+ 'max_age ' => 3600 ,
56+ ],
57+ 'paths ' => [
58+ '^/api/v2 ' => [
59+ 'origin_regex ' => true ,
60+ 'allow_origin ' => [$ frontendBaseUrl ],
61+ 'allow_methods ' => ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' , 'OPTIONS ' ],
62+ 'allow_headers ' => ['Content-Type ' , 'Authorization ' , 'Origin ' , 'Accept ' , 'php-auth-pw ' ],
63+ 'expose_headers ' => ['X-Content-Type-Options ' , 'Content-Security-Policy ' , 'X-Frame-Options ' ],
64+ 'max_age ' => 3600 ,
65+ ],
66+ ],
67+ ]);
68+ }
3869}
0 commit comments