Skip to content

Commit 6c18b81

Browse files
committed
various updates
1 parent 68dcd7f commit 6c18b81

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

src/Command/ComponentGeneration/Security/SecuritySchemeTuiGenerator.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ private function showForm(Tui $tui, SecuritySchemeTuiState $state, callable $onB
190190
$tui->getEventDispatcher()->removeListener(CancelEvent::class, $cancelListener);
191191
$this->showComponentList($tui, $onBack);
192192
break;
193+
case 'action_delete':
194+
$this->deleteComponent($state);
195+
$tui->getEventDispatcher()->removeListener(SettingChangeEvent::class, $changeListener);
196+
$tui->getEventDispatcher()->removeListener(CancelEvent::class, $cancelListener);
197+
$this->showComponentList($tui, $onBack);
198+
break;
193199
case 'action_validate':
194200
$state->name = $settingsWidget->getValue('name') ?? '';
195201
$state->type = $settingsWidget->getValue('type') ?? 'http';
@@ -283,4 +289,21 @@ private function generateComponent(SecuritySchemeTuiState $state): void
283289

284290
$this->currentOutput->writeln(sprintf('<info>Security Scheme "%s" generated successfully in %s</info>', $state->name, $dumpLocation));
285291
}
292+
293+
private function deleteComponent(SecuritySchemeTuiState $state): void
294+
{
295+
if (null === $state->loadedFrom || !file_exists($state->loadedFrom)) {
296+
return;
297+
}
298+
299+
$existingConfig = \Symfony\Component\Yaml\Yaml::parseFile($state->loadedFrom);
300+
if (!isset($existingConfig['documentation']['components']['securitySchemes'])) {
301+
return;
302+
}
303+
304+
unset($existingConfig['documentation']['components']['securitySchemes'][$state->name]);
305+
306+
$this->writeYamlFile($existingConfig, $state->loadedFrom, $this->currentOutput);
307+
$this->currentOutput->writeln(sprintf('<info>Security Scheme "%s" deleted from %s</info>', $state->name, $state->loadedFrom));
308+
}
286309
}

src/Command/ComponentGeneration/Tag/TagTuiGenerator.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ private function showForm(Tui $tui, TagTuiState $state, callable $onBack): void
152152
$settingItems[] = new SettingItem('format_output', 'Output Format', $state->format_output, 'YAML or PHP', ['yaml', 'php']);
153153
$settingItems[] = new SettingItem('output', 'Output Directory', $state->outputDir, 'Target directory', [], $textInputCallback);
154154
$settingItems[] = new SettingItem('action_validate', 'Save', '✓ Confirm', 'Save and return to the list.', ['✓ Confirm']);
155+
if (null !== $state->loadedFrom) {
156+
$settingItems[] = new SettingItem('action_delete', 'Delete', '✗ Delete', 'Delete this tag.', ['✗ Delete']);
157+
}
155158
$settingItems[] = new SettingItem('action_cancel', 'Cancel', '← Cancel', 'Return without saving.', ['← Cancel']);
156159

157160
$settingsWidget = new SettingsListWidget($settingItems, 12);
@@ -180,6 +183,12 @@ private function showForm(Tui $tui, TagTuiState $state, callable $onBack): void
180183
$tui->getEventDispatcher()->removeListener(CancelEvent::class, $cancelListener);
181184
$this->showComponentList($tui, $onBack);
182185
break;
186+
case 'action_delete':
187+
$this->deleteComponent($state);
188+
$tui->getEventDispatcher()->removeListener(SettingChangeEvent::class, $changeListener);
189+
$tui->getEventDispatcher()->removeListener(CancelEvent::class, $cancelListener);
190+
$this->showComponentList($tui, $onBack);
191+
break;
183192
case 'action_validate':
184193
$state->name = $settingsWidget->getValue('name') ?? '';
185194
$state->description = $settingsWidget->getValue('desc') ?? '';
@@ -278,4 +287,24 @@ private function generateComponent(TagTuiState $state): void
278287

279288
$this->currentOutput->writeln(sprintf('<info>Tag "%s" generated successfully in %s</info>', $state->name, $dumpLocation));
280289
}
290+
291+
private function deleteComponent(TagTuiState $state): void
292+
{
293+
if (null === $state->loadedFrom || !file_exists($state->loadedFrom)) {
294+
return;
295+
}
296+
297+
$existingConfig = \Symfony\Component\Yaml\Yaml::parseFile($state->loadedFrom);
298+
if (!isset($existingConfig['documentation']['tags']) || !is_array($existingConfig['documentation']['tags'])) {
299+
return;
300+
}
301+
302+
$existingConfig['documentation']['tags'] = array_values(array_filter(
303+
$existingConfig['documentation']['tags'],
304+
static fn (array $tag): bool => isset($tag['name']) && $tag['name'] !== $state->name,
305+
));
306+
307+
$this->writeYamlFile($existingConfig, $state->loadedFrom, $this->currentOutput);
308+
$this->currentOutput->writeln(sprintf('<info>Tag "%s" deleted from %s</info>', $state->name, $state->loadedFrom));
309+
}
281310
}

src/Command/Traits/GenerateFileTrait.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ public function generatePhpBuilderCode(array $array, string $componentName, stri
312312
} elseif ('pathItems' === $componentType) {
313313
$pathItem = $array['documentation']['components']['pathItems'][$componentName] ?? [];
314314
$code .= $this->buildPathItemCode($componentName, $pathItem, 2);
315+
} elseif ('tags' === $componentType) {
316+
$tag = $this->findTagByName($array, $componentName);
317+
$code .= $this->buildTagCode($componentName, $tag, 2);
315318
}
316319

317320
$code .= " }\n";
@@ -719,6 +722,53 @@ protected function buildPathItemCode(string $name, array $pathItem, int $indent)
719722
return $code;
720723
}
721724

725+
/**
726+
* Find a tag by name in the documentation.tags array.
727+
*
728+
* @param array<mixed> $array
729+
*
730+
* @return array<mixed>
731+
*/
732+
private function findTagByName(array $array, string $name): array
733+
{
734+
$tags = $array['documentation']['tags'] ?? [];
735+
foreach ($tags as $tag) {
736+
if (isset($tag['name']) && $tag['name'] === $name) {
737+
return $tag;
738+
}
739+
}
740+
741+
return [];
742+
}
743+
744+
/**
745+
* @param array<mixed> $tag
746+
*/
747+
protected function buildTagCode(string $name, array $tag, int $indent): string
748+
{
749+
$pad = str_repeat(' ', $indent);
750+
$code = "{$pad}\$builder->addTag('{$name}')\n";
751+
752+
if (isset($tag['description'])) {
753+
$description = addslashes($tag['description']);
754+
$code .= "{$pad} ->description('{$description}')\n";
755+
}
756+
757+
if (isset($tag['externalDocs']) && is_array($tag['externalDocs'])) {
758+
$url = addslashes((string)($tag['externalDocs']['url'] ?? ''));
759+
$desc = isset($tag['externalDocs']['description']) ? addslashes((string)$tag['externalDocs']['description']) : null;
760+
if (null !== $desc) {
761+
$code .= "{$pad} ->externalDocs('{$url}', '{$desc}')\n";
762+
} else {
763+
$code .= "{$pad} ->externalDocs('{$url}')\n";
764+
}
765+
}
766+
767+
$code .= "{$pad}->end();\n";
768+
769+
return $code;
770+
}
771+
722772
/**
723773
* Build the full output path for a file.
724774
*/

0 commit comments

Comments
 (0)