Skip to content

Commit f4d5ef0

Browse files
committed
Initial commit, standalone split-off from asm89/stack-cors
0 parents  commit f4d5ef0

File tree

12 files changed

+1165
-0
lines changed

12 files changed

+1165
-0
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.github export-ignore
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
phpunit.xml.dist export-ignore
8+
CHANGELOG.md export-ignore

.github/workflows/run-tests.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
on:
2+
push:
3+
pull_request:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
jobs:
8+
php-tests:
9+
runs-on: ${{ matrix.os }}
10+
11+
strategy:
12+
matrix:
13+
php: [7.2, 7.3, 7.4, 8.0, 8.1]
14+
symfony: [4.x, 5.x, 6.x]
15+
dependency-version: [prefer-lowest, prefer-stable]
16+
os: [ubuntu-latest]
17+
exclude:
18+
- symfony: 6.x
19+
php: 7.2
20+
- symfony: 6.x
21+
php: 7.3
22+
- symfony: 6.x
23+
php: 7.4
24+
25+
name: PHP${{ matrix.php }} Symfony${{ matrix.symfony }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
26+
27+
steps:
28+
- name: Checkout code
29+
uses: actions/checkout@v1
30+
31+
- name: Setup PHP
32+
uses: shivammathur/setup-php@v2
33+
with:
34+
php-version: ${{ matrix.php }}
35+
extensions: mbstring
36+
37+
- name: Install dependencies
38+
run: |
39+
composer require "symfony/http-foundation:${{ matrix.symfony }}" --no-interaction --no-update
40+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest --with-all-dependencies
41+
42+
- name: Execute Unit Tests
43+
run: vendor/bin/phpunit
44+
45+
- name: Check PSR-12 Codestyle
46+
run: vendor/bin/phpcs --standard=psr12 --exclude=Generic.Files.LineLength src/
47+
if: matrix.os == 'ubuntu-latest'

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
phpunit.xml
2+
composer.lock
3+
composer.phar
4+
/vendor/
5+
.phpunit.result.cache

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Changed since split from asm89/stack-cors
11+
12+
- Renamed Asm89\Stack namespace to Fruitcake\Cors
13+
- Removed HttpKernel middleware

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2013-2017 Alexander <iam.asm89@gmail.com>
2+
Copyright (c) 2017-2022 Barryvdh <barryvdh@gmail.com>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is furnished
9+
to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Stack/Cors
2+
## Stand-alone fork of https://github.com/asm89/stack-cors
3+
4+
[![Tests](https://github.com/fruitcake/php-cors/actions/workflows/run-tests.yml/badge.svg)](https://github.com/fruitcake/php-cors/actions/workflows/run-tests.yml)
5+
[![Packagist License](https://poser.pugx.org/fruitcake/php-corsr/license.png)](http://choosealicense.com/licenses/mit/)
6+
[![Latest Stable Version](https://poser.pugx.org/fruitcake/php-cors/version.png)](https://packagist.org/packages/fruitcake/php-cors)
7+
[![Total Downloads](https://poser.pugx.org/fruitcake/php-cors/d/total.png)](https://packagist.org/packages/fruitcake/php-cors)
8+
[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](https://fruitcake.nl/)
9+
10+
Library and middleware enabling cross-origin resource sharing for your
11+
http-{foundation,kernel} using application. It attempts to implement the
12+
[W3C Recommendation] for cross-origin resource sharing.
13+
14+
[W3C Recommendation]: http://www.w3.org/TR/cors/
15+
16+
Build status: ![.github/workflows/run-tests.yml](https://github.com/asm89/stack-cors/workflows/.github/workflows/run-tests.yml/badge.svg)
17+
18+
## Installation
19+
20+
Require `fruitcake/php-cors` using composer.
21+
22+
## Usage
23+
24+
This package can be used as a library. You can use it in your framework using:
25+
26+
- [Stack middleware](http://stackphp.com/): https://github.com/asm89/stack-cors
27+
- [Laravel](https://laravel.com): https://github.com/fruitcake/laravel-cors
28+
29+
30+
### Options
31+
32+
| Option | Description | Default value |
33+
|------------------------|------------------------------------------------------------|---------------|
34+
| allowedMethods | Matches the request method. | `[]` |
35+
| allowedOrigins | Matches the request origin. | `[]` |
36+
| allowedOriginsPatterns | Matches the request origin with `preg_match`. | `[]` |
37+
| allowedHeaders | Sets the Access-Control-Allow-Headers response header. | `[]` |
38+
| exposedHeaders | Sets the Access-Control-Expose-Headers response header. | `false` |
39+
| maxAge | Sets the Access-Control-Max-Age response header. | `false` |
40+
| supportsCredentials | Sets the Access-Control-Allow-Credentials header. | `false` |
41+
42+
The _allowedMethods_ and _allowedHeaders_ options are case-insensitive.
43+
44+
You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin.
45+
46+
If `['*']` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed.
47+
48+
### Example: using the library
49+
50+
```php
51+
<?php
52+
53+
use Fruitcake\Cors\CorsService;
54+
55+
$cors = new CorsService([
56+
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
57+
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
58+
'allowedOrigins' => ['http://localhost'],
59+
'allowedOriginsPatterns' => ['/localhost:\d/'],
60+
'exposedHeaders' => false,
61+
'maxAge' => false,
62+
'supportsCredentials' => false,
63+
]);
64+
65+
$cors->addActualRequestHeaders(Response $response, $origin);
66+
$cors->handlePreflightRequest(Request $request);
67+
$cors->isActualRequestAllowed(Request $request);
68+
$cors->isCorsRequest(Request $request);
69+
$cors->isPreflightRequest(Request $request);
70+
```
71+
72+
## Example: using the stack middleware
73+
74+
```php
75+
<?php
76+
77+
use Fruitcake\Cors\Cors;
78+
79+
$app = new Cors($app, [
80+
// you can use ['*'] to allow any headers
81+
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
82+
// you can use ['*'] to allow any methods
83+
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
84+
// you can use ['*'] to allow requests from any origin
85+
'allowedOrigins' => ['localhost'],
86+
// you can enter regexes that are matched to the origin request header
87+
'allowedOriginsPatterns' => ['/localhost:\d/'],
88+
'exposedHeaders' => false,
89+
'maxAge' => false,
90+
'supportsCredentials' => false,
91+
]);
92+
```
93+
94+
## License
95+
96+
Released under the MIT License, see [LICENSE](LICENSE).
97+
The original author of this Library is Alexander <iam.asm89@gmail.com>, while Barry <barryvdh@gmail.com> has been involved since 2015.
98+
This package is split-off from https://github.com/asm89/stack-cors

composer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "fruitcake/php-cors",
3+
"description": "Cross-origin resource sharing library for the Symfony HttpFoundation",
4+
"keywords": ["cors", "symfony", "laravel"],
5+
"homepage": "https://github.com/fruitcake/php-cors",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Barryvdh",
11+
"email": "barryvdh@gmail.com"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.2|^8.0",
16+
"symfony/http-foundation": "^4|^5|^6"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^7|^9",
20+
"squizlabs/php_codesniffer": "^3.5"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Fruitcake\\Cors\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Fruitcake\\Cors\\Tests\\": "tests/"
30+
}
31+
},
32+
"scripts": {
33+
"test": "phpunit",
34+
"check-style": "phpcs -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src",
35+
"fix-style": "phpcbf -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src"
36+
},
37+
"extra": {
38+
"branch-alias": {
39+
"dev-master": "1.0-dev"
40+
}
41+
}
42+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Fruitcake Cors Test Suite">
14+
<directory>./tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">./src/</directory>
21+
</whitelist>
22+
</filter>
23+
24+
</phpunit>

0 commit comments

Comments
 (0)