Skip to content

Commit 25dddd4

Browse files
committed
Merge branch 'main'
2 parents b265d21 + 41c9b00 commit 25dddd4

File tree

13 files changed

+619
-208
lines changed

13 files changed

+619
-208
lines changed

.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: barryvdh
4+
custom: ['https://fruitcake.nl']

.github/workflows/run-coverage.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Code Coverage
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
php-tests:
11+
runs-on: ubuntu-latest
12+
13+
name: PHP Code Coverage
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v1
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.1
23+
extensions: mbstring
24+
coverage: pcov
25+
26+
- name: Install dependencies
27+
run: |
28+
composer update --prefer-dist --no-interaction --no-suggest --with-all-dependencies
29+
30+
- name: Check Code Coverage
31+
run: vendor/bin/phpunit --verbose --coverage-text --coverage-clover=clover.xml --coverage-html=coverage
32+
33+
- uses: actions/upload-artifact@v2
34+
with:
35+
name: coverage
36+
path: coverage

.github/workflows/run-matcher.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
php-tests:
11+
runs-on: ubuntu-latest
12+
13+
name: PHPUnit Matcher
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v1
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: 8.1
23+
extensions: mbstring
24+
25+
- name: Configure matchers
26+
uses: mheap/phpunit-matcher-action@v1
27+
28+
- name: Install dependencies
29+
run: |
30+
composer update --prefer-dist --no-interaction --no-suggest --with-all-dependencies
31+
32+
- name: Execute Unit Tests
33+
run: vendor/bin/phpunit --teamcity

.github/workflows/run-tests.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: Unit Tests
2+
13
on:
24
push:
35
pull_request:
@@ -10,16 +12,12 @@ jobs:
1012

1113
strategy:
1214
matrix:
13-
php: [7.2, 7.3, 7.4, 8.0, 8.1]
14-
symfony: [4.x, 5.x, 6.x]
15+
php: [7.4, 8.0, 8.1]
16+
symfony: [^4.4, ^5.4, ^6]
1517
dependency-version: [prefer-lowest, prefer-stable]
1618
os: [ubuntu-latest]
1719
exclude:
18-
- symfony: 6.x
19-
php: 7.2
20-
- symfony: 6.x
21-
php: 7.3
22-
- symfony: 6.x
20+
- symfony: ^6
2321
php: 7.4
2422

