Skip to content

Commit bbcb6a3

Browse files
Merge pull request #3 from InitPHP/1.2.1
v1.2.1 Request ve Response nesnelerinin new instanceları argüman olarak kullanılabilir.
2 parents feb5145 + ee8691e commit bbcb6a3

File tree

6 files changed

+55
-58
lines changed

6 files changed

+55
-58
lines changed

README.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ server {
5656
}
5757
location ~ \.php$ {
5858
fastcgi_split_path_info ^(.+\.php)(/.+)$;
59-
fastcgi_pass unix:/var/run/php5-fpm.sock;
59+
fastcgi_pass unix:/var/run/php7.4-fpm.sock;
6060
fastcgi_index index.php;
6161
include fastcgi.conf;
6262
fastcgi_intercept_errors on;
@@ -68,16 +68,17 @@ server {
6868

6969
```php
7070
$config = [
71-
'paths' => [
72-
'controller' => null, //The full path to the directory where the Controller classes are kept.
73-
'middleware' => null, //The full path to the directory where the Middleware classes are kept.
71+
'paths' => [
72+
'controller' => null, //The full path to the directory where the Controller classes are kept.
73+
'middleware' => null, //The full path to the directory where the Middleware classes are kept.
7474
],
75-
'namespaces' => [
76-
'controller' => null, //Namespace prefix of Controller classes, if applicable.
77-
'middleware' => null, //Namespace prefix of Middleware classes, if applicable.
75+
'namespaces' => [
76+
'controller' => null, //Namespace prefix of Controller classes, if applicable.
77+
'middleware' => null, //Namespace prefix of Middleware classes, if applicable.
7878
],
79-
'base_path' => '/', // If you are working in a subdirectory; identifies your working directory.
80-
'variable_method' => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method'].
79+
'base_path' => '/', // If you are working in a subdirectory; identifies your working directory.
80+
'variable_method' => false, // It makes the request method mutable with Laravel-like $_REQUEST['_method'].
81+
'argument_new_instance' => false, // This configuration is used for Request and Response objects that you want as arguments.
8182
];
8283
```
8384

@@ -93,30 +94,12 @@ _**See the Wiki for detailed documentation.**_
9394

9495
```php
9596
require_once "vendor/autoload.php";
96-
use \InitPHP\HTTP\{Request, Response, Stream, Emitter};
97+
use \InitPHP\HTTP\Message\{Request, Response, Stream};
98+
use \InitPHP\HTTP\Emitter\Emitter;
9799
use \InitPHP\Router\Router;
98100

99-
if(($headers = function_exists('apache_request_headers') ? apache_request_headers() : []) === FALSE){
100-
$headers = [];
101-
}
102-
103-
$uri = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http')
104-
. '://'
105-
. ($_SERVER['SERVER_NAME'] ?? 'localhost')
106-
. (isset($_SERVER['SERVER_PORT']) && !\in_array($_SERVER['SERVER_PORT'], [80, 443]) ? ':' . $_SERVER['SERVER_PORT'] : '')
107-
. ($_SERVER['REQUEST_URI'] ?? '/');
108-
109-
// Construct the HTTP request object.
110-
$request = new Request(
111-
($_SERVER['REQUEST_METHOD'] ?? 'GET'),
112-
$uri,
113-
$headers,
114-
null,
115-
'1.1'
116-
);
117-
118-
// Create a new HTTP response object.
119-
$response = new Response(200, [], (new Stream('', null)), '1.1');
101+
$request = Request::createFromGlobals();
102+
$response = new Response();
120103

121104
// Create the router object.
122105
$router = new Router($request, $response, []);
@@ -126,6 +109,13 @@ $router->get('/', function () {
126109
return 'Hello World!';
127110
});
128111

112+
$router->post('/login', function (Request $request, Response $response) {
113+
return $response->json([
114+
'status' => 0,
115+
'message' => 'Unauthorized',
116+
], 401);
117+
});
118+
129119
// If you do not make a definition for 404 errors; An exception is thrown if there is no match with the request.
130120
$router->error_404(function () {
131121
echo 'Page Not Found';

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "9.5",
31-
"initphp/http": "^1.0"
31+
"initphp/http": "^2.0"
3232
}
3333
}

src/Router.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <[email protected]>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.1.3
10+
* @version 1.2.1
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -45,18 +45,21 @@ class Router
4545
protected $cache_status = false;
4646

4747
protected $configs = [
48-
'paths' => [
49-
'controller' => null,
50-
'middleware' => null,
48+
'paths' => [
49+
'controller' => null,
50+
'middleware' => null,
5151
],
52-
'namespaces' => [
53-
'controller' => null,
54-
'middleware' => null,
52+
'namespaces' => [
53+
'controller' => null,
54+
'middleware' => null,
5555
],
56-
'base_path' => '/',
57-
'variable_method' => false,
56+
'base_path' => '/',
57+
'variable_method' => false,
58+
'argument_new_instance' => false,
5859
];
5960

61+
protected $argumentNewInstance = false;
62+
6063
protected $patterns = [
6164
'{[^/]+}' => '([^/]+)',
6265
':any[0-9]?' => '([^/]+)',
@@ -134,6 +137,8 @@ public function __construct(RequestInterface $request, ResponseInterface $respon
134137
unset($configs['cache']);
135138
}
136139
$this->configs = \array_merge($this->configs, $configs);
140+
$this->argumentNewInstance = $this->configs['argument_new_instance'];
141+
unset($this->configs['argument_new_instance']);
137142
$this->request = &$request;
138143
$this->response = &$response;
139144
$this->uri = new Uri($this->request->getUri()->__toString());
@@ -1016,13 +1021,15 @@ private function resolveParameters(\Reflector $reflector, array $parameters): ar
10161021
}
10171022
continue;
10181023
}
1019-
if($class->isInstance($this->request)){
1020-
$arguments[] = $this->request;
1021-
continue;
1022-
}
1023-
if($class->isInstance($this->response)){
1024-
$arguments[] = $this->response;
1025-
continue;
1024+
if (!$this->argumentNewInstance) {
1025+
if($class->isInstance($this->request)){
1026+
$arguments[] = $this->request;
1027+
continue;
1028+
}
1029+
if($class->isInstance($this->response)){
1030+
$arguments[] = $this->response;
1031+
continue;
1032+
}
10261033
}
10271034
if($class->isInstantiable()){
10281035
$arguments[] = $this->getClassContainer($class);
@@ -1033,11 +1040,13 @@ private function resolveParameters(\Reflector $reflector, array $parameters): ar
10331040

10341041
private function getClassContainer(\ReflectionClass $class): object
10351042
{
1036-
if($class->isInstance($this->request)){
1037-
return $this->request;
1038-
}
1039-
if($class->isInstance($this->response)){
1040-
return $this->response;
1043+
if (!$this->argumentNewInstance) {
1044+
if($class->isInstance($this->request)){
1045+
return $this->request;
1046+
}
1047+
if($class->isInstance($this->response)){
1048+
return $this->response;
1049+
}
10411050
}
10421051
if(isset($this->container) && is_object($this->container)){
10431052
return $this->container->get($class->getName());

tests/Unit/NamedAndURLCreateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Tests\Router\Unit;
1717

18-
use \InitPHP\HTTP\{Request, Response, Stream};
18+
use \InitPHP\HTTP\Message\{Request, Response, Stream};
1919
use InitPHP\Router\Router;
2020
use \Psr\Http\Message\{RequestInterface, ResponseInterface};
2121

tests/Unit/ResourceAutomaticRouterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Tests\Router\Unit;
1717

18-
use \InitPHP\HTTP\{Request, Response, Stream};
18+
use \InitPHP\HTTP\Message\{Request, Response, Stream};
1919
use InitPHP\Router\Router;
2020
use \Psr\Http\Message\{RequestInterface, ResponseInterface};
2121

tests/Unit/RouterTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
namespace Tests\Router\Unit;
1717

18-
use InitPHP\HTTP\Request;
19-
use InitPHP\HTTP\Response;
20-
use InitPHP\HTTP\Stream;
18+
use \InitPHP\HTTP\Message\{Request, Response, Stream};
2119
use InitPHP\Router\Exception\InvalidArgumentException;
2220
use InitPHP\Router\Exception\PageNotFoundException;
2321
use InitPHP\Router\Router;

0 commit comments

Comments
 (0)