Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
## Table of contents
- [Rudra\Router\MiddlewareInterface](#rudra_router_middlewareinterface)
- [Rudra\Router\Router](#rudra_router_router)
- [Rudra\Router\RouterFacade](#rudra_router_routerfacade)
- [Rudra\Router\RouterInterface](#rudra_router_routerinterface)
- [Rudra\Router\Routing](#rudra_router_routing)
- [Rudra\Router\Traits\RouterAnnotationTrait](#rudra_router_traits_routerannotationtrait)
- [Rudra\Router\Traits\RouterRequestMethodTrait](#rudra_router_traits_routerrequestmethodtrait)
<hr>

<a id="rudra_router_middlewareinterface"></a>

### Class: Rudra\Router\MiddlewareInterface
| Visibility | Function |
|:-----------|:---------|
|abstract public|<em><strong>next</strong>( array $chainOfMiddlewares ): void</em><br>|


<a id="rudra_router_router"></a>

### Class: Rudra\Router\Router
##### implements [Rudra\Router\RouterInterface](#rudra_router_routerinterface)
| Visibility | Function |
|:-----------|:---------|
|public|<em><strong>set</strong>( array $route ): void</em><br>|
|private|<em><strong>handleRequestUri</strong>( array $route ): void</em><br>|
|private|<em><strong>handleRequestMethod</strong>(): void</em><br>|
|private|<em><strong>handlePattern</strong>( array $route array $request ): array</em><br>|
|private|<em><strong>setCallable</strong>( array $route $params ): void</em><br>|
|public|<em><strong>directCall</strong>( array $route $params ): void</em><br>|
|private|<em><strong>callActionThroughReflection</strong>( ?array $params string $action object $controller ): void</em><br>|
|private|<em><strong>callActionThroughException</strong>( $params $action $controller ): void</em><br>|
|public|<em><strong>handleMiddleware</strong>( array $chainOfMiddlewares ): void</em><br>|
|public|<em><strong>annotationCollector</strong>( array $controllers bool $getter bool $attributes ): ?array</em><br>|
|protected|<em><strong>handleAnnotationMiddleware</strong>( array $annotation ): array</em><br>|
|public|<em><strong>__construct</strong>( Rudra\Container\Interfaces\RudraInterface $rudra )</em><br>|
|public|<em><strong>rudra</strong>(): Rudra\Container\Interfaces\RudraInterface</em><br>|
|public|<em><strong>get</strong>( array $route ): void</em><br>|
|public|<em><strong>post</strong>( array $route ): void</em><br>|
|public|<em><strong>put</strong>( array $route ): void</em><br>|
|public|<em><strong>patch</strong>( array $route ): void</em><br>|
|public|<em><strong>delete</strong>( array $route ): void</em><br>|
|public|<em><strong>any</strong>( array $route ): void</em><br>|
|public|<em><strong>resource</strong>( array $route array $actions ): void</em><br>|


<a id="rudra_router_routerfacade"></a>

### Class: Rudra\Router\RouterFacade
| Visibility | Function |
|:-----------|:---------|
|public static|<em><strong>__callStatic</strong>( string $method array $parameters ): mixed</em><br>|


<a id="rudra_router_routerinterface"></a>

### Class: Rudra\Router\RouterInterface
| Visibility | Function |
|:-----------|:---------|
|abstract public|<em><strong>set</strong>( array $route ): void</em><br>|
|abstract public|<em><strong>directCall</strong>( array $route $params ): void</em><br>|


<a id="rudra_router_routing"></a>

### Class: Rudra\Router\Routing
| Visibility | Function |
|:-----------|:---------|


<a id="rudra_router_traits_routerannotationtrait"></a>

### Class: Rudra\Router\Traits\RouterAnnotationTrait
| Visibility | Function |
|:-----------|:---------|
|public|<em><strong>annotationCollector</strong>( array $controllers bool $getter bool $attributes ): ?array</em><br>|
|protected|<em><strong>handleAnnotationMiddleware</strong>( array $annotation ): array</em><br>|


<a id="rudra_router_traits_routerrequestmethodtrait"></a>

### Class: Rudra\Router\Traits\RouterRequestMethodTrait
| Visibility | Function |
|:-----------|:---------|
|abstract public|<em><strong>set</strong>( array $route ): void</em><br>|
|public|<em><strong>get</strong>( array $route ): void</em><br>|
|public|<em><strong>post</strong>( array $route ): void</em><br>|
|public|<em><strong>put</strong>( array $route ): void</em><br>|
|public|<em><strong>patch</strong>( array $route ): void</em><br>|
|public|<em><strong>delete</strong>( array $route ): void</em><br>|
|public|<em><strong>any</strong>( array $route ): void</em><br>|
|public|<em><strong>resource</strong>( array $route array $actions ): void</em><br>|
<hr>

###### created with [Rudra-Documentation-Collector](#https://github.com/Jagepard/Rudra-Documentation-Collector)
73 changes: 15 additions & 58 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ class Router implements RouterInterface
use SetRudraContainersTrait;
use RouterRequestMethodTrait;

protected array $reflectionCache = [];
private array $reflectionCache = [];

/**
* @param array $route
* @return void
*/
public function set(array $route): void
{
$httpMethods = str_contains($route['method'], '|')
Expand All @@ -38,63 +34,52 @@ public function set(array $route): void
}
}

/**
* @param array $route
* @return void
*/
protected function handleRequestUri(array $route): void
private function handleRequestUri(array $route): void
{
$this->handleRequestMethod();

$request = $this->rudra->request();
$server = $request->server();

// Проверяем соответствие HTTP-метода
if ($route['method'] !== $server->get('REQUEST_METHOD')) {
return;
}

$uriRaw = $server->get('REQUEST_URI');
$parsed = parse_url($uriRaw);
$requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : '';
$uriSegments = explode('/', $requestPath);

$uriRaw = $server->get('REQUEST_URI');
$parsed = parse_url($uriRaw);
$requestPath = $parsed && isset($parsed['path']) ? ltrim($parsed['path'], '/') : '';
$uriSegments = explode('/', $requestPath);
[$uri, $params] = $this->handlePattern($route, $uriSegments);


if ($uri === $uriSegments) {
$this->setCallable($route, $params);
}
}

protected function handleRequestMethod(): void
private function handleRequestMethod(): void
{
$request = $this->rudra->request();
$requestMethod = $request->server()->get('REQUEST_METHOD');

// Spoofing метода через _method
// Spoofing the method via _method parameter in POST requests
if ($requestMethod === 'POST' && $request->post()->has('_method')) {
$spoofedMethod = strtoupper($request->post()->get('_method'));

if (in_array($spoofedMethod, ['PUT', 'PATCH', 'DELETE'])) {
$requestMethod = $spoofedMethod;
$request->server()->set(['REQUEST_METHOD' => $spoofedMethod]);
}
}

// Обработка PUT/PATCH/DELETE
// Handle PUT, PATCH, and DELETE requests by parsing raw input data
if (in_array($requestMethod, ['PUT', 'PATCH', 'DELETE'])) {
$rawInput = file_get_contents('php://input');
parse_str($rawInput, $data);
$request->{strtolower($requestMethod)}()->set($data);
}
}

/**
* @param array $route
* @param array $request
* @return array
*/
protected function handlePattern(array $route, array $request): array
private function handlePattern(array $route, array $request): array
{
$uri = [];
$params = null;
Expand All @@ -113,7 +98,7 @@ protected function handlePattern(array $route, array $request): array
if (array_key_exists($i, $request)) {
$pattern = $matches[1];
if (preg_match("/^$pattern$/", $request[$i])) {
$uri[] = $request[$i];
$uri[] = $request[$i];
$params[] = $request[$i];
} else {
$uri[] = '!@#$%^&*';
Expand All @@ -129,12 +114,7 @@ protected function handlePattern(array $route, array $request): array
return [$uri, $params];
}

/**
* @param array $route
* @param $params
* @throws RouterException|ReflectionException
*/
protected function setCallable(array $route, $params): void
private function setCallable(array $route, $params): void
{
if ($route['controller'] instanceof \Closure) {
if (is_array($params)) {
Expand All @@ -149,11 +129,6 @@ protected function setCallable(array $route, $params): void
$this->directCall($route, $params);
}

/**
* @param array $route
* @param $params
* @return void
*/
public function directCall(array $route, $params = null): void
{
$controller = $this->rudra->get($route['controller']);
Expand All @@ -163,7 +138,6 @@ public function directCall(array $route, $params = null): void
throw new RouterException("503");
}

// Bootstrap controller
$controller->shipInit();
$controller->containerInit();
$controller->init();
Expand All @@ -189,13 +163,7 @@ public function directCall(array $route, $params = null): void
}
}

/**
* @param $params
* @param $action
* @param $controller
* @return void
*/
protected function callActionThroughReflection(?array $params, string $action, object $controller): void
private function callActionThroughReflection(?array $params, string $action, object $controller): void
{
if ($params && in_array('', $params, true)) {
throw new RouterException("404");
Expand All @@ -213,15 +181,7 @@ protected function callActionThroughReflection(?array $params, string $action, o
$method->invokeArgs($controller, $arguments);
}

/**
* @deprecated
*
* @param $params
* @param $action
* @param $controller
* @return void
*/
protected function callActionThroughException($params, $action, $controller): void
private function callActionThroughException($params, $action, $controller): void
{
if (isset($params) && in_array('', $params)) {
throw new RouterException("404");
Expand All @@ -242,9 +202,6 @@ protected function callActionThroughException($params, $action, $controller): vo
}
}

/**
* @param array $chainOfMiddlewares
*/
public function handleMiddleware(array $chainOfMiddlewares): void
{
if (!$chainOfMiddlewares) {
Expand Down
9 changes: 0 additions & 9 deletions src/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@

interface RouterInterface
{
/**
* @param array $route
*/
public function set(array $route): void;

/**
* @param array $route
* @param null $params
* @throws RouterException
*/
public function directCall(array $route, $params = null): void;
}
4 changes: 3 additions & 1 deletion src/Routing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* @author : Jagepard <jagepard@yandex.ru">
* @license https://mit-license.org/ MIT
Expand All @@ -11,4 +13,4 @@
class Routing
{

}
}
2 changes: 2 additions & 0 deletions tests/RouterAnnotationTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* @author : Jagepard <jagepard@yandex.ru">
* @license https://mit-license.org/ MIT
Expand Down
Loading