Skip to content

Commit d88adb4

Browse files
author
Thibo De Langhe
committed
Merge branch 'feature/console-command' into feature/laravel-8
2 parents 00d3e02 + 7568748 commit d88adb4

File tree

5 files changed

+197
-0
lines changed

5 files changed

+197
-0
lines changed

src/Console/NewFormCommand.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Codedor\LivewireForms\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class NewFormCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'form:new {name}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new laravel livewire form';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Form';
29+
30+
/**
31+
* Execute the console command.
32+
*
33+
* @return bool|null
34+
*/
35+
36+
// take parent handle to overwrite info messages
37+
public function handle()
38+
{
39+
$name = $this->qualifyClass($this->getNameInput());
40+
41+
$path = $this->getPath($name);
42+
43+
if ((! $this->hasOption('force') ||
44+
! $this->option('force')) &&
45+
$this->alreadyExists($this->getNameInput())) {
46+
$this->error($this->type.' already exists!');
47+
48+
return false;
49+
}
50+
51+
$this->makeDirectory($path);
52+
53+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
54+
55+
$formName = $this->argument('name');
56+
57+
$this->info("Form was created '{$this->getDefaultNamespace($this->rootNamespace())}\\{$formName}'🥳");
58+
59+
$this->call('form:controller', ['name' => $formName]);
60+
}
61+
62+
/**
63+
* Get the stub file for the generator.
64+
*
65+
* @return string
66+
*/
67+
protected function getStub()
68+
{
69+
return __DIR__.'/stubs/form.stub';
70+
}
71+
72+
/**
73+
* Get the default namespace for the class.
74+
*
75+
* @param string $rootNamespace
76+
* @return string
77+
*/
78+
protected function getDefaultNamespace($rootNamespace)
79+
{
80+
return $rootNamespace.'\Forms';
81+
}
82+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Codedor\LivewireForms\Console;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
7+
class NewFormControllerCommand extends GeneratorCommand
8+
{
9+
/**
10+
* The console command name.
11+
*
12+
* @var string
13+
*/
14+
protected $name = 'form:controller {name}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new livewire form controller';
22+
23+
/**
24+
* The type of class being generated.
25+
*
26+
* @var string
27+
*/
28+
protected $type = 'Controller';
29+
30+
// take parent handle to overwrite info messages
31+
public function handle()
32+
{
33+
$name = $this->qualifyClass($this->getNameInput());
34+
35+
$path = $this->getPath($name);
36+
37+
// First we will check to see if the class already exists. If it does, we don't want
38+
// to create the class and overwrite the user's code. So, we will bail out so the
39+
// code is untouched. Otherwise, we will continue generating this class' files.
40+
if ((! $this->hasOption('force') ||
41+
! $this->option('force')) &&
42+
$this->alreadyExists($this->getNameInput())) {
43+
$this->error($this->type.' already exists!');
44+
45+
return false;
46+
}
47+
48+
// Next, we will generate the path to the location where this class' file should get
49+
// written. Then, we will build the class and make the proper replacements on the
50+
// stub files so that it gets the correctly formatted namespace and class name.
51+
$this->makeDirectory($path);
52+
53+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
54+
55+
$controllerName = $this->argument('name');
56+
57+
$this->info("Controller was created '{$this->getDefaultNamespace($this->rootNamespace())}\\{$controllerName}'🥳");
58+
}
59+
60+
/**
61+
* Get the stub file for the generator.
62+
*
63+
* @return string
64+
*/
65+
protected function getStub()
66+
{
67+
return __DIR__.'/stubs/formcontroller.stub';
68+
}
69+
70+
/**
71+
* Get the default namespace for the class.
72+
*
73+
* @param string $rootNamespace
74+
* @return string
75+
*/
76+
protected function getDefaultNamespace($rootNamespace)
77+
{
78+
return $rootNamespace.'\Http\Livewire';
79+
}
80+
}

src/Console/stubs/form.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Codedor\LivewireForms\Form;
6+
7+
class DummyClass extends Form
8+
{
9+
public function fields(): array
10+
{
11+
return [
12+
//
13+
];
14+
}
15+
}

src/Console/stubs/formcontroller.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Codedor\LivewireForms\FormController;
6+
7+
class DummyClass extends FormController
8+
{
9+
public $formClass = \App\Forms\DummyClass::class;
10+
11+
// public $modelClass = Inquiry::class;
12+
}

src/LivewireFormsServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ public function publishData()
2424
'livewire-forms'
2525
);
2626
}
27+
28+
public function register()
29+
{
30+
$this->commands([
31+
Console\NewFormCommand::class,
32+
Console\NewFormControllerCommand::class
33+
]);
34+
}
2735
}

0 commit comments

Comments
 (0)