Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 345c9ce

Browse files
authored
Use fruitcake/php-cors instead of asm89/stack-cors (#553)
* Use fruitcake/php-cors instead of asm89/stack-cors * Options has moved to php-cors * Tweak versions
1 parent 03775b0 commit 345c9ce

File tree

3 files changed

+5
-66
lines changed

3 files changed

+5
-66
lines changed

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
"php": ">=7.2",
1818
"illuminate/support": "^6|^7|^8|^9",
1919
"illuminate/contracts": "^6|^7|^8|^9",
20-
"symfony/http-foundation": "^4|^5|^6",
21-
"symfony/http-kernel": "^4.3.4|^5|^6",
22-
"asm89/stack-cors": "^2.0.1"
20+
"fruitcake/php-cors": "^0.1|^1"
2321
},
2422
"require-dev": {
2523
"laravel/framework": "^6|^7.24|^8",
@@ -39,7 +37,7 @@
3937
},
4038
"extra": {
4139
"branch-alias": {
42-
"dev-master": "2.0-dev"
40+
"dev-master": "2.1-dev"
4341
},
4442
"laravel": {
4543
"providers": [

src/CorsServiceProvider.php

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Fruitcake\Cors;
44

5-
use Asm89\Stack\CorsService;
5+
use Fruitcake\Cors\CorsService;
66
use Illuminate\Foundation\Application as LaravelApplication;
77
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
88
use 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
}

src/HandleCors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Fruitcake\Cors;
44

55
use Closure;
6-
use Asm89\Stack\CorsService;
6+
use Fruitcake\Cors\CorsService;
77
use Illuminate\Contracts\Http\Kernel;
88
use Illuminate\Foundation\Http\Events\RequestHandled;
99
use Illuminate\Http\Request;

0 commit comments

Comments
 (0)