Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/Commands/Themes/MakePageBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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(
Expand All @@ -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}',
Expand All @@ -84,34 +109,35 @@ 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]
$matchedString = $lastMatch[0];
$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
Expand Down
Loading