Skip to content

Commit

Permalink
Publish component to configured Livewire namespace (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored May 24, 2024
1 parent 187b67d commit 02692a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/Commands/MakeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Livewire\Features\SupportConsoleCommands\Commands\ComponentParser;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Form;

Expand Down Expand Up @@ -36,18 +37,22 @@ public function handle(): void
options: $forms->mapWithKeys(fn ($form) => [$form->handle() => $form->title()]),
);

$classNamespace = config('livewire.class_namespace');
$className = Str::of($name)->endsWith('Form') ? $name : Str::of($name)->append('Form')->studly();
$classPath = ComponentParser::generatePathFromNamespace($classNamespace).collect()->push("{$className}.php")->implode('/');

$stub = File::get(__DIR__.'/form.stub');

$stub = str_replace('[className]', $className, $stub);

$path = app_path("Livewire/{$className}.php");
$stub = preg_replace(
['/\[namespace\]/', '/\[class\]/'],
[$classNamespace, $className],
$stub
);

if (! File::exists($path) || confirm(label: 'A component with this name already exists. Do you want to overwrite it?', default: false)) {
File::ensureDirectoryExists(app_path('Livewire'));
File::put($path, $stub);
info("The component was successfully created: <comment>{$this->getRelativePath($path)}</comment>");
if (! File::exists($classPath) || confirm(label: 'A component with this name already exists. Do you want to overwrite it?', default: false)) {
File::ensureDirectoryExists(dirname($classPath));
File::put($classPath, $stub);
info("The component was successfully created: <comment>{$this->getRelativePath($classPath)}</comment>");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/form.stub
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace App\Livewire;
namespace [namespace];

use Aerni\LivewireForms\Form\Fields;
use Aerni\LivewireForms\Livewire\BaseForm;
use Illuminate\Support\Collection;
use Statamic\Contracts\Forms\Submission;

class [className] extends BaseForm
class [class] extends BaseForm
{
/*
|--------------------------------------------------------------------------
Expand Down

0 comments on commit 02692a4

Please sign in to comment.