Skip to content

Commit

Permalink
Merge pull request #11
Browse files Browse the repository at this point in the history
Road to beta
  • Loading branch information
leonardosahon authored Dec 26, 2023
2 parents cf621e0 + b2ad5c6 commit 85fc4c7
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 33 deletions.
15 changes: 15 additions & 0 deletions __internal/Brick/Api/Hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace bricks\SystemUser\Api;

use BrickLayer\Lay\Core\Api\ApiEngine;
use BrickLayer\Lay\Core\Api\ApiHooks;

class Hook extends ApiHooks
{
public function hooks(): void
{

}
}
5 changes: 5 additions & 0 deletions __internal/Domain/foundation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

// Do all the domain-level foundational things here
// You can overwrite the default logo and icon of the project.
// Anything you do here will reflect across all routes on this domain.
10 changes: 9 additions & 1 deletion __internal/Domain/layout/body.inc
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
<?php
echo \BrickLayer\Lay\core\view\DomainResource::plaster()->body;

use BrickLayer\Lay\core\view\DomainResource;

echo DomainResource::plaster()->body;
?>

<div class="copyright">
<?= DomainResource::get()->copyright ?>
</div>
4 changes: 3 additions & 1 deletion __internal/Domain/plaster/homepage.view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ $plaster = DomainResource::plaster();
$href = $plaster->local->href;
?>

<div style="text-align: center"><img src="<?= $plaster->local->logo ?>" alt="Lay Logo"></div>
<div style="text-align: center">
<?= \BrickLayer\Lay\Core\View\Tags\Img::new()->alt("Lay Logo")->src($plaster->local->logo) ?>
</div>
<h1><?= $plaster->page->title ?></h1>
<p>This is the default index page of Lay a lite php framework</p>
<p>Modify the necessary things to start your project</p>
Expand Down
5 changes: 5 additions & 0 deletions __internal/Domain/static/dev/css/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
body {
border: solid 2px #e00;
}

