Skip to content

Commit

Permalink
Merge pull request #49
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
leonardosahon authored Feb 3, 2024
2 parents 72a33c3 + ceb9ad3 commit d24a73b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 30 deletions.
14 changes: 7 additions & 7 deletions src/BobDBuilder/Cmd/Traits/Make/Brick.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ public function hooks(): void
* Default model file
*/
// delete placeholder file
unlink($brick_dir . $this->plug->s . "model" . $this->plug->s . "model.php");
unlink($brick_dir . $this->plug->s . "Model" . $this->plug->s . "model.php");

// make brick default model file
file_put_contents(
$brick_dir . $this->plug->s . "model" . $this->plug->s . "$brick.php",
$brick_dir . $this->plug->s . "Model" . $this->plug->s . "$brick.php",
<<<FILE
<?php
declare(strict_types=1);
namespace bricks\\$brick\model;
namespace bricks\\$brick\Model;
use JetBrains\PhpStorm\ArrayShape;
use BrickLayer\Lay\Orm\SQL;
Expand Down Expand Up @@ -160,22 +160,22 @@ public function add(array \$columns) : array
*/

// delete placeholder file
unlink($brick_dir . $this->plug->s . "controller" . $this->plug->s . "controller.php");
unlink($brick_dir . $this->plug->s . "Controller" . $this->plug->s . "controller.php");

// make brick default controller
file_put_contents(
$brick_dir . $this->plug->s . "controller" . $this->plug->s . "$brick_plural.php",
$brick_dir . $this->plug->s . "Controller" . $this->plug->s . "$brick_plural.php",
<<<FILE
<?php
declare(strict_types=1);
namespace bricks\\$brick\model;
namespace bricks\\$brick\Controller;
use JetBrains\PhpStorm\ArrayShape;
use BrickLayer\Lay\Orm\SQL;
$import
use bricks\\$brick\model\\$brick;
use bricks\\$brick\Model\\$brick;
class $brick_plural
{
Expand Down
70 changes: 56 additions & 14 deletions src/Core/Api/ApiEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);
namespace BrickLayer\Lay\Core\Api;

use BrickLayer\Lay\Core\Api\Enums\ApiReturnType;
use BrickLayer\Lay\Core\View\ViewBuilder;
use Closure;
use BrickLayer\Lay\Core\Api\Enums\ApiRequestMethod;
Expand All @@ -13,10 +14,12 @@ public static function new() : self {
}

private static string $request_uri_raw;
private static array $registered_uris = [];
private static array $request_uri = [];
private static array $request_header;
private static array $method_arguments;
private static mixed $method_return_value;
private static ApiReturnType $method_return_type = ApiReturnType::JSON;
private static bool $use_lay_exception = true;
private static bool $request_found = false;
private static bool $request_complete = false;
Expand Down Expand Up @@ -54,11 +57,12 @@ private function correct_request_method(bool $throw_exception = true) : bool {
* @example `post/user/index/25`; translates to => `'post','user','index','{@int id}'`
* @return $this
*/
private function map_request(string $request_uri) : self {
private function map_request(string $request_uri, ApiReturnType $return_type) : self {
if(self::$request_found || self::$request_complete || !$this->correct_request_method(false))
return $this;

self::$method_arguments = [];
self::$method_return_type = $return_type;
$uri_text = "";
$request_uri = trim($request_uri, "/");
$request_uri = explode("/", $request_uri);
Expand All @@ -70,6 +74,12 @@ private function map_request(string $request_uri) : self {
if(isset(self::$prefix))
$request_uri = [self::$prefix, ...$request_uri];

self::$registered_uris[] = [
"uri" => implode("/",$request_uri),
"method" => self::$request_method,
"return_type" => self::$method_return_type,
];

if(count(self::$request_uri) !== count($request_uri))
return $this;

Expand Down Expand Up @@ -185,29 +195,34 @@ public function groups(\Closure ...$grouped_requests) : self {
return $this;
}

public function post(string $request_uri) : self {
public function post(string $request_uri, ApiReturnType $return_type = ApiReturnType::JSON) : self {
self::$request_method = ApiRequestMethod::POST->value;
return $this->map_request($request_uri);

return $this->map_request($request_uri, $return_type);
}

public function get(string $request_uri) : self {
public function get(string $request_uri, ApiReturnType $return_type = ApiReturnType::JSON) : self {
self::$request_method = ApiRequestMethod::GET->value;
return $this->map_request($request_uri);

return $this->map_request($request_uri, $return_type);
}

public function put(string $request_uri) : self {
public function put(string $request_uri, ApiReturnType $return_type = ApiReturnType::JSON) : self {
self::$request_method = ApiRequestMethod::PUT->value;
return $this->map_request($request_uri);

return $this->map_request($request_uri, $return_type);
}

public function head(string $request_uri) : self {
public function head(string $request_uri, ApiReturnType $return_type = ApiReturnType::JSON) : self {
self::$request_method = ApiRequestMethod::HEAD->value;
return $this->map_request($request_uri);

return $this->map_request($request_uri, $return_type);
}

public function delete(string $request_uri) : self {
public function delete(string $request_uri, ApiReturnType $return_type = ApiReturnType::JSON) : self {
self::$request_method = ApiRequestMethod::DELETE->value;
return $this->map_request($request_uri);

return $this->map_request($request_uri, $return_type);
}

/**
Expand All @@ -229,7 +244,7 @@ public function bind(Closure $callback_of_controller_method) : self {
self::$request_complete = true;
}
catch (\TypeError $e){
self::exception("ApiEngineMethodError", "Check the bind function of your route: [". self::$request_uri_raw ."]; <br>" . $e->getMessage(), $e);
self::exception("ApiEngineMethodError", "Check the bind function of your route: [" . self::$request_uri_raw . "]; <br>" . $e->getMessage(), $e);
}
catch (\Error|\Exception $e){
self::exception("ApiEngineError", $e->getMessage(), $e);
Expand All @@ -238,6 +253,11 @@ public function bind(Closure $callback_of_controller_method) : self {
return $this;
}

public function get_registered_uris() : array
{
return self::$registered_uris;
}

public function get_result() : mixed {
// Clear the prefix, because this method marks the end of a set of api routes
self::$prefix = null;
Expand All @@ -256,16 +276,38 @@ public function get_result() : mixed {
* @return string|bool|null Returns `null` when no api was his; Returns `false` on error; Returns json encoded string on success
*/
public function print_as_json(bool $print = true) : string|bool|null {
return $this->print_as(self::$method_return_type ?? ApiReturnType::JSON, $print);
}

/**
* @param ApiReturnType $return_type
* @param bool $print
* @return string|bool|null Returns `null` when no api was hit; Returns `false` on error; Returns json encoded string or html on success,
* depending on what was selected as `$return_type`
*/
public function print_as(?ApiReturnType $return_type = null, bool $print = true) : string|bool|null {
if(!isset(self::$method_return_value))
return null;

// Clear the prefix, because this method marks the end of a set of api routes
self::$prefix = null;
$return_type ??= self::$method_return_type;

$x = json_encode(self::$method_return_value);
$x = $return_type == ApiReturnType::JSON ? json_encode(self::$method_return_value) : self::$method_return_value;

if($print) {
header("Content-Type: application/json");
switch ($return_type){
case ApiReturnType::JSON:
header("Content-Type: application/json");
break;
case ApiReturnType::HTML:
header("Content-Type: text/html");
break;
default:
header("Content-Type: text/plain");
break;
}

print_r($x);
die;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Core/Api/ApiHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function hooks() : void
{
$this->request::fetch();
$this->load_brick_hooks();
$this->request->print_as_json();
$this->request->print_as();
}

public final function load_brick_hooks(string ...$class_to_ignore) : void
Expand All @@ -49,9 +49,6 @@ public final function load_brick_hooks(string ...$class_to_ignore) : void

$cmd_class = "bricks\\$brick\\Api\\Hook";

if(class_exists($cmd_class))
continue;

if(in_array($cmd_class, $class_to_ignore, true))
continue;

Expand Down
10 changes: 10 additions & 0 deletions src/Core/Api/Enums/ApiReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BrickLayer\Lay\Core\Api\Enums;

enum ApiReturnType : string
{
case JSON = "JSON";
case HTML = "HTML";
case STRING = "STRING";
}
4 changes: 3 additions & 1 deletion src/Core/View/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ private function active_pattern() : array {
return [
"sub" => [
"value" => $sub_domain[0],
"found" => !($sub_domain[0] == "www") && count($sub_domain) > 2,
"found" => !($sub_domain[0] == "www")
&& !(is_numeric($sub_domain[0]) && is_numeric($sub_domain[1]))
&& count($sub_domain) > 2,
],
"local" => [
"value" => $local_dir[0],
Expand Down
8 changes: 7 additions & 1 deletion src/Core/View/ViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function bind(Closure $handler): self

self::$view_found = true;

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

Expand Down Expand Up @@ -225,6 +225,12 @@ public function request(#[ExpectedValues(['route', 'route_as_array', 'domain_typ
return self::$current_route_data[$key] ?? '';
}

#[NoReturn] public function relocate(string $url, ?string $domain_id = null): void
{
header("location: " . Anchor::new()->href($url, $domain_id)->get_href());
die;
}

#[NoReturn] public function redirect(string $route, ViewCast $viewCast): void
{
if (self::$view_found)
Expand Down
17 changes: 14 additions & 3 deletions src/Core/View/ViewEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public function paint(array $page_data) : void {
if(empty(self::$constant_attributes))
self::constants([]);


$layConfig = LayConfig::new();
$data = $layConfig::site_data();


$const = array_replace_recursive(self::$constant_attributes, $page_data);;

$const[self::key_page]['title_raw'] = $const[self::key_page]['title'];
Expand All @@ -115,7 +115,11 @@ public function paint(array $page_data) : void {
unset($const[self::key_assets]);

self::$meta_data = LayObject::new()->to_object($const);
$this->create_html_page();

if($const[self::key_core]['skeleton'])
$this->create_html_page();
else
echo $this->skeleton_body();
}

private function create_html_page() : void {
Expand Down Expand Up @@ -212,8 +216,8 @@ private function skeleton_head() : string
private function skeleton_body() : string
{
ob_start();

$this->add_view_section(self::key_body);

echo $this->core_script();
$this->add_view_section(self::key_script);

Expand Down Expand Up @@ -247,6 +251,13 @@ private function add_view_section(string $view_section) : void
// That is: body.inc for `body section`, head.inc for `head section`.
if($meta->{self::key_core}->skeleton === true)
$this->insert_view($view_section, "inc", false);

// The echos the body value without the skeleton of Lay.
// This only works with the `body` method, it doesn't work with `head` or `script` method
else {
DomainResource::make_plaster(self::$meta_data);
echo self::$meta_data->{$view_section};
}
}

private function insert_view(?string $file, string $type, bool $as_string) : ?string
Expand Down

0 comments on commit d24a73b

Please sign in to comment.