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

92 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use App\Services\BundleCreators\Contracts\CreatesBundle;
use App\Services\BundleCreators\Facades\BundleCreator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Support\Facades\Storage;
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 }';
/**
* 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->call('bundle:update', ['path' => $path]);
$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);
}
/**
* List available sections as options for a select input
*/
private function listSections()
{
$sections = Bundle::findBundles(Storage::disk(env('CONTENT_DISK')), '/');
$options = [];
foreach ($sections as $bundle) {
$options[basename($bundle->getPath())] = $bundle->getArticleTitle();
}
return $options;
}
}