22
33namespace Fruitcake \Cors ;
44
5- use Asm89 \ Stack \CorsService ;
5+ use Fruitcake \ Cors \CorsService ;
66use Illuminate \Foundation \Application as LaravelApplication ;
77use Illuminate \Support \ServiceProvider as BaseServiceProvider ;
88use Laravel \Lumen \Application as LumenApplication ;
@@ -20,7 +20,7 @@ public function register()
2020 $ this ->mergeConfigFrom ($ this ->configPath (), 'cors ' );
2121
2222 $ this ->app ->singleton (CorsService::class, function ($ app ) {
23- return new CorsService ($ this ->corsOptions (), $ app );
23+ return new CorsService ($ this ->app [ ' config ' ]-> get ( ' cors ' ) );
2424 });
2525 }
2626
@@ -53,63 +53,4 @@ protected function configPath()
5353 {
5454 return __DIR__ . '/../config/cors.php ' ;
5555 }
56-
57- /**
58- * Get options for CorsService
59- *
60- * @return array
61- */
62- protected function corsOptions ()
63- {
64- $ config = $ this ->app ['config ' ]->get ('cors ' );
65-
66- if ($ config ['exposed_headers ' ] && !is_array ($ config ['exposed_headers ' ])) {
67- throw new \RuntimeException ('CORS config `exposed_headers` should be `false` or an array ' );
68- }
69-
70- foreach (['allowed_origins ' , 'allowed_origins_patterns ' , 'allowed_headers ' , 'allowed_methods ' ] as $ key ) {
71- if (!is_array ($ config [$ key ])) {
72- throw new \RuntimeException ('CORS config ` ' . $ key . '` should be an array ' );
73- }
74- }
75-
76- // Convert case to supported options
77- $ options = [
78- 'supportsCredentials ' => $ config ['supports_credentials ' ],
79- 'allowedOrigins ' => $ config ['allowed_origins ' ],
80- 'allowedOriginsPatterns ' => $ config ['allowed_origins_patterns ' ],
81- 'allowedHeaders ' => $ config ['allowed_headers ' ],
82- 'allowedMethods ' => $ config ['allowed_methods ' ],
83- 'exposedHeaders ' => $ config ['exposed_headers ' ],
84- 'maxAge ' => $ config ['max_age ' ],
85- ];
86-
87- // Transform wildcard pattern
88- foreach ($ options ['allowedOrigins ' ] as $ origin ) {
89- if (strpos ($ origin , '* ' ) !== false ) {
90- $ options ['allowedOriginsPatterns ' ][] = $ this ->convertWildcardToPattern ($ origin );
91- }
92- }
93-
94- return $ options ;
95- }
96-
97- /**
98- * Create a pattern for a wildcard, based on Str::is() from Laravel
99- *
100- * @see https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Str.php
101- * @param string $pattern
102- * @return string
103- */
104- protected function convertWildcardToPattern ($ pattern )
105- {
106- $ pattern = preg_quote ($ pattern , '# ' );
107-
108- // Asterisks are translated into zero-or-more regular expression wildcards
109- // to make it convenient to check if the strings starts with the given
110- // pattern such as "library/*", making any string check convenient.
111- $ pattern = str_replace ('\* ' , '.* ' , $ pattern );
112-
113- return '#^ ' . $ pattern . '\z#u ' ;
114- }
11556}
0 commit comments