Skip to content

Commit

Permalink
Merge pull request #3 from InitPHP/1.2.1
Browse files Browse the repository at this point in the history
v1.2.1 Request ve Response nesnelerinin new instanceları argüman olarak kullanılabilir.
  • Loading branch information
muhammetsafak authored Mar 20, 2023
2 parents feb5145 + ee8691e commit bbcb6a3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 58 deletions.
52 changes: 21 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
];
```

Expand All @@ -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, []);
Expand All @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
},
"require-dev": {
"phpunit/phpunit": "9.5",
"initphp/http": "^1.0"
"initphp/http": "^2.0"
}
}
51 changes: 30 additions & 21 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.1.3
* @version 1.2.1
* @link https://www.muhammetsafak.com.tr
*/

Expand Down Expand Up @@ -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]?' => '([^/]+)',
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/NamedAndURLCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ResourceAutomaticRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
4 changes: 1 addition & 3 deletions tests/Unit/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bbcb6a3

Please sign in to comment.