88 lines
2.1 KiB
PHP
88 lines
2.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Console\Commands\Article;
|
||
|
|
||
|
use App\Services\BundleCreator\Contracts\CreatesBundle;
|
||
|
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 = 'article:create { section : Specific section in which the article will be created }';
|
||
|
|
||
|
/**
|
||
|
* The console command description.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $description = 'Create new article';
|
||
|
|
||
|
/**
|
||
|
* 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 app()->make('bundleCreator.factory')->getBundleCreatorFor($section, $data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
}
|
||
|
}
|