Skip to content

Commit

Permalink
Merge pull request #230 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Fixes the http client, validator, container
  • Loading branch information
papac authored May 19, 2023
2 parents 3722bc6 + a255c1f commit 51e8716
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/Configuration/LoggerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function loadFrontLogger(Logger $monolog, $error_handler)
{
$whoops = new \Whoops\Run();

if (app_env('APP_ENV') != 'production') {
if (app_env('APP_DEBUG')) {
$whoops->pushHandler(new PrettyPageHandler());
$whoops->register();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/stubs/middleware.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class {className} implements BaseMiddleware
* @param array $args
* @return mixed
*/
public function process(Request $request, callable $next, array $args = []): void
public function process(Request $request, callable $next, array $args = []): mixed
{
// Code Here

Expand Down
56 changes: 29 additions & 27 deletions src/Container/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,13 @@ public function call(callable|string|array $actions, ?array $param = null): mixe
* like [AppController::class, 'action']
*/
if (count($actions) === 2) {
if (class_exists($actions[0])) {
$actions = [$actions[0] . '::' . $actions[1]];
if (!class_exists($actions[0])) {
throw new InvalidArgumentException(
'The controller ' . $actions[0] . ' is not exists',
E_USER_ERROR
);
}
$actions = [$actions[0] . '::' . $actions[1]];
}

/**
Expand All @@ -181,31 +185,6 @@ public function call(callable|string|array $actions, ?array $param = null): mixe
$actions = (array) $actions['controller'];
}

$functions = [];

/**
* We normalize of the action to execute and
* creation of the dependency injection
*/
foreach ($actions as $key => $action) {
if (is_string($action)) {
array_push($functions, $this->controller($action));
continue;
}

if (!is_callable($action)) {
continue;
}

if (is_array($action) && $action[0] instanceof Closure) {
$injection = $this->injectorForClosure($action[0]);
} else {
$injection = $this->injectorForClosure($action);
}

array_push($functions, ['action' => $action, 'injection' => $injection]);
}

/**
* We load the middleware associated with the action
*/
Expand Down Expand Up @@ -271,6 +250,29 @@ public function call(callable|string|array $actions, ?array $param = null): mixe
return $response->toArray();
}

$functions = [];

/**
* We normalize of the action to execute and
* creation of the dependency injection
*/
foreach ($actions as $key => $action) {
if (is_string($action)) {
array_push($functions, $this->controller($action));
continue;
}
if (!is_callable($action)) {
continue;
}
if (is_array($action) && $action[0] instanceof Closure) {
$injection = $this->injectorForClosure($action[0]);
} else {
$injection = $this->injectorForClosure($action);
}

array_push($functions, ['action' => $action, 'injection' => $injection]);
}

return $this->dispatchControllers($functions, $param);
}

Expand Down
34 changes: 25 additions & 9 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ public function __construct(?string $base_url = null)
throw new \BadFunctionCallException('cURL php is require.');
}

$this->base_url = rtrim($base_url, "/");
if (!is_null($base_url)) {
$this->base_url = rtrim($base_url, "/");
}
}

/**
* Set the base url
*
* @param string $url
* @return void
*/
public function setBaseUrl(string $url): void
{
$this->base_url = rtrim($url, "/");
}

/**
Expand All @@ -53,9 +66,9 @@ public function __construct(?string $base_url = null)
*/
public function get(string $url, array $data = []): Parser
{
$this->initCurl($url);
$params = http_build_query($data);

$this->addFields($data);
$this->init($url . "?" . $params);

curl_setopt($this->ch, CURLOPT_HTTPGET, true);

Expand All @@ -71,7 +84,7 @@ public function get(string $url, array $data = []): Parser
*/
public function post(string $url, array $data = []): Parser
{
$this->initCurl($url);
$this->init($url);

if (!empty($this->attach)) {
curl_setopt($this->ch, CURLOPT_UPLOAD, true);
Expand All @@ -84,6 +97,7 @@ public function post(string $url, array $data = []): Parser
}

curl_setopt($this->ch, CURLOPT_POST, true);

$this->addFields($data);

return new Parser($this->ch);
Expand All @@ -98,7 +112,7 @@ public function post(string $url, array $data = []): Parser
*/
public function put(string $url, array $data = []): Parser
{
$this->initCurl($url);
$this->init($url);

if (!curl_setopt($this->ch, CURLOPT_PUT, true)) {
$this->addFields($data);
Expand Down Expand Up @@ -147,15 +161,17 @@ public function addHeaders(array $headers): HttpClient
* @param string $url
* @return void
*/
private function initCurl(string $url): void
private function init(string $url): void
{
$url = $this->base_url . "/" . trim($url, "/");
if (is_null($this->base_url)) {
$url = $this->base_url . "/" . trim($url, "/");
}

$this->ch = curl_init($url);
$this->ch = curl_init(trim($url, "/"));
}

/**
* Add field
* Add fields
*
* @param array $data
* @return void
Expand Down
65 changes: 13 additions & 52 deletions src/Http/Client/Parser.php → src/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use CurlHandle;

class Parser
class Response
{
/**
* The error message
Expand All @@ -30,11 +30,11 @@ class Parser
private CurlHandle $ch;

/**
* The header
* The headers
*
* @var array
*/
private array $header = [];
private array $headers = [];

/**
* Flag
Expand All @@ -43,13 +43,6 @@ class Parser
*/
private bool $executed = false;

/**
* The attachment collection
*
* @var array
*/
private array $attach = [];

/**
* Parser constructor.
*
Expand Down Expand Up @@ -193,7 +186,7 @@ private function execute(): string

$this->error = curl_error($this->ch);
$this->errno = curl_errno($this->ch);
$this->header = curl_getinfo($this->ch);
$this->headers = curl_getinfo($this->ch);
$this->executed = true;

$this->close();
Expand All @@ -217,7 +210,7 @@ public function getHeaders(): array
$this->execute();
}

return $this->header;
return $this->headers;
}

/**
Expand All @@ -232,7 +225,7 @@ public function getCode(): ?int
$this->execute();
}

return $this->header['http_code'] ?? null;
return $this->headers['http_code'] ?? null;
}

/**
Expand All @@ -247,7 +240,7 @@ public function getExecutionTime(): ?int
$this->execute();
}

return $this->header['total_time'] ?? null;
return $this->headers['total_time'] ?? null;
}

/**
Expand All @@ -262,7 +255,7 @@ public function getConnexionTime(): ?float
$this->execute();
}

return $this->header['connect_time'] ?? null;
return $this->headers['connect_time'] ?? null;
}

/**
Expand All @@ -277,7 +270,7 @@ public function getUploadSize(): ?float
$this->execute();
}

return $this->header['size_upload'] ?? null;
return $this->headers['size_upload'] ?? null;
}

/**
Expand All @@ -292,7 +285,7 @@ public function getUploadSpeed(): ?float
$this->execute();
}

return $this->header['speed_upload'] ?? null;
return $this->headers['speed_upload'] ?? null;
}

/**
Expand All @@ -307,7 +300,7 @@ public function getDownloadSize(): ?float
$this->execute();
}

return $this->header['size_download'] ?? null;
return $this->headers['size_download'] ?? null;
}

/**
Expand All @@ -322,7 +315,7 @@ public function getDownloadSpeed(): ?float
$this->execute();
}

return $this->header['speed_download'] ?? null;
return $this->headers['speed_download'] ?? null;
}

/**
Expand Down Expand Up @@ -367,39 +360,7 @@ public function getContentType(): ?string
$this->execute();
}

return $this->header['content_type'] ?? null;
}

/**
* Add attach file
*
* @param array $attach
* @return void
*/
public function addAttach($attach)
{
$this->attach = array_merge($this->attach, (array) $attach);
}

/**
* Get attached files
*
* @return array
*/
public function getAttach(): array
{
return $this->attach;
}

/**
* Set attach files
*
* @param array $attachs
* @return void
*/
public function setAttach(array $attachs): void
{
$this->attach = $attachs;
return $this->headers['content_type'] ?? null;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function __construct()
}

foreach ($this->input as $key => $value) {
if (!is_array($value) && strlen($value) == 0) {
if (is_string($value) && strlen($value) == 0) {
$value = null;
}

Expand Down Expand Up @@ -351,7 +351,7 @@ public function isAjax(): bool
*/
public function is($match): bool
{
return (bool) preg_match('@' . $match . '@', $this->path());
return (bool) preg_match('@' . addcslashes($match, "/*{()}[]$^") . '@', $this->path());
}

/**
Expand All @@ -362,7 +362,7 @@ public function is($match): bool
*/
public function isReferer($match): bool
{
return (bool) preg_match('@' . $match . '@', $this->referer());
return (bool) preg_match('@' . addcslashes($match, "/*{()}[]$^") . '@', $this->referer());
}

/**
Expand Down Expand Up @@ -574,7 +574,7 @@ public function only($exceptions)
{
$data = [];

if (! is_array($exceptions)) {
if (!is_array($exceptions)) {
$exceptions = func_get_args();
}

Expand All @@ -594,7 +594,7 @@ public function ignore($ignores)
{
$data = $this->input;

if (! is_array($ignores)) {
if (!is_array($ignores)) {
$ignores = func_get_args();
}

Expand Down
Loading

0 comments on commit 51e8716

Please sign in to comment.