2523
name: PHP${{ matrix.php }} Symfony${{ matrix.symfony }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
@@ -34,14 +32,21 @@ jobs:
3432
php-version: ${{ matrix.php }}
3533
extensions: mbstring
3634

35+
- name: Configure matchers
36+
uses: mheap/phpunit-matcher-action@v1
37+
3738
- name: Install dependencies
3839
run: |
3940
composer require "symfony/http-foundation:${{ matrix.symfony }}" --no-interaction --no-update
4041
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest --with-all-dependencies
4142
4243
- name: Execute Unit Tests
43-
run: vendor/bin/phpunit
44+
run: composer test
45+
46+
- name: Analyse with PHPStan
47+
run: composer analyse
48+
if: matrix.os == 'ubuntu-latest' && matrix.symfony != '^4.4'
4449

4550
- name: Check PSR-12 Codestyle
46-
run: vendor/bin/phpcs --standard=psr12 --exclude=Generic.Files.LineLength src/
47-
if: matrix.os == 'ubuntu-latest'
51+
run: composer test
52+
if: matrix.os == 'ubuntu-latest'

CHANGELOG.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,27 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
8+
## 1.2.0 (2022-02-20)
9+
10+
### Changed
11+
- Drop support for PHP 7.2/7.3 and legacy Symfony HttpFoundation versions.
12+
- Used TypedProperties instead of manual validation
13+
14+
## 1.1.0 (2022-02-20)
15+
16+
### Added
17+
- setOptions() method to overwrite or set the options later.
18+
19+
## 1.0.0 (2020-02-19)
920

1021
### Changed since split from asm89/stack-cors
1122

1223
- Renamed Asm89\Stack namespace to Fruitcake\Cors
13-
- Removed HttpKernel middleware
24+
- Removed HttpKernel middleware
25+
26+
### New Features
27+
- Allow underscore options (both `allowed_origins` and `allowedOrigins` are allowed)
28+
- Support wildcard patterns on AllowedOrigins (eg `https://*.example.com` will be converted to an allowedOriginPattern)
29+
- Validate input (so invalid options are caught immediately)
30+
- Bump PHPStan to Level 9
31+
- Ensure 100% Code Coverage

README.md

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Stack/Cors
2-
## Stand-alone fork of https://github.com/asm89/stack-cors
1+
# CORS for PHP (using the Symfony HttpFoundation)
32

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/)
3+
[![Unit Tests](https://github.com/fruitcake/php-cors/actions/workflows/run-tests.yml/badge.svg)](https://github.com/fruitcake/php-cors/actions)
4+
[![PHPStan Level 9](https://img.shields.io/badge/PHPStan-Level%209-blue)](https://github.com/fruitcake/php-cors/actions)
5+
[![Code Coverage](https://img.shields.io/badge/CodeCoverage-100%25-brightgreen)](https://github.com/fruitcake/php-cors/actions/workflows/run-coverage.yml)
6+
[![Packagist License](https://poser.pugx.org/fruitcake/php-cors/license.png)](http://choosealicense.com/licenses/mit/)
67
[![Latest Stable Version](https://poser.pugx.org/fruitcake/php-cors/version.png)](https://packagist.org/packages/fruitcake/php-cors)
78
[![Total Downloads](https://poser.pugx.org/fruitcake/php-cors/d/total.png)](https://packagist.org/packages/fruitcake/php-cors)
89
[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](https://fruitcake.nl/)
@@ -13,8 +14,7 @@ http-{foundation,kernel} using application. It attempts to implement the
1314

1415
[W3C Recommendation]: http://www.w3.org/TR/cors/
1516

16-
Build status: ![.github/workflows/run-tests.yml](https://github.com/asm89/stack-cors/workflows/.github/workflows/run-tests.yml/badge.svg)
17-
17+
> Note: This is a standalone fork of https://github.com/asm89/stack-cors and is compatible with the options for CorsService.
1818
## Installation
1919

2020
Require `fruitcake/php-cors` using composer.
@@ -35,16 +35,18 @@ This package can be used as a library. You can use it in your framework using:
3535
| allowedOrigins | Matches the request origin. | `[]` |
3636
| allowedOriginsPatterns | Matches the request origin with `preg_match`. | `[]` |
3737
| 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` |
38+
| exposedHeaders | Sets the Access-Control-Expose-Headers response header. | `[]` |
39+
| maxAge | Sets the Access-Control-Max-Age response header. | `0` |
4040
| supportsCredentials | Sets the Access-Control-Allow-Credentials header. | `false` |
4141

4242
The _allowedMethods_ and _allowedHeaders_ options are case-insensitive.
4343

44-
You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin.
44+
You don't need to provide both _allowedOrigins_ and _allowedOriginsPatterns_. If one of the strings passed matches, it is considered a valid origin. A wildcard in allowedOrigins will be converted to a pattern.
4545

4646
If `['*']` is provided to _allowedMethods_, _allowedOrigins_ or _allowedHeaders_ all methods / origins / headers are allowed.
4747

48+
> Note: Allowing a single static origin will improve cacheability.
49+
4850
### Example: using the library
4951

5052
```php
@@ -55,10 +57,10 @@ use Fruitcake\Cors\CorsService;
5557
$cors = new CorsService([
5658
'allowedHeaders' => ['x-allowed-header', 'x-other-allowed-header'],
5759
'allowedMethods' => ['DELETE', 'GET', 'POST', 'PUT'],
58-
'allowedOrigins' => ['http://localhost'],
60+
'allowedOrigins' => ['http://localhost', 'https://*.example.com'],
5961
'allowedOriginsPatterns' => ['/localhost:\d/'],
60-
'exposedHeaders' => false,
61-
'maxAge' => false,
62+
'exposedHeaders' => ['Content-Encoding'],
63+
'maxAge' => 0,
6264
'supportsCredentials' => false,
6365
]);
6466

@@ -69,30 +71,8 @@ $cors->isCorsRequest(Request $request);
6971
$cors->isPreflightRequest(Request $request);
7072
```
7173

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-
9474
## License
9575

9676
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
77+
78+
> This package is split-off from https://github.com/asm89/stack-cors and developed as stand-alone library since 2022

composer.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
"type": "library",
77
"license": "MIT",
88
"authors": [
9+
{
10+
"name": "Fruitcake",
11+
"homepage": "https://fruitcake.nl"
12+
},
913
{
1014
"name": "Barryvdh",
1115
"email": "barryvdh@gmail.com"
1216
}
1317
],
1418
"require": {
15-
"php": "^7.2|^8.0",
16-
"symfony/http-foundation": "^4|^5|^6"
19+
"php": "^7.4|^8.0",
20+
"symfony/http-foundation": "^4.4|^5.4|^6"
1721
},
1822
"require-dev": {
19-
"phpunit/phpunit": "^7|^9",
20-
"squizlabs/php_codesniffer": "^3.5"
23+
"phpunit/phpunit": "^9",
24+
"squizlabs/php_codesniffer": "^3.5",
25+
"phpstan/phpstan": "^1.4"
2126
},
2227
"autoload": {
2328
"psr-4": {
@@ -30,13 +35,15 @@
3035
}
3136
},
3237
"scripts": {
38+
"actions": "composer test && composer analyse && composer check-style",
3339
"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"
40+
"analyse": "phpstan analyse src tests --level=9",
41+
"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 tests",
42+
"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 tests"
3643
},
3744
"extra": {
3845
"branch-alias": {
39-
"dev-master": "1.0-dev"
46+
"dev-main": "1.1-dev"
4047
}
4148
}
4249
}

phpunit.xml.dist

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
<?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-
>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src/</directory>
6+
</include>
7+
</coverage>
128
<testsuites>
139
<testsuite name="Fruitcake Cors Test Suite">
1410
<directory>./tests</directory>
1511
</testsuite>
1612
</testsuites>
17-
18-
<filter>
19-
<whitelist>
20-
<directory suffix=".php">./src/</directory>
21-
</whitelist>
22-
</filter>
23-
2413
</phpunit>

0 commit comments

Comments
 (0)