Skip to content

Commit

Permalink
Resolve query params
Browse files Browse the repository at this point in the history
  • Loading branch information
marriosdev committed Jul 6, 2023
1 parent fb0693d commit ef5bf72
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 70 deletions.
86 changes: 43 additions & 43 deletions router.log
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
[2023-06-27 22:21:43] - ::1 - ok
[2023-06-27 22:23:40] - ::1 - ok
[2023-06-27 22:24:24] - ::1 - ok
[2023-06-27 22:32:26] - ::1 - ok
[2023-06-27 22:32:56] - ::1 - ok
[2023-06-27 22:33:09] - ::1 - ok
[2023-06-27 22:41:53] - ::1 - ok
[2023-06-27 22:42:00] - ::1 - ok
[2023-06-27 22:42:05] - ::1 - ok
[2023-06-27 22:42:25] - ::1 - ok2
[2023-06-27 22:42:29] - ::1 - ok2
[2023-06-27 22:43:13] - ::1 - ok2
[2023-06-27 22:43:22] - ::1 - ok2
[2023-06-27 22:56:02] - ::1 - ok
[2023-06-27 22:56:35] - ::1 - ok
[2023-06-27 23:18:03] - ::1 - ok
[2023-06-27 23:18:10] - ::1 - ok
[2023-06-27 23:22:28] - ::1 - ok
[2023-06-27 23:24:39] - ::1 - ok
[2023-06-27 23:34:13] - ::1 - ok
[2023-06-27 23:34:38] - ::1 - ok
[2023-06-27 23:34:44] - ::1 - ok
[2023-06-27 23:40:49] - ::1 - ok
[2023-06-27 23:42:00] - ::1 - ok
[2023-06-27 23:42:23] - ::1 - ok
[2023-06-27 23:53:33] - ::1 - ok
[2023-06-27 23:53:39] - ::1 - ok
[2023-06-27 23:53:44] - ::1 - ok2
[2023-06-27 23:53:48] - ::1 - ok3
[2023-06-27 23:54:17] - ::1 - ok3
[2023-06-28 00:02:40] - ::1 - ok3
[2023-06-28 00:03:30] - ::1 - ok3
[2023-06-28 00:03:39] - ::1 - ok3
[2023-06-28 00:04:48] - ::1 - ok3
[2023-06-28 00:04:55] - ::1 - ok
[2023-06-28 00:05:12] - ::1 - ok
[2023-06-28 00:06:18] - ::1 - ok
[2023-06-28 00:07:34] - ::1 - ok
[2023-06-28 00:08:14] - ::1 - ok
[2023-06-28 00:09:18] - ::1 - ok
[2023-06-28 00:11:58] - ::1 - ok
[2023-06-28 00:12:10] - ::1 - ok
[2023-06-28 00:12:25] - ::1 - ok

IP: ::1
Date: 2023-06-28 21:29:37
Url: ok
Task execution time: 0.00034403800964355
Execution time all process: 0.00059103965759277
--------------------------------------
IP: ::1
Date: 2023-06-28 21:29:38
Url: ok
Task execution time: 0.00025391578674316
Execution time all process: 0.00061893463134766
--------------------------------------
IP: ::1
Date: 2023-06-28 21:29:40
Url: ok
Task execution time: 0.00021004676818848
Execution time all process: 0.00047111511230469
--------------------------------------
IP: ::1
Date: 2023-07-06 23:38:56
Url: ok/teste
Task Time: 0.00036907196044922
Process Time: 0.00066304206848145
--------------------------------------
IP: ::1
Date: 2023-07-06 23:38:58
Url: ok/teste?dsad
Task Time: 0.00039196014404297
Process Time: 0.00082707405090332
--------------------------------------
IP: ::1
Date: 2023-07-06 23:39:20
Url: ok/teste?teste=teste
Task Time: 0.00055694580078125
Process Time: 0.00085186958312988
--------------------------------------
IP: ::1
Date: 2023-07-06 23:39:26
Url: ok/teste?teste=teste
Task Time: 0.00027322769165039
Process Time: 0.00076913833618164
--------------------------------------
12 changes: 3 additions & 9 deletions src/Group.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
<?php

