From b9dfa3e7e6fd01de58663bbdec0e568d2e71c8da Mon Sep 17 00:00:00 2001 From: Chris Hickingbottom Date: Thu, 7 May 2020 01:02:36 -0500 Subject: [PATCH 1/4] update plugin to cakephp 4.x --- .phpunit.result.cache | 1 + composer.json | 9 ++- phpunit.xml.dist | 12 +--- src/Error/AppExceptionRenderer.php | 14 ++-- .../Middleware/CorsMiddleware.php | 70 ++++++++++++------- src/Plugin.php | 8 +++ .../Controller/ErrorControllerTest.php | 5 +- .../Middleware/CorsMiddlewareTest.php | 37 +++++++--- tests/bootstrap.php | 2 +- 9 files changed, 96 insertions(+), 62 deletions(-) create mode 100644 .phpunit.result.cache rename src/{Routing => }/Middleware/CorsMiddleware.php (57%) create mode 100644 src/Plugin.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..66441b0 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +C:37:"PHPUnit\Runner\DefaultTestResultCache":7834:{a:2:{s:7:"defects";a:46:{s:60:"Cors\TestCase\Controller\ErrorControllerTest::testIncomplete";i:2;s:91:"Cors\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";i:4;s:82:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";i:4;s:88:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";i:4;s:81:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";i:4;s:70:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";i:4;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";i:4;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";i:4;s:72:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";i:4;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";i:4;s:71:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";i:4;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";i:4;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";i:4;s:61:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodString";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";i:4;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";i:4;s:83:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";i:4;s:68:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";i:4;s:66:"Cors\Tests\TestCase\Controller\ErrorControllerTest::testIncomplete";i:2;s:97:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";i:3;s:88:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";i:4;s:94:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";i:4;s:87:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";i:3;s:76:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";i:4;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";i:4;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";i:4;s:78:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";i:4;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";i:4;s:77:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";i:4;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";i:4;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";i:4;s:67:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodString";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";i:4;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";i:4;s:89:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";i:3;s:74:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";i:4;}s:5:"times";a:46:{s:60:"Cors\TestCase\Controller\ErrorControllerTest::testIncomplete";d:0.007;s:91:"Cors\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";d:0.014;s:82:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";d:0;s:88:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";d:0;s:81:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";d:0;s:70:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";d:0;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";d:0;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";d:0;s:72:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";d:0;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";d:0;s:71:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";d:0;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";d:0;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";d:0;s:61:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodString";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";d:0;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";d:0;s:83:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";d:0;s:68:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";d:0;s:66:"Cors\Tests\TestCase\Controller\ErrorControllerTest::testIncomplete";d:0.002;s:97:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";d:0.007;s:88:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";d:0;s:94:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";d:0;s:87:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";d:0;s:76:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";d:0;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";d:0;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";d:0;s:78:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";d:0;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";d:0;s:77:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";d:0;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";d:0;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";d:0;s:67:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodString";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";d:0;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";d:0;s:89:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";d:0;s:74:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";d:0;}}} \ No newline at end of file diff --git a/composer.json b/composer.json index 262d929..96754c3 100644 --- a/composer.json +++ b/composer.json @@ -3,11 +3,14 @@ "description": "A CakePHP (3.3.x) plugin for activate cors domain in your application", "type": "cakephp-plugin", "require": { - "php": ">=5.6", - "cakephp/cakephp": ">=3.3.2 <4.0.0" + "php": ">=7.2.0", + "cakephp/cakephp": "^4.0", + "psr/http-server-handler": "^1.0", + "psr/http-server-middleware": "^1.0" }, "require-dev": { - "phpunit/phpunit": "5.*" + "phpunit/phpunit": "~8.5.0", + "cakephp/cakephp-codesniffer": "^4.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 312dfd8..b31c703 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,6 @@ colors="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="./tests/bootstrap.php" > @@ -31,13 +30,8 @@ - - ./vendor/ - ./vendor/ - - ./tests/ - ./tests/ - + + src/ + - diff --git a/src/Error/AppExceptionRenderer.php b/src/Error/AppExceptionRenderer.php index ad3b810..fd56188 100644 --- a/src/Error/AppExceptionRenderer.php +++ b/src/Error/AppExceptionRenderer.php @@ -2,12 +2,7 @@ namespace Cors\Error; use Cake\Core\Configure; -use Cake\Event\Event; -use Cake\Network\Request; -use Cake\Network\Response; -use Cake\Routing\Router; -use Exception; -use Cors\Routing\Middleware\CorsMiddleware; +use Cors\Middleware\CorsMiddleware; function get_dynamic_parent() { return Configure::read('Error.baseExceptionRenderer');// return what you need @@ -16,7 +11,6 @@ class_alias(get_dynamic_parent(), 'Cors\Error\BaseExceptionRenderer'); class AppExceptionRenderer extends BaseExceptionRenderer { - /** * Returns the current controller. * @@ -26,9 +20,9 @@ protected function _getController() { $controller = parent::_getController(); $cors = new CorsMiddleware(); - $controller->response = $cors( - $controller->request, $controller->response, - function($request, $response){ return $response; } + $controller->response = $cors->addHeaders( + $controller->getRequest(), + $controller->getResponse() ); return $controller; } diff --git a/src/Routing/Middleware/CorsMiddleware.php b/src/Middleware/CorsMiddleware.php similarity index 57% rename from src/Routing/Middleware/CorsMiddleware.php rename to src/Middleware/CorsMiddleware.php index 040dca9..979795e 100644 --- a/src/Routing/Middleware/CorsMiddleware.php +++ b/src/Middleware/CorsMiddleware.php @@ -1,41 +1,55 @@ handle($request); + + $response = $this->addHeaders($request, $response); + + return $response; + } + + public function addHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { if ($request->getHeader('Origin')) { $response = $response ->withHeader('Access-Control-Allow-Origin', $this->_allowOrigin($request)) ->withHeader('Access-Control-Allow-Credentials', $this->_allowCredentials()) - ->withHeader('Access-Control-Max-Age', $this->_maxAge()) - ->withHeader('Access-Control-Expose-Headers', $this->_exposeHeaders()) - ; + ->withHeader('Access-Control-Max-Age', $this->_maxAge()); if (strtoupper($request->getMethod()) === 'OPTIONS') { $response = $response + ->withHeader('Access-Control-Expose-Headers', $this->_exposeHeaders()) ->withHeader('Access-Control-Allow-Headers', $this->_allowHeaders($request)) - ->withHeader('Access-Control-Allow-Methods', $this->_allowMethods()) - ; - return $response; + ->withHeader('Access-Control-Allow-Methods', $this->_allowMethods()); } + } - return $next($request, $response); + return $response; } + /** - * PHPCS docblock fix needed! + * @param \Psr\Http\Message\ServerRequestInterface $request + * @return array|string */ - private function _allowOrigin($request) { + private function _allowOrigin(ServerRequestInterface $request) + { $allowOrigin = Configure::read('Cors.AllowOrigin'); $origin = $request->getHeader('Origin'); @@ -59,36 +73,41 @@ private function _allowOrigin($request) { } /** - * PHPCS docblock fix needed! + * @return String */ - private function _allowCredentials() { + private function _allowCredentials(): String + { return (Configure::read('Cors.AllowCredentials')) ? 'true' : 'false'; } /** - * PHPCS docblock fix needed! + * @return String */ - private function _allowMethods() { + private function _allowMethods(): String + { return implode(', ', (array) Configure::read('Cors.AllowMethods')); } /** - * PHPCS docblock fix needed! + * @param \Psr\Http\Message\ServerRequestInterface $request + * @return String */ - private function _allowHeaders($request) { + private function _allowHeaders(ServerRequestInterface $request): String + { $allowHeaders = Configure::read('Cors.AllowHeaders'); if ($allowHeaders === true) { - return $request->getHeader('Access-Control-Request-Headers'); + return $request->getHeaderLine('Access-Control-Request-Headers'); } return implode(', ', (array) $allowHeaders); } /** - * PHPCS docblock fix needed! + * @return String */ - private function _exposeHeaders() { + private function _exposeHeaders(): String + { $exposeHeaders = Configure::read('Cors.ExposeHeaders'); if (is_string($exposeHeaders) || is_array($exposeHeaders)) { @@ -99,9 +118,10 @@ private function _exposeHeaders() { } /** - * PHPCS docblock fix needed! + * @return String */ - private function _maxAge() { + private function _maxAge(): String + { $maxAge = (string) Configure::read('Cors.MaxAge'); return ($maxAge) ?: '0'; diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 0000000..f75380f --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,8 @@ +server = [ @@ -33,12 +36,9 @@ private function _setServer(Array $server) private function _sendRequest() { $request = ServerRequestFactory::fromGlobals($this->server); - $response = new Response(); + $handler = new RequestHandlerStub(); $middleware = new CorsMiddleware(); - $next = function ($request, $response) { - return $response; - }; - $response = $middleware($request, $response, $next); + $response = $middleware->process($request, $handler); return $response; } @@ -85,7 +85,7 @@ public function testDefaultValuesIfIsAOptionsRequest() $response = $this->_sendRequest(); $headers = $response->getHeaders(); - $this->assertEquals([], $headers['Access-Control-Allow-Headers']); + $this->assertEquals('', current($headers['Access-Control-Allow-Headers'])); $this->assertEquals('GET, POST, PUT, PATCH, DELETE', current($headers['Access-Control-Allow-Methods'])); $this->assertEquals('', current($headers['Access-Control-Expose-Headers'])); } @@ -224,3 +224,20 @@ public function testMaxAgeFalse() $this->assertEquals(0, $responseMaxAge); } } + +class RequestHandlerStub implements RequestHandlerInterface +{ + public $callable; + + public function __construct(?callable $callable = null) + { + $this->callable = $callable ?: function ($request) { + return new Response(); + }; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return ($this->callable)($request); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5b0f489..90bb771 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -23,4 +23,4 @@ chdir($root); require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -Plugin::load('Cors', ['bootstrap' => true, 'path' => dirname(dirname(__FILE__)) . DS]); +Plugin::getCollection()->add(new \Cors\Plugin(['bootstrap' => true, 'path' => dirname(dirname(__FILE__)) . DS])); From a49d35647133163a6324cec21e700ff13e886a9e Mon Sep 17 00:00:00 2001 From: Chris Hickingbottom Date: Thu, 7 May 2020 01:03:11 -0500 Subject: [PATCH 2/4] update plugin to cakephp 4.x --- .phpunit.result.cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 66441b0..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":7834:{a:2:{s:7:"defects";a:46:{s:60:"Cors\TestCase\Controller\ErrorControllerTest::testIncomplete";i:2;s:91:"Cors\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";i:4;s:82:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";i:4;s:88:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";i:4;s:81:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";i:4;s:70:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";i:4;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";i:4;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";i:4;s:72:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";i:4;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";i:4;s:71:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";i:4;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";i:4;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";i:4;s:61:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodString";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";i:4;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";i:4;s:83:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";i:4;s:68:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";i:4;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";i:4;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";i:4;s:66:"Cors\Tests\TestCase\Controller\ErrorControllerTest::testIncomplete";i:2;s:97:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";i:3;s:88:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";i:4;s:94:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";i:4;s:87:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";i:3;s:76:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";i:4;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";i:4;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";i:4;s:78:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";i:4;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";i:4;s:77:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";i:4;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";i:4;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";i:4;s:67:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodString";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";i:4;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";i:4;s:89:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";i:3;s:74:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";i:4;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";i:4;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";i:4;}s:5:"times";a:46:{s:60:"Cors\TestCase\Controller\ErrorControllerTest::testIncomplete";d:0.007;s:91:"Cors\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";d:0.014;s:82:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";d:0;s:88:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";d:0;s:81:"Cors\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";d:0;s:70:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";d:0;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";d:0;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";d:0;s:72:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";d:0;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";d:0;s:71:"Cors\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";d:0;s:64:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";d:0;s:65:"Cors\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";d:0;s:61:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodString";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";d:0;s:66:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";d:0;s:83:"Cors\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";d:0;s:68:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";d:0;s:67:"Cors\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";d:0;s:60:"Cors\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";d:0;s:66:"Cors\Tests\TestCase\Controller\ErrorControllerTest::testIncomplete";d:0.002;s:97:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testNoExposeAllCorsHeadersIfNotAOptionsRequest";d:0.007;s:88:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfNotAOptionsRequest";d:0;s:94:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeAllCorsHeadersIfIsAOptionsRequest";d:0;s:87:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testDefaultValuesIfIsAOptionsRequest";d:0;s:76:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginDifferentAllKey";d:0;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginIsForbidden";d:0;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsOk";d:0;s:78:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginStringIsForbidden";d:0;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsOk";d:0;s:77:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testOriginArrayIsForbidden";d:0;s:70:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsTrue";d:0;s:71:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testCredentialsFalse";d:0;s:67:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodString";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMethodArray";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersString";d:0;s:72:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersArray";d:0;s:89:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testAllowHeadersAllReturnSendedHeaders";d:0;s:74:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersString";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersArray";d:0;s:73:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testExposeHeadersFalse";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAge1Hour";d:0;s:66:"Cors\Tests\TestCase\Middleware\CorsMiddlewareTest::testMaxAgeFalse";d:0;}}} \ No newline at end of file From c59e46910ba82345e0fb1d451d220658583c106f Mon Sep 17 00:00:00 2001 From: ozee31 Date: Thu, 14 May 2020 18:12:24 +0200 Subject: [PATCH 3/4] Move Middleware to routing directory and fix AppExceptionRenderer.php (Thnx @rchavik) --- src/Error/AppExceptionRenderer.php | 5 +++-- src/{ => Routing}/Middleware/CorsMiddleware.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) rename src/{ => Routing}/Middleware/CorsMiddleware.php (99%) diff --git a/src/Error/AppExceptionRenderer.php b/src/Error/AppExceptionRenderer.php index fd56188..a6d60f7 100644 --- a/src/Error/AppExceptionRenderer.php +++ b/src/Error/AppExceptionRenderer.php @@ -2,7 +2,8 @@ namespace Cors\Error; use Cake\Core\Configure; -use Cors\Middleware\CorsMiddleware; +use Cake\Controller\Controller; +use Cors\Routing\Middleware\CorsMiddleware; function get_dynamic_parent() { return Configure::read('Error.baseExceptionRenderer');// return what you need @@ -16,7 +17,7 @@ class AppExceptionRenderer extends BaseExceptionRenderer * * @return \Cake\Controller\Controller */ - protected function _getController() + protected function _getController(): Controller { $controller = parent::_getController(); $cors = new CorsMiddleware(); diff --git a/src/Middleware/CorsMiddleware.php b/src/Routing/Middleware/CorsMiddleware.php similarity index 99% rename from src/Middleware/CorsMiddleware.php rename to src/Routing/Middleware/CorsMiddleware.php index 979795e..0e6ba12 100644 --- a/src/Middleware/CorsMiddleware.php +++ b/src/Routing/Middleware/CorsMiddleware.php @@ -1,5 +1,5 @@ Date: Thu, 14 May 2020 18:19:28 +0200 Subject: [PATCH 4/4] Update readme for Cakephp 4 --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7d26c3e..b3ae5a6 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,16 @@ [![Build Status](https://travis-ci.org/ozee31/cakephp-cors.svg?branch=master)](https://travis-ci.org/ozee31/cakephp-cors) -A CakePHP (3.3+) plugin for activate cors domain in your application with [Middleware](http://book.cakephp.org/3.0/en/controllers/middleware.html). +A CakePHP (4+) plugin for activate cors domain in your application with [Middleware](http://book.cakephp.org/3.0/en/controllers/middleware.html). [Learn more about CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) +**For cake 3.3+ use branch [cake-3](https://github.com/ozee31/cakephp-cors/tree/cake-3)** + ## Requirements -- PHP version 5.6 or higher -- CakePhp 3.3 or higher +- PHP version 7.2 or higher +- CakePhp 4.0 or higher ## Installation @@ -26,8 +28,12 @@ composer require ozee31/cakephp-cors Loading the Plugin ```PHP - // In config/bootstrap.php - Plugin::load('Cors', ['bootstrap' => true, 'routes' => false]); +// In src/Application.php +public function bootstrap(): void +{ + // code ... + $this->addPlugin('Cors'); +} ``` By default the plugin authorize cors for all origins, all methods and all headers and caches all for one day.