1
0
cms11/app/Console/Commands/Bundle/Create.php

89 lines
2.2 KiB
PHP
Raw Normal View History

2024-04-17 11:41:10 +02:00
<?php
namespace App\Console\Commands\Bundle;
2024-04-17 11:41:10 +02:00
use App\Services\BundleCreators\Contracts\CreatesBundle;
use App\Services\BundleCreators\Facades\BundleCreator;
2024-04-17 11:41:10 +02:00
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use function Laravel\Prompts\select;
class Create extends Command implements PromptsForMissingInput
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:create { section : Specific section in which the article will be created }';
2024-04-17 11:41:10 +02:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create new article';
/**
* Execute the console command.
*/
public function handle()
{
$section = $this->argument('section');
$data = [];
$creator = $this->getBundleCreator($section);
while (!$creator->canCreateBundle()) {
$specs = $creator->formSpecs();
foreach ($specs as $key => $func) {
$data[$key] = $func();
}
$creator = $this->getBundleCreator($section, $data);
}
$path = $creator->createBundle();
$this->info('Bundle created successfully!');
$this->line($path);
}
/**
* Prompt for missing input arguments using the returned questions.
*
* @return array<string, string>
*/
protected function promptForMissingArgumentsUsing(): array
{
return [
'section' => fn () => select('Article section', $this->listSections()),
];
}
/**
* Return either an instance of the bundle creator or form specifications
*/
protected function getBundleCreator(string $section, ?array $data = []): array|CreatesBundle
{
return BundleCreator::getBundleCreatorFor($section, $data);
}
2024-04-17 11:41:10 +02:00
/**
* List available sections as options for a select input
*/
private function listSections()
{
$sections = config('sections');
$options = [];
foreach ($sections as $name => $data) {
$options[$name] = $data['title'];
}
return $options;
}
}