namespace Marrios\Router;
use Closure;

trait Group
{
private bool $runGroup = false;

public function group(Array $routes) : void
public function group(Closure $closure) : void
{
foreach($routes as $route) {
$route->run();
}

$this->runGroup = false;
$this->middlewareAccess = true;
$closure($this);
}
}
36 changes: 21 additions & 15 deletions src/HttpRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,35 @@ class HttpRouter
*/
public $currentUrl;

/**
* @var float
*/
protected $allProcessExecutionTime;

/**
* @var float
*/
protected $timeTaskExecution;

/**
*
*/
public function __construct()
{
$this->currentUrl = new Url($_SERVER["REQUEST_URI"]);
$this->allProcessExecutionTime = microtime(true);
}

/**
* Processing routes
*/
public function run()
{
// If it is running in a group of routes, it will only execute the routes after they are grouped.
// So we stop the code here and let the Group perform the necessary steps
if ($this->runGroup) {
$instanceClone = clone $this;
$instanceClone->runGroup = false;
return $instanceClone;
}

$currentUrl = $this->currentUrl;
$routePath = $this->definedRoute->route;
$url = new Url($this->definedRoute->route);

$this->timeTaskExecution = microtime(true);

if ($this->definedRoute->method == $_SERVER["REQUEST_METHOD"]) {
$urlsMatched = $this->matched($currentUrl, $url);
Expand All @@ -65,6 +70,10 @@ public function run()
if ($this->middlewareAccess) {
$this->execute($this->definedRoute->routeAction, $urlParams);
$this->middlewareAccess = true;

$this->timeTaskExecution = microtime(true) - $this->timeTaskExecution;
$this->allProcessExecutionTime = microtime(true) - $this->allProcessExecutionTime;

$this->startLogs($this);
exit;
}
Expand Down Expand Up @@ -105,10 +114,6 @@ public function matched(Url $currentUrl, Url $definedUrl)

for ($i = 0; $i < $countMatch; $i++) {
if ($definedUrl[$i] != $currentUrl[$i]) {
/**
* Checking if this parameter the parameter is dynamic
* If it is dynamic, we disregard the difference
*/
if (!preg_match("/[{}]/", $definedUrl[$i])) {
return false;
}
Expand All @@ -132,8 +137,9 @@ public function matched(Url $currentUrl, Url $definedUrl)
*/
public function getUrlParams(Url $url)
{
$url = explode("/", $url->get());
$currentUrl = explode("/", $this->currentUrl->get());

$url = explode("/", explode("?", $url->get())[0]);
$currentUrl = explode("/", explode("?", $this->currentUrl->get())[0]);

$paramsList = new RouteParameters();

Expand Down
4 changes: 3 additions & 1 deletion src/Logs/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Log
public function __construct(
public String $ip,
public DateTime $dateTime,
public Url $url
public Url $url,
public float $timeTaskExecution,
public float $allProcessExecutionTime,
){
}
}
11 changes: 9 additions & 2 deletions src/Logs/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ public function startLogs(HttpRouter $routerInstance )
$log = new Log(
ip: $_SERVER["REMOTE_ADDR"],
dateTime: new \DateTime(),
url: $routerInstance->currentUrl
url: $routerInstance->currentUrl,
timeTaskExecution: $routerInstance->timeTaskExecution,
allProcessExecutionTime: $routerInstance->allProcessExecutionTime
);

$this->register($log);
}

private function formatLog(Log $log) : String
{
$log = "[{$log->dateTime->format('Y-m-d H:i:s')}] - {$log->ip} - {$log->url->get()}\n";
$log = "IP: " . $log->ip . "\n" .
"Date: " . $log->dateTime->format("Y-m-d H:i:s") . "\n" .
"Url: " . $log->url->get() . "\n" .
"Task Time: " . $log->timeTaskExecution . "\n" .
"Process Time: " . $log->allProcessExecutionTime . "\n" .
"--------------------------------------\n";
return $log;
}

Expand Down

0 comments on commit ef5bf72

Please sign in to comment.