Skip to content

Commit

Permalink
Close #32
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Feb 7, 2019
1 parent 2556e20 commit 0f03de1
Show file tree
Hide file tree
Showing 29 changed files with 1,078 additions and 1,574 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: php
php:
- 5.6
- hhvm
- 7.0
- 7.1
- 7.2
- 7.3
matrix:
include:
- php: 7.0
- php: 7.1
script:
- php vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
after_script:
Expand Down
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Framework agnostic Cross-Origin Resource Sharing (CORS) implementation

Copyright 2015 info@neomerx.com
Copyright 2015-2019 info@neomerx.com

This product includes software developed at Neomerx (www.neomerx.com).
76 changes: 52 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[![Project Management](https://img.shields.io/badge/project-management-blue.svg)](https://waffle.io/neomerx/cors-psr7)
[![Build Status](https://travis-ci.org/neomerx/cors-psr7.svg?branch=master)](https://travis-ci.org/neomerx/cors-psr7)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/neomerx/cors-psr7/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/neomerx/cors-psr7/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/neomerx/cors-psr7/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/neomerx/cors-psr7/?branch=master)
[![Build Status](https://travis-ci.org/neomerx/cors-psr7.svg?branch=master)](https://travis-ci.org/neomerx/cors-psr7)
[![HHVM](https://img.shields.io/hhvm/neomerx/cors-psr7.svg)](https://travis-ci.org/neomerx/cors-psr7)
[![License](https://img.shields.io/packagist/l/neomerx/cors-psr7.svg)](https://packagist.org/packages/neomerx/cors-psr7)

## Description
Expand All @@ -23,9 +21,9 @@ Why this package?
The package is designed to be used as a middleware. Typical usage

```php
use \Neomerx\Cors\Analyzer;
use \Psr\Http\Message\RequestInterface;
use \Neomerx\Cors\Contracts\AnalysisResultInterface;
use Neomerx\Cors\Analyzer;
use Psr\Http\Message\RequestInterface;
use Neomerx\Cors\Contracts\AnalysisResultInterface;

class CorsMiddleware
{
Expand Down Expand Up @@ -71,6 +69,44 @@ class CorsMiddleware
}
```

### Settings

Analyzer accepts settings in `Analyzer::instance($settings)` which must implement `AnalysisStrategyInterface`. You can use default implementation `\Neomerx\Cors\Strategies\Settings` to set the analyzer up.

For example,

```php
use Neomerx\Cors\Strategies\Settings;

$settings = (new Settings())
->setServerOrigin('https', 'api.example.com', 443)
->setPreFlightCacheMaxAge(0)
->setCredentialsSupported()
->setAllowedOrigins(['https://www.example.com', ...]) // or enableAllOriginsAllowed()
->setAllowedMethods(['GET', 'POST', 'DELETE', ...]) // or enableAllMethodsAllowed()
->setAllowedHeaders(['X-Custom-Header', ...]) // or enableAllHeadersAllowed()
->setExposedHeaders(['X-Custom-Header', ...])
->disableAddAllowedMethodsToPreFlightResponse()
->disableAddAllowedHeadersToPreFlightResponse()
->enableCheckHost();

$cors = Analyzer::instance($settings)->analyze($request);
```

Settings could be cached which improves performance. If you already have settings configured as in the example above you can get internal settings state as

```php
/** @var array $dataToCache */
$dataToCache = $settings->getData();
```

Cached state should be used as

```php
$settings = (new Settings())->setData($dataFromCache);
$cors = Analyzer::instance($settings)->analyze($request);
```

## Install

```
Expand All @@ -82,23 +118,19 @@ composer require neomerx/cors-psr7
Debug logging will provide a detailed step-by-step description of how requests are handled. In order to activate it a [PSR-3 compatible Logger](http://www.php-fig.org/psr/psr-3/) should be set to `Analyzer`.

```php
/** @var \Psr\Log\LoggerInterface $logger */
$logger = ...;
/** @var \Psr\Http\Message\RequestInterface $request */
$request = ...;
/** @var \Neomerx\Cors\Contracts\Strategies\SettingsStrategyInterface $settings */
$settings = ...;

$analyzer = Analyzer::instance($settings);
$analyzer->setLogger($logger)
$cors = $analyzer->analyze($request);
/** @var \Psr\Log\LoggerInterface $logger */
$logger = ...;

$analyzer = Analyzer::instance($settings);
$analyzer->setLogger($logger)
$cors = $analyzer->analyze($request);
```

## Advanced Usage

There are many possible strategies for handling cross and same origin requests which might and might not depend on data from requests.

This package has built-in strategy called `Settings` which implements simple settings identical for all requests (same list of allowed origins, same allowed methods for all requests and etc).
This built-in strategy `Settings` implements simple settings identical for all requests (same list of allowed origins, same allowed methods for all requests and etc).

However you can customize such behaviour. For example you can send different sets of allowed methods depending on request. This might be helpful when you have some kind of Access Control System and wish to differentiate response based on request (for example on its origin). You can either implement `AnalysisStrategyInterface` from scratch or override methods in `Settings` class if only a minor changes are needed to `Settings`. The new strategy could be sent to `Analyzer` constructor or `Analyzer::instance` method could be used for injection.

Expand All @@ -107,10 +139,8 @@ Example
```php
class CustomMethodsSettings extends Settings
{
public function getRequestAllowedMethods(
RequestInterface $request,
$requestMethod
) {
public function getRequestAllowedMethods(RequestInterface $request): string
{
// An external Access Control System could be used to determine
// which methods are allowed for this request.

Expand All @@ -129,14 +159,12 @@ composer test

## Questions?

Do not hesitate to contact us on [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/neomerx/json-api) or post an [issue](https://github.com/neomerx/cors-psr7/issues).
Do not hesitate to check [issues](https://github.com/neomerx/cors-psr7/issues) or post a new one.

## Contributing

If you have spotted any compliance issues with the [CORS Recommendation](http://www.w3.org/TR/cors/) please post an [issue](https://github.com/neomerx/cors-psr7/issues). Pull requests for documentation and code improvements (PSR-2, tests) are welcome.

Current tasks are managed with [Waffle.io](https://waffle.io/neomerx/cors-psr7).

## Versioning

This package is using [Semantic Versioning](http://semver.org/).
Expand Down
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=7.1.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^7.0",
"mockery/mockery": "^1.0",
"scrutinizer/ocular": "^1.1",
"squizlabs/php_codesniffer": "^3.0"
"scrutinizer/ocular": "^1.4",
"squizlabs/php_codesniffer": "^2.9",
"phpmd/phpmd": "^2.6"
},
"minimum-stability": "stable",
"autoload": {
Expand All @@ -45,9 +46,10 @@
}
},
"scripts": {
"test": "composer phpunit && composer phpcs",
"phpcs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"phpunit": "phpunit",
"hhvm": "hhvm ./vendor/phpunit/phpunit/phpunit"
"test": ["@test-unit", "@test-cs", "@test-md"],
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming"
}
}
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true">
cacheResult="false">
<testsuites>
<testsuite name="All">
<directory>./tests/</directory>
Expand All @@ -27,4 +27,4 @@
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/clover.xml"/>
</logging-->
</phpunit>
</phpunit>
14 changes: 8 additions & 6 deletions src/AnalysisResult.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace Neomerx\Cors;
<?php declare(strict_types=1);

namespace Neomerx\Cors;

/**
* Copyright 2015 info@neomerx.com (www.neomerx.com)
* Copyright 2015-2019 info@neomerx.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +18,7 @@
* limitations under the License.
*/

use \Neomerx\Cors\Contracts\AnalysisResultInterface;
use Neomerx\Cors\Contracts\AnalysisResultInterface;

/**
* @package Neomerx\Cors
Expand All @@ -36,7 +38,7 @@ class AnalysisResult implements AnalysisResultInterface
/**
* @inheritdoc
*/
public function __construct($requestType, array $responseHeaders)
public function __construct(int $requestType, array $responseHeaders)
{
$this->requestType = $requestType;
$this->headers = $responseHeaders;
Expand All @@ -45,15 +47,15 @@ public function __construct($requestType, array $responseHeaders)
/**
* @inheritdoc
*/
public function getRequestType()
public function getRequestType(): int
{
return $this->requestType;
}

/**
* @inheritdoc
*/
public function getResponseHeaders()
public function getResponseHeaders(): array
{
return $this->headers;
}
Expand Down
Loading

0 comments on commit 0f03de1

Please sign in to comment.