Skip to content

Commit

Permalink
Merge pull request #3 from PHPBrickLayer/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
leonardosahon authored Dec 23, 2023
2 parents 49720dc + 9492ea9 commit 5c1f785
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 67 deletions.
16 changes: 16 additions & 0 deletions src/BobDBuilder/BobExec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace BrickLayer\Lay\BobDBuilder;

class BobExec
{
public function __construct(string $command)
{
$command = "php bob $command";

new Engine(
explode(" ", $command),
true
);
}
}
6 changes: 3 additions & 3 deletions src/BobDBuilder/Cmd/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Make implements CmdLayout
use Domain;
use Brick;

private readonly EnginePlug $plug;
private readonly array $tags;
private readonly string $internal_dir;
private EnginePlug $plug;
private array $tags;
private string $internal_dir;

public function _init(EnginePlug $plug): void
{
Expand Down
23 changes: 20 additions & 3 deletions src/BobDBuilder/Cmd/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BrickLayer\Lay\BobDBuilder\Cmd;

use BrickLayer\Lay\BobDBuilder\BobExec;
use BrickLayer\Lay\BobDBuilder\EnginePlug;
use BrickLayer\Lay\BobDBuilder\Interface\CmdLayout;
use BrickLayer\Lay\Core\Traits\IsSingleton;
Expand All @@ -11,13 +12,13 @@ class Project implements CmdLayout
{
use IsSingleton;

private readonly EnginePlug $plug;
private readonly array $tags;
private EnginePlug $plug;
private array $tags;

public function _init(EnginePlug $plug): void
{
$this->plug = $plug;
$plug->add_arg($this, ["project:create"], 'project_create', true);
$plug->add_arg($this, ["project:create"], 'project_create', true, 0);
}

public function _spin(): void
Expand All @@ -33,10 +34,13 @@ public function _spin(): void
public function create(): void
{
$cmd = $this->tags['project_create'][0] ?? null;
$tag = $this->tags['project_create'][1] ?? "";

if (!$cmd)
return;

$tag = trim($tag);

$server = $this->plug->server;

// copy env file if it doesn't exist
Expand All @@ -48,5 +52,18 @@ public function create(): void

// copy helper js file to project lay folder
new LayCopyDir($server->lay_static . "js", $server->shared . "lay");

if($tag == "--fresh-project") {
$this->plug->write_info("Fresh project detected!");

// create a default domain folder
new BobExec("make:domain Default * --force --silent");

// link lay folder to default folder
$s = DIRECTORY_SEPARATOR;
new BobExec("link:dir web{$s}shared{$s}lay web{$s}domains{$s}Default{$s}lay --silent");

$this->plug->write_info("Default domain folder created successfully!");
}
}
}
2 changes: 1 addition & 1 deletion src/BobDBuilder/Cmd/Symlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Symlink implements CmdLayout
{
use IsSingleton;

private readonly EnginePlug $plug;
private EnginePlug $plug;

public function _init(EnginePlug $plug): void
{
Expand Down
88 changes: 69 additions & 19 deletions src/BobDBuilder/Cmd/Traits/Make/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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

use BrickLayer\Lay\Core\Exception;
use BrickLayer\Lay\Libs\LayCopyDir;
use BrickLayer\Lay\Libs\LayUnlinkDir;
use BrickLayer\Lay\Orm\SQL;
use SplFileObject;


Expand All @@ -29,7 +31,7 @@ public function domain(): void
. "Example: 'case-study'\n"
);

if (empty($pattern) || trim($pattern) == "*")
if (empty($pattern) || (!$this->plug->is_internal && trim($pattern) == "*"))
$this->plug->write_warn(
"Pattern cannot be an empty quote or '*'\n"
. "\n"
Expand All @@ -40,12 +42,12 @@ public function domain(): void


$domain = explode(" ", ucwords($domain));
$domain_id = implode("-", $domain) . "-id";
$domain_id = strtolower(implode("-", $domain) . "-id");
$domain = implode("", $domain);
$domain_dir = $this->plug->server->domains . $domain;
$exists = is_dir($domain_dir);

if ($this->plug->force && $exists)
if (!$this->plug->force && $exists)
$this->plug->write_fail(
"Domain directory *$domain_dir* exists already!\n"
. "If you wish to force this action, pass the tag --force with the command\n"
Expand Down Expand Up @@ -80,8 +82,7 @@ public function domain(): void
public function domain_default_files(string $domain_name, string $domain_dir): void
{
file_put_contents(
$domain_dir . $this->plug->s .
"index.php",
$domain_dir . $this->plug->s . "index.php",
<<<FILE
<?php
use BrickLayer\Lay\Core\View\Domain;
Expand All @@ -92,7 +93,7 @@ public function domain_default_files(string $domain_name, string $domain_dir): v
Domain::new()->create(
id: "default",
builder: new \web\domains\{$domain_name}\Plaster(),
builder: new \web\domains\\$domain_name\Plaster(),
);
FILE
Expand All @@ -103,11 +104,11 @@ public function domain_default_files(string $domain_name, string $domain_dir): v
"Plaster.php",
<<<FILE
<?php
namespace web\domains\{$domain_name};
namespace web\domains\\$domain_name;
use BrickLayer\Lay\core\view\DomainResource;
use BrickLayer\Lay\core\view\ViewBuilder;
use BrickLayer\Lay\core\view\ViewCast;
use BrickLayer\Lay\Core\View\DomainResource;
use BrickLayer\Lay\Core\View\ViewBuilder;
use BrickLayer\Lay\Core\View\ViewCast;
class Plaster extends ViewCast
{
Expand Down Expand Up @@ -155,26 +156,56 @@ public function update_general_domain_entry(string $domain, string $domain_id, s

$pattern = rtrim($pattern, ",");

$file = new SplFileObject($this->plug->server->web . "index.php", 'w+');
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
$main_file = $this->plug->server->web . "index.php";
$lock_file = $this->plug->server->web . ".index.php.lock";

copy($main_file, $lock_file);

$file = new SplFileObject($lock_file, 'r+');
$file->setFlags(SplFileObject::DROP_NEW_LINE);

$page = [];
$domains = [];
$key = 0;
$storing_domain = false;
$existing_domain_key = null;
$page_index = 0;

while (!$file->eof()) {
$entry = $file->fgets();

if($page_index > 7 && empty($entry))
continue;

if (str_starts_with($entry, "Domain::new()"))
$storing_domain = true;

if ($storing_domain) {
if (empty($entry))
continue;

$domains[$key][] = $entry;

if(
$existing_domain_key === null &&
$this->plug->force
) {
if(
str_starts_with(ltrim($entry), "id:")
)
$existing_domain_key = trim(
rtrim(
explode("id:", $entry)[1],
","
), "'\""
) == $domain_id ? $key : null;

if(
!$existing_domain_key &&
str_starts_with(ltrim($entry), "builder:")
)
$existing_domain_key = @explode("\\", $entry)[3] == $domain ? $key : null;
}


if (str_ends_with($entry, ";")) {
$storing_domain = false;
$key++;
Expand All @@ -184,22 +215,41 @@ public function update_general_domain_entry(string $domain, string $domain_id, s
}

$page[] = $entry;
$page_index++;
}

$default_domain = end($domains);
$default_domain = [];

if(!$this->plug->is_internal) {
$default_domain = end($domains);
array_pop($domains);
}

if($existing_domain_key)
unset($domains[$existing_domain_key]);

$domains = SQL::new()->array_flatten($domains);

$new_domain = [
'Domain::new()->create(',
' id: "' . $domain_id . '",',
' builder: new \web\domains\\' . $domain . '\\Plaster(),',
' patterns: [' . $pattern . '],',
');',
'',
];

array_pop($domains);
array_push($page, $domains, $new_domain, $default_domain);
try{
array_push($page, "", ...$domains, ...[""], ...$new_domain, ...[""], ...$default_domain, ...[""]);
$file->rewind();
$file->fwrite(implode("\n", $page));
} catch (\Exception $e) {
Exception::throw_exception($e->getMessage(), "MakeDomain");

new LayUnlinkDir($domain_dir);
unlink($lock_file);
}

$file->fwrite(implode("\n", $page));
copy($lock_file, $main_file);
unlink($lock_file);
}
}
66 changes: 39 additions & 27 deletions src/BobDBuilder/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,20 @@ class Engine
public EnginePlug $plug;

public function __construct(
private array $args
private array $args,
private readonly bool $is_internal = false
)
{
$show_help = array_search("--help", $this->args, true);
$show_help = $show_help === false ? array_search("--h", $this->args, true) : $show_help;

if ($show_help !== false) {
unset($this->args[$show_help]);
$show_help = true;
}

$force_action = array_search("--force", $this->args, true);
$force_action = $force_action === false ? array_search("--f", $this->args, true) : $force_action;

if ($force_action !== false) {
unset($this->args[$force_action]);
$force_action = true;
}
$force = $this->extract_global_tag("--force", "-f");
$show_help = $this->extract_global_tag("--help", "-h");
$silent = $this->extract_global_tag("--silent", "-s");

$this->plug = new EnginePlug($this->args);
$this->plug->is_internal = $this->is_internal;

$this->plug->force = $force_action ?? false;
$this->plug->show_help = $show_help ?? false;
$this->plug->force = $force;
$this->plug->show_help = $show_help;
$this->plug->silent = $silent;

foreach ($this->args as $i => $arg) {
if($this->plug->run($i, $arg) == CustomContinueBreak::BREAK)
Expand Down Expand Up @@ -61,20 +52,41 @@ public function __construct(

// End Bob execution
if(empty($this->plug->active_cmd))
$this->plug->write_info(
$this->plug->write_warn(
"-- Bob has determined that the current command is invalid\n"
. "-- Please use --help to see the list of available commands"
, ["current_cmd" => $this->plug->typed_cmd]
);

$this->plug->write_success(
"\n" . (
isset($this->plug->active_cmd_class) ?
"-- Operation completed!" :
""
),
['close_talk' => true]
);
if(!$this->plug->silent)
$this->plug->write_success(
"\n" . (
isset($this->plug->active_cmd_class) ?
"-- Operation completed!" :
""
),
['close_talk' => true]
);
}

public function extract_global_tag(string ...$tags) : bool
{
$out = false;

foreach ($tags as $tag) {
if($out !== false)
break;

$out = array_search($tag, $this->args, true);

}

if ($out !== false) {
unset($this->args[$out]);
$out = true;
}

return $out;
}

public function intro(bool $close_talk = true): void
Expand Down
Loading

0 comments on commit 5c1f785

Please sign in to comment.