.copyright {
position: fixed;
bottom: 15px;
}
12 changes: 8 additions & 4 deletions src/Core/Api/ApiEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ private function correct_request_method(bool $throw_exception = true) : bool {
return true;

if($throw_exception)
self::exception("InvalidRequestMethod", "Invalid request method received, please use a valid request verb");
self::exception(
"UnmatchedRequestMethod",
"Request method for api request [". self::$request_uri_raw ."]; don't match.
Check if you have binded you route to a method, it could be using the method of the previous route"
);

return false;
}
Expand Down Expand Up @@ -145,9 +149,9 @@ public function clear_prefix() : void {
* `user/register`
* `user/login`
* `$req->group("user", function(LayRequestHandler $req) {
$req->post("register")->bind(fn() => SystemUsers::new()->register())
->post("login")->bind(fn() => SystemUsers::new()->login());
})`
$req->post("register")->bind(fn() => SystemUsers::new()->register())
->post("login")->bind(fn() => SystemUsers::new()->login());
})`
*/
public function group(string $name, \Closure $grouped_requests) : self {
if(self::$request_complete)
Expand Down
48 changes: 45 additions & 3 deletions src/Core/Api/ApiHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BrickLayer\Lay\Core\Api;

use BrickLayer\Lay\Core\Exception;
use BrickLayer\Lay\Core\LayConfig;

abstract class ApiHooks
Expand All @@ -18,16 +19,57 @@ public function __construct(
public final function init() : void
{
LayConfig::connect();

if($this->prefetch)
$this->request::fetch();

$this->hooks();
$this->request::end();
}

public function hooks() : void
{
$this->request::fetch();
$this->load_brick_hooks();
$this->request->print_as_json();
}

public function load_brick_hooks(string ...$namespaces) : void
{
$bricks_root = LayConfig::server_data()->bricks;

foreach (scandir($bricks_root) as $brick) {
if (
$brick == "." ||
$brick == ".." ||
!file_exists($bricks_root . $brick . DIRECTORY_SEPARATOR . "Api" . DIRECTORY_SEPARATOR . "Hook.php")
)
continue;

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

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

$cmd_class = "\\$cmd_class";

try{
$brick = new \ReflectionClass($cmd_class);
} catch (\ReflectionException $e){
Exception::throw_exception($e->getMessage(), "ReflectionException");
}

try {
$brick = $brick->newInstance();
} catch (\ReflectionException $e) {
Exception::throw_exception($e->getMessage(), "$brick::ApiError");
}

try {
$brick->hooks();
} catch (\Error|\Exception $e) {
Exception::throw_exception($e->getMessage(), "$brick::HookError");
}
}
}
}
14 changes: 14 additions & 0 deletions src/Core/View/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function activate_domain(string $id, string $pattern, ViewCast|ApiHooks
$this->cache_active_domain($id, $pattern);

$file = explode("\\", $builder::class);

array_pop($file);
$domain_file = $file;
array_shift($domain_file);
Expand All @@ -146,6 +147,19 @@ private function activate_domain(string $id, string $pattern, ViewCast|ApiHooks
self::$current_route_details['domain_base'] = $data->domain . $domain_base;
self::$current_route_details['domain_root'] = LayConfig::server_data()->root . implode(DIRECTORY_SEPARATOR, $file) . DIRECTORY_SEPARATOR;

// Init domain resources before including the domain-level foundation file so the data can be manipulated
DomainResource::init();

// Include domain-level foundation file
$web_root = LayConfig::server_data()->web;

if(file_exists(self::$current_route_details['domain_root'] . "foundation.php"))
include_once self::$current_route_details['domain_root'] . "foundation.php";

if(file_exists($web_root . "foundation.php"))
include_once $web_root . "foundation.php";

// init domain
$builder->init();
}

Expand Down
11 changes: 9 additions & 2 deletions src/Core/View/DomainResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BrickLayer\Lay\Core\LayConfig;
use BrickLayer\Lay\Core\Traits\IsSingleton;
use BrickLayer\Lay\Core\View\Enums\DomainType;
use BrickLayer\Lay\Libs\LayObject;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\ObjectShape;

Expand Down Expand Up @@ -74,12 +75,18 @@ public static function init() : void
"root" => $domain->domain_root . "shared" . DIRECTORY_SEPARATOR . "lay" . DIRECTORY_SEPARATOR,
];

if(isset(self::$resource))
$obj = (object) array_merge((array) $obj, (array) self::$resource);

self::$resource = $obj;
}

public static function set_res(string $key, mixed $value) : void
{
self::$resource->others->key = $value;
if(!isset(self::$resource))
self::$resource = new \stdClass();

self::$resource->{$key} = $value;
}

#[ObjectShape([
Expand All @@ -91,7 +98,7 @@ public static function set_res(string $key, mixed $value) : void
'img' => 'string',
'js' => 'string',
'shared' => 'object [root, static, css, img, js, img_default [object [logo, favicon, icon, meta]]]',
'domain' => 'object',
'domain' => 'object [domain_uri,route, route_as_array, domain_type, domain_id, domain_root, pattern, 0, 1 ...n]',
'lay' => 'object [uri, root]',
])]
public static function get() : object
Expand Down
26 changes: 26 additions & 0 deletions src/Core/View/SrcFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace BrickLayer\Lay\Core\View;

class SrcFilter
{
public static function go(string $src) : string {
$client = DomainResource::get();

return str_replace(
[
"@shared/", "@#/", "@static/",
"@shared_js/", "@js/", "@static_env/",
"@shared_img/", "@img/", "@ui/",
"@shared_css/", "@css/",
],
[
$client->shared->root, $client->root, $client->static,
$client->shared->js, $client->js, $client->static_env,
$client->shared->img, $client->img, $client->static_env . "ui/",
$client->shared->css, $client->css,
],
$src
);
}
}
2 changes: 0 additions & 2 deletions src/Core/View/ViewCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ final public function init(): void
if(!isset($this->builder))
$this->builder = ViewBuilder::new();

DomainResource::new()::init();

$this->init_pages();
$this->default();
$this->pages();
Expand Down
6 changes: 2 additions & 4 deletions src/Core/View/ViewEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private function create_html_page() : void {
{$this->skeleton_body()}
</body></html>
STR;

if($layConfig::$ENV_IS_PROD && $layConfig::is_page_compressed())
$page = preg_replace("/>(\s)+</m","><",preg_replace("/<!--(.|\s)*?-->/","",$page));

Expand All @@ -210,6 +210,7 @@ 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);

$this->dump_assets("js");
Expand Down Expand Up @@ -322,9 +323,6 @@ private function dump_assets(string $asset_type) : void
$view .= $resolve_asset($asset, $k);
}

if($asset_type == "js")
$view = $this->core_script() . $view;

echo $view;
}

Expand Down
18 changes: 2 additions & 16 deletions src/Core/View/ViewSrc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,9 @@
final class ViewSrc {
public static function gen(string $src) : string
{
$client = DomainResource::get();
$src = SrcFilter::go($src);

$src = str_replace(
[
"@shared/", "@#/", "@static/",
"@shared_js/", "@js/",
"@shared_img/", "@img/",
"@shared_css/", "@css/",
],
[
$client->shared->root, $client->root, $client->static,
$client->shared->js, $client->js,
$client->shared->img, $client->img,
$client->shared->css, $client->css,
],
$src
);
$client = DomainResource::get();

$base = $client->domain->domain_base;

Expand Down

0 comments on commit 85fc4c7

Please sign in to comment.