Skip to content

Commit

Permalink
Merge pull request #12 from PHPBrickLayer/dev
Browse files Browse the repository at this point in the history
Added support for make:brick
  • Loading branch information
leonardosahon authored Dec 28, 2023
2 parents 85fc4c7 + 9dabeaf commit d0e9a4b
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 17 deletions.
15 changes: 0 additions & 15 deletions __internal/Brick/Api/Hook.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/BobDBuilder/Cmd/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public function _init(EnginePlug $plug): void
$this->internal_dir = $this->plug->server->lay . "__internal" . $this->plug->s;

$plug->add_arg($this, ["make:domain"], 'make_domain', 0, 1);
$plug->add_arg($this, ["make:brick"], 'make_brick', 0, 1);
}

public function _spin(): void
{
$this->tags = $this->plug->tags;

$this->brick();
$this->domain();
}

Expand Down
173 changes: 172 additions & 1 deletion src/BobDBuilder/Cmd/Traits/Make/Brick.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,182 @@

namespace BrickLayer\Lay\BobDBuilder\Cmd\Traits\Make;

use BrickLayer\Lay\Libs\LayCopyDir;
use BrickLayer\Lay\Libs\LayUnlinkDir;
use BrickLayer\Lay\Libs\String\Pluralize;

trait Brick
{
// TODO: Implement feature, devs should be able to create a brick with the whole file structure set
public function brick() : void
{
if(!isset($this->tags['make_brick']))
return;

$brick = $this->tags['make_brick'][0] ?? null;
$singleton = $this->tags['make_brick'][1] ?? true;

$talk = fn($msg) => $this->plug->write_talk($msg);

if (!$brick)
$this->plug->write_fail("No brick specified");

if(is_string($singleton) && !str_starts_with($singleton, "-"))
$this->plug->write_warn("*$singleton* is not a valid tag.\n"
. "This command only accepts *BrickName* and *--n-singleton* as it's arguments");

if($singleton === "--n-singleton")
$singleton = false;

$talk("- If your brick is written in plural form, it'll be converted to singular form");

$brick = Pluralize::to_singular(ucwords($brick));
$brick_dir = $this->plug->server->bricks . $brick;
$exists = is_dir($brick_dir);

if (!$this->plug->force && $exists)
$this->plug->write_fail(
"Brick directory *$brick_dir* exists already!\n"
. "If you wish to force this action, pass the tag *--force* with the command\n"
. "Note, using *--force* will delete the existing directory and this process cannot be reversed!"
);

if ($exists) {
$talk(
"- Directory *$brick_dir* exists but *--force* tag detected\n"
. "- Deleting existing *$brick_dir*"
);

new LayUnlinkDir($brick_dir);
}

$talk("- Creating new Brick directory in *$brick_dir*");
new LayCopyDir($this->internal_dir . "Brick", $brick_dir);

$talk("- Creating default brick files");

$this->brick_default_files($brick, $brick_dir, $singleton);
}

public function brick_default_files(string $brick, string $brick_dir, bool $singleton): void
{
$brick_plural = Pluralize::to_plural($brick);
// convert uppercase to _ and lowercase for the tables
$brick_words = preg_split('/\B(?=[A-Z])/s', $brick_plural);
$brick_table = strtolower(implode("_", $brick_words));

$import = "";
$body = "";
$brick_init = "new $brick()";

if($singleton) {
$import = "use BrickLayer\Lay\Core\Traits\IsSingleton;";
$body = "use IsSingleton;";
$brick_init = "$brick::new()";
}

// default api hook
file_put_contents(
$brick_dir . $this->plug->s . "Api" . $this->plug->s . "Hook.php",
<<<FILE
<?php
declare(strict_types=1);
namespace bricks\\$brick\Api;
use BrickLayer\Lay\Core\Api\ApiEngine;
use BrickLayer\Lay\Core\Api\ApiHooks;
// This is the alpha Api Hook class.
// If you wish to create more individual hook classes,
// know that those will not be executed automatically
class Hook extends ApiHooks
{
// you can create public methods which can be used in other parts of your projects
// In as much as this is an ApiHooks class, it is also a normal class,
// So every normal class rule applies to it.
public function hooks(): void
{
// All hooks placed here are added to the general hooks of the project
// You don't have to do anything extra.
}
}
FILE
);

// default model file
file_put_contents(
$brick_dir . $this->plug->s . "model" . $this->plug->s . "$brick.php",
<<<FILE
<?php
declare(strict_types=1);
namespace bricks\\$brick\model;
use JetBrains\PhpStorm\ArrayShape;
use BrickLayer\Lay\Orm\SQL;
$import
class $brick
{
$body
public static string \$table = "$brick_table";
public static function orm(?string \$table = null) : SQL
{
if(\$table)
return SQL::instance()->open(\$table);
return SQL::instance();
}
#[ArrayShape(["code" => "int", "msg" => "string", "data" => "bool"])]
public function add(array \$columns) : array
{
\$columns['id'] = \$columns['id'] ?? 'UUID()';
return [
"code" => 200,
"msg" => "Ok",
"data" => self::orm(self::\$table)->insert(\$columns)
];
}
}
FILE
);

// default controller file
file_put_contents(
$brick_dir . $this->plug->s . "controller" . $this->plug->s . "$brick_plural.php",
<<<FILE
<?php
declare(strict_types=1);
namespace bricks\\$brick\model;
use JetBrains\PhpStorm\ArrayShape;
use BrickLayer\Lay\Orm\SQL;
$import
use bricks\\$brick\model\\$brick;
class $brick_plural
{
$body
private static function model(): $brick
{
return $brick_init;
}
}
FILE
);


}
}
3 changes: 3 additions & 0 deletions src/BobDBuilder/Cmd/Traits/Make/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ trait Domain
{
public function domain(): void
{
if(!isset($this->tags['make_domain']))
return;

$domain = $this->tags['make_domain'][0] ?? null;
$pattern = $this->tags['make_domain'][1] ?? null;

Expand Down
3 changes: 2 additions & 1 deletion src/Core/Api/ApiHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

abstract class ApiHooks
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public function hooks() : void
$this->request->print_as_json();
}

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

Expand Down
Loading

0 comments on commit d0e9a4b

Please sign in to comment.