Skip to content

Commit 72ca126

Browse files
committed
✨ Add column command
1 parent a83a6da commit 72ca126

12 files changed

Lines changed: 256 additions & 137 deletions

src/Commands/Aliases/MakeBaseFieldCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Symfony\Component\Console\Attribute\AsCommand;
99

1010
#[AsCommand(name: 'sikessem:base-field')]
11-
class MakeBaseFieldCommand extends Commands\MakeBaseFieldCommand
11+
class MakeBaseFieldCommand extends Commands\MakeFieldCommand
1212
{
1313
protected $hidden = true;
1414

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sikessem\Components\Commands\Aliases;
6+
7+
use Sikessem\Components\Commands;
8+
use Symfony\Component\Console\Attribute\AsCommand;
9+
10+
#[AsCommand(name: 'sikessem:field')]
11+
class MakeColumnCommand extends Commands\MakeColumnCommand
12+
{
13+
protected $hidden = true;
14+
15+
protected $signature = 'sikessem:column {name} {component} {column} {label?} {--f|force}';
16+
}

src/Commands/Aliases/MakeFieldCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Symfony\Component\Console\Attribute\AsCommand;
99

1010
#[AsCommand(name: 'sikessem:field')]
11-
class MakeFieldCommand extends Commands\MakeFieldCommand
11+
class MakeFieldCommand extends Commands\MakeInputCommand
1212
{
1313
protected $hidden = true;
1414

src/Commands/MakeBaseFieldCommand.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Commands/MakeColumnCommand.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sikessem\Components\Commands;
6+
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
#[AsCommand(name: 'make:sikessem-column')]
11+
class MakeColumnCommand extends MakeCommand
12+
{
13+
protected $signature = 'make:sikessem-column {name} {component} {column} {label?} {--f|force}';
14+
15+
protected $description = 'Create a new column';
16+
17+
protected $type = 'column';
18+
19+
protected function getStub()
20+
{
21+
return $this->resolveStubPath('/stubs/column.stub');
22+
}
23+
24+
/**
25+
* Get the console command arguments.
26+
*
27+
* @return array
28+
*/
29+
#[\Override]
30+
protected function getArguments()
31+
{
32+
return [
33+
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)],
34+
['component', InputArgument::REQUIRED, 'The form component of the '.strtolower($this->type)],
35+
['column', InputArgument::REQUIRED, 'The table column of the '.strtolower($this->type)],
36+
['label', InputArgument::OPTIONAL, 'The label of the '.strtolower($this->type)],
37+
];
38+
}
39+
40+
/**
41+
* Build the class with the given name.
42+
*
43+
* @param string $name
44+
* @return string
45+
*
46+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
47+
*/
48+
#[\Override]
49+
protected function buildClass($name)
50+
{
51+
$stub = parent::buildClass($name);
52+
53+
$this
54+
->replaceArgument($stub, 'component')
55+
->replaceArgument($stub, 'column')
56+
->replaceArgument($stub, 'name')
57+
->replaceArgument($stub, 'label');
58+
59+
return $stub;
60+
}
61+
62+
protected function replaceArgument(string &$stub, string $name, string $default = 'null'): static
63+
{
64+
$subject = $name === 'name'
65+
? str($this->getNameInput())->beforeLast(ucfirst($this->type))->snake()
66+
: str($this->argument($name))->trim();
67+
68+
if ($subject->isEmpty()) {
69+
$subject = $default;
70+
} elseif (in_array($name, ['name', 'label'])) {
71+
$subject = $subject->replace(['\\', '\''], ['\\\\', '\\\''])->wrap('\'');
72+
}
73+
74+
$stub = str_replace(['Dummy'.ucfirst($name), '{{ '.$name.' }}', '{{'.$name.'}}'], (string) $subject, $stub);
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @param string $rootNamespace
81+
*/
82+
#[\Override]
83+
protected function getDefaultNamespace($rootNamespace): string
84+
{
85+
return $rootNamespace.'\\Columns';
86+
}
87+
}

src/Commands/MakeFieldCommand.php

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@
77
use Symfony\Component\Console\Attribute\AsCommand;
88
use Symfony\Component\Console\Input\InputArgument;
99

10-
#[AsCommand(name: 'make:sikessem-field')]
10+
#[AsCommand(name: 'make:sikessem-base-field')]
1111
class MakeFieldCommand extends MakeCommand
1212
{
13-
protected $signature = 'make:sikessem-field {name} {component} {column} {label?} {--f|force}';
13+
protected $signature = 'make:sikessem-field {name=Field : The name of the abstract field} {--f|force}';
1414

15-
protected $description = 'Create a new field';
15+
protected $description = 'Create the base field';
1616

1717
protected $type = 'field';
1818

19-
protected function getStub()
20-
{
21-
return $this->resolveStubPath('/stubs/field.stub');
22-
}
23-
2419
/**
2520
* Get the console command arguments.
2621
*
@@ -30,57 +25,31 @@ protected function getStub()
3025
protected function getArguments()
3126
{
3227
return [
33-
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)],
34-
['component', InputArgument::REQUIRED, 'The form component of the '.strtolower($this->type)],
35-
['column', InputArgument::REQUIRED, 'The table column of the '.strtolower($this->type)],
36-
['label', InputArgument::OPTIONAL, 'The label of the '.strtolower($this->type)],
28+
['name', InputArgument::OPTIONAL, 'The name of the '.str($this->type)->lower()],
3729
];
3830
}
3931

32+
protected function getStub()
33+
{
34+
return $this->resolveStubPath('/stubs/field.stub');
35+
}
36+
4037
/**
41-
* Build the class with the given name.
38+
* Get the desired class name from the input.
4239
*
43-
* @param string $name
4440
* @return string
45-
*
46-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4741
*/
4842
#[\Override]
49-
protected function buildClass($name)
50-
{
51-
$stub = parent::buildClass($name);
52-
53-
$this
54-
->replaceArgument($stub, 'component')
55-
->replaceArgument($stub, 'column')
56-
->replaceArgument($stub, 'name')
57-
->replaceArgument($stub, 'label');
58-
59-
return $stub;
60-
}
61-
62-
protected function replaceArgument(string &$stub, string $name, string $default = 'null'): static
43+
protected function getNameInput()
6344
{
64-
$subject = $name === 'name'
65-
? str($this->getNameInput())->beforeLast(ucfirst($this->type))->snake()
66-
: str($this->argument($name))->trim();
67-
68-
if ($subject->isEmpty()) {
69-
$subject = $default;
70-
} elseif (in_array($name, ['name', 'label'])) {
71-
$subject = $subject->replace(['\\', '\''], ['\\\\', '\\\''])->wrap('\'');
72-
}
73-
74-
$stub = str_replace(['Dummy'.ucfirst($name), '{{ '.$name.' }}', '{{'.$name.'}}'], (string) $subject, $stub);
45+
$name = str(parent::getNameInput());
46+
$type = str($this->type)->studly()->toString();
7547

76-
return $this;
48+
return $name->beforeLast($type)->studly()->toString();
7749
}
7850

79-
/**
80-
* @param string $rootNamespace
81-
*/
8251
#[\Override]
83-
protected function getDefaultNamespace($rootNamespace): string
52+
protected function getDefaultNamespace($rootNamespace)
8453
{
8554
return $rootNamespace.'\\Fields';
8655
}

src/Commands/MakeInputCommand.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sikessem\Components\Commands;
6+
7+
use Symfony\Component\Console\Attribute\AsCommand;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
10+
#[AsCommand(name: 'make:sikessem-input')]
11+
class MakeInputCommand extends MakeCommand
12+
{
13+
protected $signature = 'make:sikessem-input {name} {component} {column} {label?} {--f|force}';
14+
15+
protected $description = 'Create a new input';
16+
17+
protected $type = 'input';
18+
19+
protected function getStub()
20+
{
21+
return $this->resolveStubPath('/stubs/input.stub');
22+
}
23+
24+
/**
25+
* Get the console command arguments.
26+
*
27+
* @return array
28+
*/
29+
#[\Override]
30+
protected function getArguments()
31+
{
32+
return [
33+
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)],
34+
['component', InputArgument::REQUIRED, 'The form component of the '.strtolower($this->type)],
35+
['column', InputArgument::REQUIRED, 'The table column of the '.strtolower($this->type)],
36+
['label', InputArgument::OPTIONAL, 'The label of the '.strtolower($this->type)],
37+
];
38+
}
39+
40+
/**
41+
* Build the class with the given name.
42+
*
43+
* @param string $name
44+
* @return string
45+
*
46+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
47+
*/
48+
#[\Override]
49+
protected function buildClass($name)
50+
{
51+
$stub = parent::buildClass($name);
52+
53+
$this
54+
->replaceArgument($stub, 'component')
55+
->replaceArgument($stub, 'column')
56+
->replaceArgument($stub, 'name')
57+
->replaceArgument($stub, 'label');
58+
59+
return $stub;
60+
}
61+
62+
protected function replaceArgument(string &$stub, string $name, string $default = 'null'): static
63+
{
64+
$subject = $name === 'name'
65+
? str($this->getNameInput())->beforeLast(ucfirst($this->type))->snake()
66+
: str($this->argument($name))->trim();
67+
68+
if ($subject->isEmpty()) {
69+
$subject = $default;
70+
} elseif (in_array($name, ['name', 'label'])) {
71+
$subject = $subject->replace(['\\', '\''], ['\\\\', '\\\''])->wrap('\'');
72+
}
73+
74+
$stub = str_replace(['Dummy'.ucfirst($name), '{{ '.$name.' }}', '{{'.$name.'}}'], (string) $subject, $stub);
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @param string $rootNamespace
81+
*/
82+
#[\Override]
83+
protected function getDefaultNamespace($rootNamespace): string
84+
{
85+
return $rootNamespace.'\\Inputs';
86+
}
87+
}

src/ComponentsServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public function configurePackage(Package $package): void
2323
protected function getCommands(): array
2424
{
2525
$commands = [
26-
Commands\MakeBaseFieldCommand::class,
2726
Commands\MakeFieldCommand::class,
27+
Commands\MakeInputCommand::class,
28+
Commands\MakeColumnCommand::class,
2829
];
2930

3031
$aliases = [];

stubs/base-field.stub

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)