Skip to content

Commit

Permalink
Rewrote Cmd/Trait/Make to the state it was before it was overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosahon committed Dec 19, 2023
1 parent c284ccb commit f25f8f0
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 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 Down Expand Up @@ -45,7 +47,7 @@ public function domain(): void
$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+');
$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 | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);

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

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

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

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

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

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

if(
$this->plug->force &&
$existing_domain_key !== null
) {
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,38 @@ public function update_general_domain_entry(string $domain, string $domain_id, s
}

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

$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->seek(0);
$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);
}
}

0 comments on commit f25f8f0

Please sign in to comment.