mirror of
				https://github.com/getgrav/grav.git
				synced 2025-10-27 00:16:06 +01:00 
			
		
		
		
	Added PSR-7 and PSR-15 classes
This commit is contained in:
		| @@ -2,6 +2,8 @@ | |||||||
| ## mm/dd/2018 | ## mm/dd/2018 | ||||||
|  |  | ||||||
| 1. [](#new) | 1. [](#new) | ||||||
|  |     * Added PSR-7 classes | ||||||
|  |     * Added PSR-15 `RequestHandler` class | ||||||
|     * Added `Page::httpResponseCode()` and `Page::httpHeaders()` methods |     * Added `Page::httpResponseCode()` and `Page::httpHeaders()` methods | ||||||
| 1. [](#improved) | 1. [](#improved) | ||||||
|     * Added apcu autoloader optimization |     * Added apcu autoloader optimization | ||||||
|   | |||||||
| @@ -18,6 +18,10 @@ | |||||||
|  |  | ||||||
|         "psr/simple-cache": "^1.0", |         "psr/simple-cache": "^1.0", | ||||||
|         "psr/http-message": "^1.0", |         "psr/http-message": "^1.0", | ||||||
|  |         "psr/http-server-middleware": "^1.0", | ||||||
|  |  | ||||||
|  |         "nyholm/psr7-server": "^0.2.1", | ||||||
|  |         "nyholm/psr7": "^1.0", | ||||||
|  |  | ||||||
|         "twig/twig": "~1.35", |         "twig/twig": "~1.35", | ||||||
|         "erusev/parsedown": "1.6.4", |         "erusev/parsedown": "1.6.4", | ||||||
|   | |||||||
							
								
								
									
										324
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										324
									
								
								composer.lock
									
									
									
										generated
									
									
									
								
							| @@ -4,7 +4,7 @@ | |||||||
|         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | ||||||
|         "This file is @generated automatically" |         "This file is @generated automatically" | ||||||
|     ], |     ], | ||||||
|     "content-hash": "62efd8660a745d3e62621d0432f6ef91", |     "content-hash": "a6592549298919c71cbcd8007b976e85", | ||||||
|     "packages": [ |     "packages": [ | ||||||
|         { |         { | ||||||
|             "name": "antoligy/dom-string-iterators", |             "name": "antoligy/dom-string-iterators", | ||||||
| @@ -1017,6 +1017,120 @@ | |||||||
|             ], |             ], | ||||||
|             "time": "2017-06-19T01:22:40+00:00" |             "time": "2017-06-19T01:22:40+00:00" | ||||||
|         }, |         }, | ||||||
|  |         { | ||||||
|  |             "name": "nyholm/psr7", | ||||||
|  |             "version": "1.0.1", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/Nyholm/psr7.git", | ||||||
|  |                 "reference": "02a20d5e9241daefd68935f7a43d9a7a8b4b6277" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/Nyholm/psr7/zipball/02a20d5e9241daefd68935f7a43d9a7a8b4b6277", | ||||||
|  |                 "reference": "02a20d5e9241daefd68935f7a43d9a7a8b4b6277", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": "^7.1", | ||||||
|  |                 "php-http/message-factory": "^1.0", | ||||||
|  |                 "psr/http-factory": "^1.0", | ||||||
|  |                 "psr/http-message": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "provide": { | ||||||
|  |                 "psr/http-factory-implementation": "1.0", | ||||||
|  |                 "psr/http-message-implementation": "1.0" | ||||||
|  |             }, | ||||||
|  |             "require-dev": { | ||||||
|  |                 "http-interop/http-factory-tests": "dev-master", | ||||||
|  |                 "php-http/psr7-integration-tests": "dev-master", | ||||||
|  |                 "phpunit/phpunit": "^7.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "extra": { | ||||||
|  |                 "branch-alias": { | ||||||
|  |                     "dev-master": "1.0-dev" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Nyholm\\Psr7\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "Tobias Nyholm", | ||||||
|  |                     "email": "tobias.nyholm@gmail.com" | ||||||
|  |                 }, | ||||||
|  |                 { | ||||||
|  |                     "name": "Martijn van der Ven", | ||||||
|  |                     "email": "martijn@vanderven.se" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "A fast PHP7 implementation of PSR-7", | ||||||
|  |             "homepage": "http://tnyholm.se", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "psr-17", | ||||||
|  |                 "psr-7" | ||||||
|  |             ], | ||||||
|  |             "time": "2018-09-02T11:50:33+00:00" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             "name": "nyholm/psr7-server", | ||||||
|  |             "version": "0.2.1", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/Nyholm/psr7-server.git", | ||||||
|  |                 "reference": "d7f72c4a57acf774d0049e46091929803976283b" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/Nyholm/psr7-server/zipball/d7f72c4a57acf774d0049e46091929803976283b", | ||||||
|  |                 "reference": "d7f72c4a57acf774d0049e46091929803976283b", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": "^7.1", | ||||||
|  |                 "psr/http-factory": "^1.0", | ||||||
|  |                 "psr/http-message": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "require-dev": { | ||||||
|  |                 "nyholm/nsa": "^1.1", | ||||||
|  |                 "nyholm/psr7": "^1.0", | ||||||
|  |                 "phpunit/phpunit": "^7.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Nyholm\\Psr7Server\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "Tobias Nyholm", | ||||||
|  |                     "email": "tobias.nyholm@gmail.com" | ||||||
|  |                 }, | ||||||
|  |                 { | ||||||
|  |                     "name": "Martijn van der Ven", | ||||||
|  |                     "email": "martijn@vanderven.se" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "Helper classes to handle PSR-7 server requests", | ||||||
|  |             "homepage": "http://tnyholm.se", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "psr-17", | ||||||
|  |                 "psr-7" | ||||||
|  |             ], | ||||||
|  |             "time": "2018-08-02T20:01:51+00:00" | ||||||
|  |         }, | ||||||
|         { |         { | ||||||
|             "name": "phive/twig-extensions-deferred", |             "name": "phive/twig-extensions-deferred", | ||||||
|             "version": "v1.0.2", |             "version": "v1.0.2", | ||||||
| @@ -1060,6 +1174,56 @@ | |||||||
|             ], |             ], | ||||||
|             "time": "2017-03-17T21:39:21+00:00" |             "time": "2017-03-17T21:39:21+00:00" | ||||||
|         }, |         }, | ||||||
|  |         { | ||||||
|  |             "name": "php-http/message-factory", | ||||||
|  |             "version": "v1.0.2", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/php-http/message-factory.git", | ||||||
|  |                 "reference": "a478cb11f66a6ac48d8954216cfed9aa06a501a1" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/php-http/message-factory/zipball/a478cb11f66a6ac48d8954216cfed9aa06a501a1", | ||||||
|  |                 "reference": "a478cb11f66a6ac48d8954216cfed9aa06a501a1", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": ">=5.4", | ||||||
|  |                 "psr/http-message": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "extra": { | ||||||
|  |                 "branch-alias": { | ||||||
|  |                     "dev-master": "1.0-dev" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Http\\Message\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "Márk Sági-Kazár", | ||||||
|  |                     "email": "mark.sagikazar@gmail.com" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "Factory interfaces for PSR-7 HTTP Message", | ||||||
|  |             "homepage": "http://php-http.org", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "factory", | ||||||
|  |                 "http", | ||||||
|  |                 "message", | ||||||
|  |                 "stream", | ||||||
|  |                 "uri" | ||||||
|  |             ], | ||||||
|  |             "time": "2015-12-19T14:08:53+00:00" | ||||||
|  |         }, | ||||||
|         { |         { | ||||||
|             "name": "pimple/pimple", |             "name": "pimple/pimple", | ||||||
|             "version": "v3.2.3", |             "version": "v3.2.3", | ||||||
| @@ -1159,6 +1323,58 @@ | |||||||
|             ], |             ], | ||||||
|             "time": "2017-02-14T16:28:37+00:00" |             "time": "2017-02-14T16:28:37+00:00" | ||||||
|         }, |         }, | ||||||
|  |         { | ||||||
|  |             "name": "psr/http-factory", | ||||||
|  |             "version": "1.0.0", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/php-fig/http-factory.git", | ||||||
|  |                 "reference": "378bfe27931ecc54ff824a20d6f6bfc303bbd04c" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/php-fig/http-factory/zipball/378bfe27931ecc54ff824a20d6f6bfc303bbd04c", | ||||||
|  |                 "reference": "378bfe27931ecc54ff824a20d6f6bfc303bbd04c", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": ">=7.0.0", | ||||||
|  |                 "psr/http-message": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "extra": { | ||||||
|  |                 "branch-alias": { | ||||||
|  |                     "dev-master": "1.0.x-dev" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Psr\\Http\\Message\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "PHP-FIG", | ||||||
|  |                     "homepage": "http://www.php-fig.org/" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "Common interfaces for PSR-7 HTTP message factories", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "factory", | ||||||
|  |                 "http", | ||||||
|  |                 "message", | ||||||
|  |                 "psr", | ||||||
|  |                 "psr-17", | ||||||
|  |                 "psr-7", | ||||||
|  |                 "request", | ||||||
|  |                 "response" | ||||||
|  |             ], | ||||||
|  |             "time": "2018-07-30T21:54:04+00:00" | ||||||
|  |         }, | ||||||
|         { |         { | ||||||
|             "name": "psr/http-message", |             "name": "psr/http-message", | ||||||
|             "version": "1.0.1", |             "version": "1.0.1", | ||||||
| @@ -1209,6 +1425,112 @@ | |||||||
|             ], |             ], | ||||||
|             "time": "2016-08-06T14:39:51+00:00" |             "time": "2016-08-06T14:39:51+00:00" | ||||||
|         }, |         }, | ||||||
|  |         { | ||||||
|  |             "name": "psr/http-server-handler", | ||||||
|  |             "version": "1.0.0", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/php-fig/http-server-handler.git", | ||||||
|  |                 "reference": "439d92054dc06097f2406ec074a2627839955a02" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/php-fig/http-server-handler/zipball/439d92054dc06097f2406ec074a2627839955a02", | ||||||
|  |                 "reference": "439d92054dc06097f2406ec074a2627839955a02", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": ">=7.0", | ||||||
|  |                 "psr/http-message": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "extra": { | ||||||
|  |                 "branch-alias": { | ||||||
|  |                     "dev-master": "1.0.x-dev" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Psr\\Http\\Server\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "PHP-FIG", | ||||||
|  |                     "homepage": "http://www.php-fig.org/" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "Common interface for HTTP server-side request handler", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "handler", | ||||||
|  |                 "http", | ||||||
|  |                 "http-interop", | ||||||
|  |                 "psr", | ||||||
|  |                 "psr-15", | ||||||
|  |                 "psr-7", | ||||||
|  |                 "request", | ||||||
|  |                 "response", | ||||||
|  |                 "server" | ||||||
|  |             ], | ||||||
|  |             "time": "2018-01-22T17:04:15+00:00" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             "name": "psr/http-server-middleware", | ||||||
|  |             "version": "1.0.0", | ||||||
|  |             "source": { | ||||||
|  |                 "type": "git", | ||||||
|  |                 "url": "https://github.com/php-fig/http-server-middleware.git", | ||||||
|  |                 "reference": "ea17eb1fb2c8df6db919cc578451a8013c6a0ae5" | ||||||
|  |             }, | ||||||
|  |             "dist": { | ||||||
|  |                 "type": "zip", | ||||||
|  |                 "url": "https://api.github.com/repos/php-fig/http-server-middleware/zipball/ea17eb1fb2c8df6db919cc578451a8013c6a0ae5", | ||||||
|  |                 "reference": "ea17eb1fb2c8df6db919cc578451a8013c6a0ae5", | ||||||
|  |                 "shasum": "" | ||||||
|  |             }, | ||||||
|  |             "require": { | ||||||
|  |                 "php": ">=7.0", | ||||||
|  |                 "psr/http-message": "^1.0", | ||||||
|  |                 "psr/http-server-handler": "^1.0" | ||||||
|  |             }, | ||||||
|  |             "type": "library", | ||||||
|  |             "extra": { | ||||||
|  |                 "branch-alias": { | ||||||
|  |                     "dev-master": "1.0.x-dev" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "autoload": { | ||||||
|  |                 "psr-4": { | ||||||
|  |                     "Psr\\Http\\Server\\": "src/" | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             "notification-url": "https://packagist.org/downloads/", | ||||||
|  |             "license": [ | ||||||
|  |                 "MIT" | ||||||
|  |             ], | ||||||
|  |             "authors": [ | ||||||
|  |                 { | ||||||
|  |                     "name": "PHP-FIG", | ||||||
|  |                     "homepage": "http://www.php-fig.org/" | ||||||
|  |                 } | ||||||
|  |             ], | ||||||
|  |             "description": "Common interface for HTTP server-side middleware", | ||||||
|  |             "keywords": [ | ||||||
|  |                 "http", | ||||||
|  |                 "http-interop", | ||||||
|  |                 "middleware", | ||||||
|  |                 "psr", | ||||||
|  |                 "psr-15", | ||||||
|  |                 "psr-7", | ||||||
|  |                 "request", | ||||||
|  |                 "response" | ||||||
|  |             ], | ||||||
|  |             "time": "2018-01-22T17:08:31+00:00" | ||||||
|  |         }, | ||||||
|         { |         { | ||||||
|             "name": "psr/log", |             "name": "psr/log", | ||||||
|             "version": "1.0.2", |             "version": "1.0.2", | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/Request.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/Request.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\Request', 'Grav\Framework\Psr7\Request'); | ||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/Response.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/Response.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\Response', 'Grav\Framework\Psr7\Response'); | ||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/ServerRequest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/ServerRequest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\ServerRequest', 'Grav\Framework\Psr7\ServerRequest'); | ||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/Stream.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/Stream.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\Stream', 'Grav\Framework\Psr7\Stream'); | ||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/UploadedFile.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/UploadedFile.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\UploadedFile', 'Grav\Framework\Psr7\UploadedFile'); | ||||||
							
								
								
									
										3
									
								
								system/src/Grav/Framework/Psr7/Uri.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								system/src/Grav/Framework/Psr7/Uri.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | class_alias('Nyholm\Psr7\Uri', 'Grav\Framework\Psr7\Uri'); | ||||||
| @@ -0,0 +1,40 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | declare(strict_types=1); | ||||||
|  |  | ||||||
|  | namespace Grav\Framework\RequestHandler\Exception; | ||||||
|  |  | ||||||
|  | use Throwable; | ||||||
|  |  | ||||||
|  | class InvalidArgumentException extends \InvalidArgumentException | ||||||
|  | { | ||||||
|  |     /** | ||||||
|  |      * @var mixed|null | ||||||
|  |      */ | ||||||
|  |     private $invalidMiddleware; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * InvalidArgumentException constructor. | ||||||
|  |      * | ||||||
|  |      * @param string $message | ||||||
|  |      * @param mixed|null $invalidMiddleware | ||||||
|  |      * @param int $code | ||||||
|  |      * @param Throwable|null $previous | ||||||
|  |      */ | ||||||
|  |     public function __construct($message = '', $invalidMiddleware = null, $code = 0, Throwable $previous = null) | ||||||
|  |     { | ||||||
|  |         parent::__construct($message, $code, $previous); | ||||||
|  |  | ||||||
|  |         $this->invalidMiddleware = $invalidMiddleware; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Return the invalid middleware | ||||||
|  |      * | ||||||
|  |      * @return mixed|null | ||||||
|  |      */ | ||||||
|  |     public function getInvalidMiddleware() | ||||||
|  |     { | ||||||
|  |         return $this->invalidMiddleware; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										93
									
								
								system/src/Grav/Framework/RequestHandler/RequestHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								system/src/Grav/Framework/RequestHandler/RequestHandler.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | declare(strict_types=1); | ||||||
|  |  | ||||||
|  | namespace Grav\Framework\RequestHandler; | ||||||
|  |  | ||||||
|  | use Grav\Framework\RequestHandler\Exception\InvalidArgumentException; | ||||||
|  | use Psr\Container\ContainerInterface; | ||||||
|  | use Psr\Http\Message\ResponseInterface; | ||||||
|  | use Psr\Http\Message\ServerRequestInterface; | ||||||
|  | use Psr\Http\Server\MiddlewareInterface; | ||||||
|  | use Psr\Http\Server\RequestHandlerInterface; | ||||||
|  |  | ||||||
|  | class RequestHandler implements RequestHandlerInterface | ||||||
|  | { | ||||||
|  |     /** @var string[]|MiddlewareInterface[]|array */ | ||||||
|  |     protected $middleware; | ||||||
|  |  | ||||||
|  |     /** @var callable */ | ||||||
|  |     private $default; | ||||||
|  |  | ||||||
|  |     /** @var ContainerInterface */ | ||||||
|  |     private $container; | ||||||
|  |  | ||||||
|  |     /** @var callable */ | ||||||
|  |     private $wrapper; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Delegate constructor. | ||||||
|  |      * | ||||||
|  |      * @param array $middleware | ||||||
|  |      * @param callable $default | ||||||
|  |      * @param ContainerInterface|null $container | ||||||
|  |      */ | ||||||
|  |     public function __construct(array $middleware, callable $default, ContainerInterface $container = null) | ||||||
|  |     { | ||||||
|  |         $this->middleware = $middleware; | ||||||
|  |         $this->default = $default; | ||||||
|  |         $this->container = $container; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param callable $wrapper | ||||||
|  |      * @return $this | ||||||
|  |      */ | ||||||
|  |     public function setWrapper(callable $wrapper) | ||||||
|  |     { | ||||||
|  |         $this->wrapper = $wrapper; | ||||||
|  |  | ||||||
|  |         return $this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param $name | ||||||
|  |      * @param callable $callable | ||||||
|  |      * @return $this | ||||||
|  |      */ | ||||||
|  |     public function add($name, callable $callable) | ||||||
|  |     { | ||||||
|  |         $this->container[$name] = $callable; | ||||||
|  |  | ||||||
|  |         return $this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * {@inheritdoc} | ||||||
|  |      * @throws InvalidArgumentException | ||||||
|  |      */ | ||||||
|  |     public function handle(ServerRequestInterface $request): ResponseInterface | ||||||
|  |     { | ||||||
|  |         $middleware = array_shift($this->middleware); | ||||||
|  |  | ||||||
|  |         // Use default callable if there is no middleware. | ||||||
|  |         if ($middleware === null) { | ||||||
|  |             return \call_user_func($this->default, $request); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if ($middleware instanceof MiddlewareInterface) { | ||||||
|  |             return $middleware->process($request, clone $this); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if (!$this->container || !$this->container->has($middleware)) { | ||||||
|  |             throw new InvalidArgumentException( | ||||||
|  |                 sprintf('The middleware is not a valid %s and is not passed in the Container', MiddlewareInterface::class), | ||||||
|  |                 $middleware | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         array_unshift($this->middleware, $this->container->get($middleware)); | ||||||
|  |  | ||||||
|  |         return $this->handle($request); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user