Skip to content

Commit

Permalink
Merge pull request #190
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Oct 29, 2024
2 parents 33e152e + af18630 commit 8dd29c5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
23 changes: 20 additions & 3 deletions src/Core/Api/ApiEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BrickLayer\Lay\Core\Api\Enums\ApiReturnType;
use BrickLayer\Lay\Core\Api\Enums\ApiStatus;
use BrickLayer\Lay\Core\CoreException;
use BrickLayer\Lay\Core\LayConfig;
use BrickLayer\Lay\Core\View\DomainResource;
use BrickLayer\Lay\Core\View\ViewBuilder;
Expand Down Expand Up @@ -466,7 +467,6 @@ public static function set_response_header(int|ApiStatus $code, ?ApiReturnType $
* @param int|null $last_mod
* @param array $cache_control
* @return void
* @throws \Exception
*/
public static function add_cache_header(
?int $last_mod = null,
Expand All @@ -476,7 +476,7 @@ public static function add_cache_header(
])] array $cache_control = []
) : void
{
if(headers_sent())
if(headers_sent() || CoreException::$HAS_500)
return;

header_remove("Pragma");
Expand Down Expand Up @@ -907,9 +907,17 @@ private static function matched_uri_obj() : array

/**
* @param Closure $callback_of_controller_method method name of the set controller.
* @param array|null $cache add caching features to your route
* If you wish to retrieve the value of the method, ensure to return it;
*/
public function bind(Closure $callback_of_controller_method) : self {
public function bind(
Closure $callback_of_controller_method,
#[ArrayShape([
'last_mod' => '?int',
'max_age' => 'int|string|null',
'public' => 'bool|null',
])] ?array $cache = null
) : self {
if(!self::$route_found || self::$request_complete)
return $this;

Expand Down Expand Up @@ -941,6 +949,15 @@ public function bind(Closure $callback_of_controller_method) : self {
if(!self::$DEBUG_DUMP_MODE) {
$arguments = self::get_mapped_args();
self::set_return_value($callback_of_controller_method(...$arguments));

if($cache)
self::add_cache_header(
$cache['last_mod'] ?? null,
[
"max_age" => $cache['max_age'] ?? null,
"public" => $cache['public'] ?? true,
]
);
}
}
catch (\TypeError $e){
Expand Down
13 changes: 7 additions & 6 deletions src/Core/CoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class CoreException
{
use IsSingleton;

public static function new(): self
{
return self::instance();
}
public static bool $HAS_500 = false;

private static bool $already_caught = false;
private static bool $show_internal_trace = true;
Expand Down Expand Up @@ -436,8 +433,10 @@ private function show_exception($opt = []): ?array
if(self::$already_caught)
return null;

if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500)
if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500) {
self::$HAS_500 = true;
LayFn::header("HTTP/1.1 500 Internal Server Error");
}

$use_lay_error = $opt['use_lay_error'] ?? true;
$type = $opt['exception_type'];
Expand Down Expand Up @@ -493,8 +492,10 @@ class_alias(get_class($anon_class), $exception_class);
return $act;

if($act['display_error']) {
if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500)
if(LayConfig::get_mode() === LayMode::HTTP && $this->throw_500) {
self::$HAS_500 = true;
LayFn::header("HTTP/1.1 500 Internal Server Error");
}

self::$already_caught = true;
echo $act['error'];
Expand Down
28 changes: 26 additions & 2 deletions src/Core/View/ViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace BrickLayer\Lay\Core\View;

use BrickLayer\Lay\Core\Api\ApiEngine;
use BrickLayer\Lay\Core\Exception;
use BrickLayer\Lay\Core\LayConfig;
use BrickLayer\Lay\Core\Traits\IsSingleton;
Expand Down Expand Up @@ -140,7 +141,20 @@ public function route(string $route, string ...$aliases): self
return $this;
}

public function bind(Closure $handler): self
/**
* Bind a page to a route
* @param Closure $handler
* @param array|null $cache add caching features to your route
* @return $this
*/
public function bind(
Closure $handler,
#[ArrayShape([
'last_mod' => '?int',
'max_age' => 'int|string|null',
'public' => 'bool|null',
])] ?array $cache = null
): self
{
// Cache default page
if (self::$route == self::DEFAULT_ROUTE)
Expand All @@ -167,8 +181,18 @@ public function bind(Closure $handler): self

self::$view_found = true;

if (isset($current_page['page']['title']) || @$current_page['core']['skeleton'] === false)
if (isset($current_page['page']['title']) || @$current_page['core']['skeleton'] === false) {
ViewEngine::new()->paint($current_page);

if($cache)
ApiEngine::add_cache_header(
$cache['last_mod'] ?? null,
[
"max_age" => $cache['max_age'] ?? null,
"public" => $cache['public'] ?? true,
]
);
}
}

return $this;
Expand Down
17 changes: 9 additions & 8 deletions src/Libs/ID/Gen.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class Gen
private static string $confirm_table;
private static string $confirm_column;

/**
* @throws \Exception
*/
public static function uuid($length = 13): string
{
if (function_exists("random_bytes"))
$bytes = random_bytes(ceil($length / 2));

elseif (function_exists("openssl_random_pseudo_bytes"))
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
try {
if (function_exists("random_bytes"))
$bytes = random_bytes(ceil($length / 2));

elseif (function_exists("openssl_random_pseudo_bytes"))
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
} catch (\Exception $e) {
Exception::throw_exception("", "UUIDError", exception: $e);
}

if(!isset($bytes))
Exception::throw_exception("openssl_random_pseudo_bytes or random_bytes doesn't exist!", "NoCryptoFunc");
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/LayDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function date(string|int|null $datetime = null, string $format = "
$format = match ($format_index) {
0 => "Y-m-d",
1 => "H:i:s",
2 => "D d, M Y | h:i a",
2 => "D, d M Y h:i a",
3 => "D, d M Y H:i:s T",
default => $format,
};
Expand Down

0 comments on commit 8dd29c5

Please sign in to comment.