diff --git a/README.md b/README.md index 4ab164d..7409915 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ server { } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_pass unix:/var/run/php5-fpm.sock; + fastcgi_pass unix:/var/run/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_intercept_errors on; @@ -68,16 +68,17 @@ server { ```php $config = [ - 'paths' => [ - 'controller' => null, //The full path to the directory where the Controller classes are kept. - 'middleware' => null, //The full path to the directory where the Middleware classes are kept. + 'paths' => [ + 'controller' => null, //The full path to the directory where the Controller classes are kept. + 'middleware' => null, //The full path to the directory where the Middleware classes are kept. ], - 'namespaces' => [ - 'controller' => null, //Namespace prefix of Controller classes, if applicable. - 'middleware' => null, //Namespace prefix of Middleware classes, if applicable. + 'namespaces' => [ + 'controller' => null, //Namespace prefix of Controller classes, if applicable. + 'middleware' => null, //Namespace prefix of Middleware classes, if applicable. ], - 'base_path' => '/', // If you are working in a subdirectory; identifies your working directory. - 'variable_method' => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method']. + 'base_path' => '/', // If you are working in a subdirectory; identifies your working directory. + 'variable_method' => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method']. + 'argument_new_instance' => false, // This configuration is used for Request and Response objects that you want as arguments. ]; ``` @@ -93,30 +94,12 @@ _**See the Wiki for detailed documentation.**_ ```php require_once "vendor/autoload.php"; -use \InitPHP\HTTP\{Request, Response, Stream, Emitter}; +use \InitPHP\HTTP\Message\{Request, Response, Stream}; +use \InitPHP\HTTP\Emitter\Emitter; use \InitPHP\Router\Router; -if(($headers = function_exists('apache_request_headers') ? apache_request_headers() : []) === FALSE){ - $headers = []; -} - -$uri = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') - . '://' - . ($_SERVER['SERVER_NAME'] ?? 'localhost') - . (isset($_SERVER['SERVER_PORT']) && !\in_array($_SERVER['SERVER_PORT'], [80, 443]) ? ':' . $_SERVER['SERVER_PORT'] : '') - . ($_SERVER['REQUEST_URI'] ?? '/'); - -// Construct the HTTP request object. -$request = new Request( - ($_SERVER['REQUEST_METHOD'] ?? 'GET'), - $uri, - $headers, - null, - '1.1' -); - -// Create a new HTTP response object. -$response = new Response(200, [], (new Stream('', null)), '1.1'); +$request = Request::createFromGlobals(); +$response = new Response(); // Create the router object. $router = new Router($request, $response, []); @@ -126,6 +109,13 @@ $router->get('/', function () { return 'Hello World!'; }); +$router->post('/login', function (Request $request, Response $response) { + return $response->json([ + 'status' => 0, + 'message' => 'Unauthorized', + ], 401); +}); + // If you do not make a definition for 404 errors; An exception is thrown if there is no match with the request. $router->error_404(function () { echo 'Page Not Found'; diff --git a/composer.json b/composer.json index b1a5356..be47112 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,6 @@ }, "require-dev": { "phpunit/phpunit": "9.5", - "initphp/http": "^1.0" + "initphp/http": "^2.0" } } diff --git a/src/Router.php b/src/Router.php index 5aa429b..cc78329 100644 --- a/src/Router.php +++ b/src/Router.php @@ -7,7 +7,7 @@ * @author Muhammet ŞAFAK * @copyright Copyright © 2022 Muhammet ŞAFAK * @license ./LICENSE MIT - * @version 1.1.3 + * @version 1.2.1 * @link https://www.muhammetsafak.com.tr */ @@ -45,18 +45,21 @@ class Router protected $cache_status = false; protected $configs = [ - 'paths' => [ - 'controller' => null, - 'middleware' => null, + 'paths' => [ + 'controller' => null, + 'middleware' => null, ], - 'namespaces' => [ - 'controller' => null, - 'middleware' => null, + 'namespaces' => [ + 'controller' => null, + 'middleware' => null, ], - 'base_path' => '/', - 'variable_method' => false, + 'base_path' => '/', + 'variable_method' => false, + 'argument_new_instance' => false, ]; + protected $argumentNewInstance = false; + protected $patterns = [ '{[^/]+}' => '([^/]+)', ':any[0-9]?' => '([^/]+)', @@ -134,6 +137,8 @@ public function __construct(RequestInterface $request, ResponseInterface $respon unset($configs['cache']); } $this->configs = \array_merge($this->configs, $configs); + $this->argumentNewInstance = $this->configs['argument_new_instance']; + unset($this->configs['argument_new_instance']); $this->request = &$request; $this->response = &$response; $this->uri = new Uri($this->request->getUri()->__toString()); @@ -1016,13 +1021,15 @@ private function resolveParameters(\Reflector $reflector, array $parameters): ar } continue; } - if($class->isInstance($this->request)){ - $arguments[] = $this->request; - continue; - } - if($class->isInstance($this->response)){ - $arguments[] = $this->response; - continue; + if (!$this->argumentNewInstance) { + if($class->isInstance($this->request)){ + $arguments[] = $this->request; + continue; + } + if($class->isInstance($this->response)){ + $arguments[] = $this->response; + continue; + } } if($class->isInstantiable()){ $arguments[] = $this->getClassContainer($class); @@ -1033,11 +1040,13 @@ private function resolveParameters(\Reflector $reflector, array $parameters): ar private function getClassContainer(\ReflectionClass $class): object { - if($class->isInstance($this->request)){ - return $this->request; - } - if($class->isInstance($this->response)){ - return $this->response; + if (!$this->argumentNewInstance) { + if($class->isInstance($this->request)){ + return $this->request; + } + if($class->isInstance($this->response)){ + return $this->response; + } } if(isset($this->container) && is_object($this->container)){ return $this->container->get($class->getName()); diff --git a/tests/Unit/NamedAndURLCreateTest.php b/tests/Unit/NamedAndURLCreateTest.php index d2a865d..6e4dd9d 100644 --- a/tests/Unit/NamedAndURLCreateTest.php +++ b/tests/Unit/NamedAndURLCreateTest.php @@ -15,7 +15,7 @@ namespace Tests\Router\Unit; -use \InitPHP\HTTP\{Request, Response, Stream}; +use \InitPHP\HTTP\Message\{Request, Response, Stream}; use InitPHP\Router\Router; use \Psr\Http\Message\{RequestInterface, ResponseInterface}; diff --git a/tests/Unit/ResourceAutomaticRouterTest.php b/tests/Unit/ResourceAutomaticRouterTest.php index 5d39540..71bc279 100644 --- a/tests/Unit/ResourceAutomaticRouterTest.php +++ b/tests/Unit/ResourceAutomaticRouterTest.php @@ -15,7 +15,7 @@ namespace Tests\Router\Unit; -use \InitPHP\HTTP\{Request, Response, Stream}; +use \InitPHP\HTTP\Message\{Request, Response, Stream}; use InitPHP\Router\Router; use \Psr\Http\Message\{RequestInterface, ResponseInterface}; diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php index 11e2619..bd40b9a 100644 --- a/tests/Unit/RouterTest.php +++ b/tests/Unit/RouterTest.php @@ -15,9 +15,7 @@ namespace Tests\Router\Unit; -use InitPHP\HTTP\Request; -use InitPHP\HTTP\Response; -use InitPHP\HTTP\Stream; +use \InitPHP\HTTP\Message\{Request, Response, Stream}; use InitPHP\Router\Exception\InvalidArgumentException; use InitPHP\Router\Exception\PageNotFoundException; use InitPHP\Router\Router;