Skip to content

Commit

Permalink
Merge pull request #9
Browse files Browse the repository at this point in the history
Turned Cmd classes from singleton to normal classes
  • Loading branch information
leonardosahon authored Dec 25, 2023
2 parents 84fc26d + a6fb585 commit 5da2bca
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 42 deletions.
3 changes: 0 additions & 3 deletions src/BobDBuilder/Cmd/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
use BrickLayer\Lay\BobDBuilder\Cmd\Traits\Make\Domain;
use BrickLayer\Lay\BobDBuilder\EnginePlug;
use BrickLayer\Lay\BobDBuilder\Interface\CmdLayout;
use BrickLayer\Lay\Core\Traits\IsSingleton;

class Make implements CmdLayout
{
use IsSingleton;

use Domain;
use Brick;

Expand Down
18 changes: 11 additions & 7 deletions src/BobDBuilder/Cmd/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
use BrickLayer\Lay\BobDBuilder\BobExec;
use BrickLayer\Lay\BobDBuilder\EnginePlug;
use BrickLayer\Lay\BobDBuilder\Interface\CmdLayout;
use BrickLayer\Lay\Core\Traits\IsSingleton;
use BrickLayer\Lay\Libs\LayCopyDir;

class Project implements CmdLayout
{
use IsSingleton;

private EnginePlug $plug;
private array $tags;

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

public function _spin(): void
Expand All @@ -33,10 +30,9 @@ public function _spin(): void

public function create(): void
{
$cmd = $this->tags['project_create'][0] ?? null;
$tag = $this->tags['project_create'][1] ?? "";
$tag = $this->tags['project_create'][0] ?? null;

if (!$cmd)
if (!$tag)
return;

$tag = trim($tag);
Expand All @@ -53,11 +49,19 @@ public function create(): void
// copy helper js file to project lay folder
new LayCopyDir($server->lay_static . "js", $server->shared . "lay");

if($tag == "--force-refresh") {
$this->plug->write_info("Default domain forcefully refreshed");

new BobExec("make:domain Default * --silent --force");
return;
}

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

// Replace default domain folder on a fresh project
new BobExec("make:domain Default * --silent --force");
return;
}

// create a default domain folder if not exists
Expand Down
3 changes: 0 additions & 3 deletions src/BobDBuilder/Cmd/Symlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

class Symlink implements CmdLayout
{
use IsSingleton;

private EnginePlug $plug;

public function _init(EnginePlug $plug): void
Expand All @@ -31,7 +29,6 @@ public function _spin(): void
$this->file();
}


use Htaccess;
use Dir;
use File;
Expand Down
31 changes: 25 additions & 6 deletions src/BobDBuilder/Cmd/Traits/Make/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public function domain(): void
$domain_dir = $this->plug->server->domains . $domain;
$exists = is_dir($domain_dir);

if($domain == "Default" && !$this->plug->is_internal)
$this->plug->write_warn(
"Unfortunately you cannot create a *Default* domain automatically\n"
. "If for whatever reasons you need to do that, navigate to the Lay Repository and copy it from there\n"
. "Github: *https://github.com/PHPBrickLayer/lay*"
);

if($domain == "Default" && $this->plug->is_internal) {
$domain_id = "default";
$pattern = "*";
}

if (!$this->plug->force && $exists)
$this->plug->write_fail(
"Domain directory *$domain_dir* exists already!\n"
Expand All @@ -71,7 +83,7 @@ public function domain(): void
$this->domain_default_files($domain, $domain_id, $domain_dir);

$talk("- Linking .htaccess *{$this->plug->server->web}*");
new BobExec("link:h taccess $domain --silent");
new BobExec("link:htaccess $domain --silent");

$talk("- Linking shared directory *{$this->plug->server->shared}*");
new BobExec("link:dir web{$this->plug->s}shared web{$this->plug->s}domains{$this->plug->s}$domain{$this->plug->s}shared --silent");
Expand All @@ -82,6 +94,7 @@ public function domain(): void

public function domain_default_files(string $domain_name, string $domain_id, string $domain_dir): void
{
// root index.php
file_put_contents(
$domain_dir . $this->plug->s . "index.php",
<<<FILE
Expand All @@ -100,6 +113,7 @@ public function domain_default_files(string $domain_name, string $domain_id, str
FILE
);

// Plaster.php for handling routes
file_put_contents(
$domain_dir . $this->plug->s .
"Plaster.php",
Expand Down Expand Up @@ -146,6 +160,12 @@ public function pages(): void
FILE
);

// favicon.ico
copy(
$this->plug->server->lay_static . "img" . $this->plug->s . "favicon.ico",
$domain_dir . $this->plug->s . "favicon.ico"
);
}

public function update_general_domain_entry(string $domain, string $domain_id, string $patterns): void
Expand Down Expand Up @@ -221,14 +241,13 @@ public function update_general_domain_entry(string $domain, string $domain_id, s

$default_domain = [''];

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

if($domains > 1)
array_pop($domains);

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

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

Expand Down
45 changes: 22 additions & 23 deletions src/BobDBuilder/EnginePlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ public function fire(): void
}
}

public function run(int $index, string $arg): CustomContinueBreak
{
$this->current_arg = $arg;
$this->current_index = $index;

$this->show_intro = $index < 1;

if ($index == 1)
$this->typed_cmd = $arg;

foreach ($this->plugged_args as $key => $arg) {
if ($this->arg($arg['class'], [...$arg['cmd']], $this->tags[$key], ...$arg['value']))
return CustomContinueBreak::BREAK;
}

return CustomContinueBreak::CONTINUE;
}

private function load_cmd_classes() : void
{
$namespace = explode("\\", self::class);
Expand All @@ -125,16 +107,15 @@ private function load_cmd_classes() : void
}

try {
$class = $class->getMethod('new');
$class = $class->newInstance();
} catch (ReflectionException) {
Exception::throw_exception(
" $cmd_class is not a singleton. \n"
. " All Cmd classes must be singletons, use the trait `IsSingleton` to clear this error",
"IsNotSingleton"
" $cmd_class constructor class is private. \n"
. " All Cmd classes must expose their __construct function to clear this error",
"ConstructPrivate"
);
}

$class = $class->invoke(null);
$this->cmd_classes[] = $class;

try {
Expand All @@ -145,6 +126,24 @@ private function load_cmd_classes() : void
}
}

public function run(int $index, string $arg): CustomContinueBreak
{
$this->current_arg = $arg;
$this->current_index = $index;

$this->show_intro = $index < 1;

if ($index == 1)
$this->typed_cmd = $arg;

foreach ($this->plugged_args as $key => $arg) {
if ($this->arg($arg['class'], [...$arg['cmd']], $this->tags[$key], ...$arg['value']))
return CustomContinueBreak::BREAK;
}

return CustomContinueBreak::CONTINUE;
}

/**
* Use this function to add the arguments required by each Cmd Class
* @param CmdLayout $cmd_layout
Expand Down
Binary file added src/static/img/favicon.ico
Binary file not shown.

0 comments on commit 5da2bca

Please sign in to comment.