From f25f8f05ec41dd7bae97b713859c484b6aff89b9 Mon Sep 17 00:00:00 2001 From: Osahenrumwen Aigbogun Date: Tue, 19 Dec 2023 23:17:01 +0100 Subject: [PATCH] Rewrote Cmd/Trait/Make to the state it was before it was overwritten --- src/BobDBuilder/Cmd/Traits/Make/Domain.php | 77 +++++++++++++++++----- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/BobDBuilder/Cmd/Traits/Make/Domain.php b/src/BobDBuilder/Cmd/Traits/Make/Domain.php index 532fbe0..99bc82f 100644 --- a/src/BobDBuilder/Cmd/Traits/Make/Domain.php +++ b/src/BobDBuilder/Cmd/Traits/Make/Domain.php @@ -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; @@ -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" @@ -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", <<create( id: "default", - builder: new \web\domains\{$domain_name}\Plaster(), + builder: new \web\domains\\$domain_name\Plaster(), ); FILE @@ -103,11 +104,11 @@ public function domain_default_files(string $domain_name, string $domain_dir): v "Plaster.php", <<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++; @@ -184,9 +215,16 @@ 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(', @@ -194,12 +232,21 @@ public function update_general_domain_entry(string $domain, string $domain_id, s ' 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); } } \ No newline at end of file