From b08f2c604a5436e1061faeb88ee35560ce452c92 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2026 08:56:12 +0000 Subject: [PATCH] Refactor MakePageBlockCommand handle method Co-authored-by: juzaweb <47020363+juzaweb@users.noreply.github.com> --- src/Commands/Themes/MakePageBlockCommand.php | 46 +++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Commands/Themes/MakePageBlockCommand.php b/src/Commands/Themes/MakePageBlockCommand.php index 0651deb..1f9baeb 100644 --- a/src/Commands/Themes/MakePageBlockCommand.php +++ b/src/Commands/Themes/MakePageBlockCommand.php @@ -31,6 +31,18 @@ public function handle(): int Stub::setBasePath(config('dev-tool.themes.stubs.path') . '/'); + if ($this->makeViews($theme, $name) === self::FAILURE) { + return self::FAILURE; + } + + $this->modifyProvider($theme, $name, $themeName); + + $this->info("Block {$name} created successfully."); + return self::SUCCESS; + } + + protected function makeViews($theme, string $name): int + { $formPath = $theme->path("src/resources/views/components/blocks/{$name}/form.blade.php"); $viewPath = $theme->path("src/resources/views/components/blocks/{$name}/view.blade.php"); @@ -59,6 +71,11 @@ public function handle(): int $this->info("Generated {$viewPath}"); + return self::SUCCESS; + } + + protected function modifyProvider($theme, string $name, string $themeName): void + { $providerFile = $theme->path('src/Providers/StyleServiceProvider.php'); if (!file_exists($providerFile)) { $content = $this->generateContents( @@ -72,6 +89,14 @@ public function handle(): int $content = file_get_contents($providerFile); } + $newContent = $this->appendBlockToProvider($content, $name, $themeName); + $newContent = $this->addUseStatementToProvider($newContent); + + file_put_contents($providerFile, $newContent); + } + + protected function appendBlockToProvider(string $content, string $name, string $themeName): string + { $pattern = '/(public function boot\s*\(\)(?:\s*:\s*void)?\s*\{)([\s\S]*?)(^\s*\})/m'; $replacement = '$1$2' . " PageBlock::make( '{$name}', @@ -84,12 +109,16 @@ function () { } );\n" . '$3'; - $newContent = preg_replace($pattern, $replacement, $content); + return preg_replace($pattern, $replacement, $content); + } + + protected function addUseStatementToProvider(string $content): string + { $useStatement = "use Juzaweb\\Modules\\Admin\\Facades\\PageBlock;"; - if (!str_contains($newContent, $useStatement)) { + if (!str_contains($content, $useStatement)) { // Tìm vị trí cuối cùng của nhóm use hiện tại - if (preg_match_all('/^use\s+[^;]+;/m', $newContent, $allMatches, PREG_OFFSET_CAPTURE)) { + if (preg_match_all('/^use\s+[^;]+;/m', $content, $allMatches, PREG_OFFSET_CAPTURE)) { // $allMatches[0] is an array of matches; pick the last one $lastMatch = end($allMatches[0]); // $lastMatch is [matchedString, offset] @@ -97,21 +126,18 @@ function () { $matchedOffset = $lastMatch[1]; $insertPos = $matchedOffset + strlen($matchedString); - $newContent = substr_replace($newContent, "\n{$useStatement}", $insertPos, 0); + $content = substr_replace($content, "\n{$useStatement}", $insertPos, 0); } else { // If there is no use block, add after namespace - $newContent = preg_replace( + $content = preg_replace( '/(namespace\s+[^\n;]+;)/', "$1\n\n{$useStatement}", - $newContent + $content ); } } - file_put_contents($providerFile, $newContent); - - $this->info("Block {$name} created successfully."); - return self::SUCCESS; + return $content; } protected function generateContents(string $stub, array $data): string