2024-04-17 11:41:10 +02:00
|
|
|
<?php
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
namespace App\Console\Commands\Bundle;
|
2024-04-17 11:41:10 +02:00
|
|
|
|
2024-04-21 16:38:09 +02:00
|
|
|
use App\Classes\Bundle;
|
2024-04-20 23:27:47 +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;
|
2024-04-21 16:38:09 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-04-17 11:41:10 +02:00
|
|
|
|
|
|
|
use function Laravel\Prompts\select;
|
|
|
|
|
|
|
|
class Create extends Command implements PromptsForMissingInput
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2024-04-20 23:27:47 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-20 23:27:47 +02:00
|
|
|
/**
|
|
|
|
* 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()
|
|
|
|
{
|
2024-04-21 16:38:09 +02:00
|
|
|
$sections = Bundle::findBundles(Storage::disk(env('CONTENT_DISK')), '/');
|
2024-04-17 11:41:10 +02:00
|
|
|
$options = [];
|
|
|
|
|
2024-04-21 16:38:09 +02:00
|
|
|
foreach ($sections as $bundle) {
|
|
|
|
$options[basename($bundle->getPath())] = $bundle->getArticleTitle();
|
2024-04-17 11:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
}
|