Skip to content

Commit 2c245aa

Browse files
committed
WIP
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
1 parent 65d99c9 commit 2c245aa

File tree

5 files changed

+85
-27
lines changed

5 files changed

+85
-27
lines changed

src/FileSystem.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ public function serviceInterface(string $name): File
425425
);
426426
}
427427

428+
public function templateFile(string $subDirectory, string $name): File
429+
{
430+
return new File(
431+
$this->templatesDir($subDirectory),
432+
'',
433+
$name,
434+
'html.twig'
435+
);
436+
}
437+
428438
public function templates(string $name = 'templates'): Directory
429439
{
430440
return new Directory($name, sprintf('%s/src/%s', $this->rootDir, $this->moduleName));

src/FileSystem/File.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public function __construct(
2121
private readonly Directory $parentDirectory,
2222
string $namespace,
2323
string $className,
24+
string $extension = 'php',
2425
) {
25-
$this->name = sprintf('%s.php', $className);
26+
$this->name = sprintf('%s.%s', $className, $extension);
2627
$this->path = sprintf('%s/%s', $parentDirectory->getPath(), $this->name);
2728
$this->component = new Component($namespace, $className);
2829
}

src/Type/Handler.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,55 +36,116 @@ public function create(string $name): File
3636
{
3737
$name = preg_replace('/Handler$/', '', $name);
3838

39+
$entity = $this->fileSystem->entity($name);
40+
3941
$plural = Component::pluralize($name);
4042
if ($this->context->isApi()) {
4143
if (Input::confirm(sprintf('Allow listing %s?', $plural))) {
4244
$this->component(TypeEnum::Collection)->create($name);
4345
$this->component(TypeEnum::HandlerApiGetCollection)->create($name);
4446
}
47+
4548
if (Input::confirm(sprintf('Allow viewing %s?', $plural))) {
4649
$this->component(TypeEnum::HandlerApiGetResource)->create($name);
4750
}
51+
4852
if (Input::confirm(sprintf('Allow creating %s?', $plural))) {
4953
$this->component(TypeEnum::HandlerApiPostResource)->create($name);
5054
$this->component(TypeEnum::InputFilterCreateResource)->create($name);
5155
}
56+
5257
if (Input::confirm(sprintf('Allow deleting %s?', $plural))) {
5358
$this->component(TypeEnum::HandlerApiDeleteResource)->create($name);
5459
}
60+
5561
if (Input::confirm(sprintf('Allow editing %s?', $plural))) {
5662
$this->component(TypeEnum::HandlerApiPatchResource)->create($name);
5763
$this->component(TypeEnum::InputFilterEditResource)->create($name);
5864
}
65+
5966
if (Input::confirm(sprintf('Allow replacing %s?', $plural))) {
6067
$this->component(TypeEnum::HandlerApiPutResource)->create($name);
6168
$this->component(TypeEnum::InputFilterReplaceResource)->create($name);
6269
}
6370
} else {
6471
if (Input::confirm(sprintf('Allow listing %s?', $plural))) {
6572
$this->component(TypeEnum::HandlerGetListResource)->create($name);
73+
74+
$template = $this->fileSystem
75+
->templateFile(
76+
$entity->getComponent()->toKebabCase(),
77+
sprintf('%s-list', $entity->getComponent()->toKebabCase())
78+
);
79+
if (! $template->exists()) {
80+
$template->create(sprintf('List %s template', $plural));
81+
Output::success(sprintf('Created template file: %s', $template->getPath()));
82+
}
6683
}
84+
6785
if (Input::confirm(sprintf('Allow viewing %s?', $plural))) {
6886
$this->component(TypeEnum::HandlerGetViewResource)->create($name);
87+
88+
$template = $this->fileSystem
89+
->templateFile(
90+
$entity->getComponent()->toKebabCase(),
91+
sprintf('%s-view', $entity->getComponent()->toKebabCase())
92+
);
93+
if (! $template->exists()) {
94+
$template->create(sprintf('View %s template', $plural));
95+
Output::success(sprintf('Created template file: %s', $template->getPath()));
96+
}
6997
}
98+
7099
if (Input::confirm(sprintf('Allow creating %s?', $plural))) {
71100
$this->component(TypeEnum::HandlerGetCreateResource)->create($name);
72101
$this->component(TypeEnum::HandlerPostCreateResource)->create($name);
73102
$this->component(TypeEnum::FormCreateResource)->create($name);
74103
$this->component(TypeEnum::InputFilterCreateResource)->create($name);
104+
105+
$template = $this->fileSystem
106+
->templateFile(
107+
$entity->getComponent()->toKebabCase(),
108+
sprintf('%s-create-form', $entity->getComponent()->toKebabCase())
109+
);
110+
if (! $template->exists()) {
111+
$template->create(sprintf('Create %s template', $plural));
112+
Output::success(sprintf('Created template file: %s', $template->getPath()));
113+
}
75114
}
115+
76116
if (Input::confirm(sprintf('Allow deleting %s?', $plural))) {
77117
$this->component(TypeEnum::HandlerGetDeleteResource)->create($name);
78118
$this->component(TypeEnum::HandlerPostDeleteResource)->create($name);
79119
$this->component(TypeEnum::FormDeleteResource)->create($name);
80120
$this->component(TypeEnum::InputFilterDeleteResource)->create($name);
81121
$this->component(TypeEnum::Input)->create(sprintf('%sConfirmation', $name));
122+
123+
$template = $this->fileSystem
124+
->templateFile(
125+
$entity->getComponent()->toKebabCase(),
126+
sprintf('%s-delete-form', $entity->getComponent()->toKebabCase())
127+
);
128+
if (! $template->exists()) {
129+
$template->create(sprintf('Delete %s template', $plural));
130+
Output::success(sprintf('Created template file: %s', $template->getPath()));
131+
}
82132
}
133+
83134
if (Input::confirm(sprintf('Allow editing %s?', $plural))) {
84135
$this->component(TypeEnum::HandlerGetEditResource)->create($name);
85136
$this->component(TypeEnum::HandlerPostEditResource)->create($name);
86137
$this->component(TypeEnum::FormEditResource)->create($name);
87138
$this->component(TypeEnum::InputFilterEditResource)->create($name);
139+
140+
$template = $this->fileSystem
141+
->templateFile(
142+
$entity->getComponent()->toKebabCase(),
143+
sprintf('%s-edit-form', $entity->getComponent()->toKebabCase())
144+
);
145+
if (! $template->exists()) {
146+
$template->create(sprintf('Edit %s template', $plural));
147+
Output::success(sprintf('Created template file: %s', $template->getPath()));
148+
}
88149
}
89150
}
90151

src/Type/Help.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ public function __invoke(): void
1515
1616
Usage: ./vendor/bin/dot-maker <component>
1717
18-
Where <component> must be one of the following strings:
19-
20-
- collection
21-
- command
22-
- command
23-
- entity
24-
- form
25-
- handler
26-
- input-filter
27-
- middleware
28-
- module
29-
- repository
30-
- service
18+
Where <component> must be replaced with one of the following strings:
19+
- collection
20+
- command
21+
- command
22+
- entity
23+
- form
24+
- handler
25+
- input-filter
26+
- middleware
27+
- module
28+
- repository
29+
- service
3130
HELP);
3231
}
3332
}

src/Type/Module.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ public function __invoke(): void
6363
$this->component(TypeEnum::RoutesDelegator)->create($module->getName());
6464
}
6565

66-
if (! $this->context->isApi()) {
67-
$entity = $this->fileSystem->entity($module->getName());
68-
69-
$templates = $this->fileSystem->templates();
70-
if (! $templates->exists()) {
71-
$templates->create();
72-
}
73-
$templatesDir = $this->fileSystem->templatesDir($entity->getComponent()->toKebabCase());
74-
if (! $templatesDir->exists()) {
75-
$templatesDir->create();
76-
}
77-
}
78-
7966
Output::writeLine('');
8067

8168
if ($this->context->isApi()) {

0 commit comments

Comments
 (0)