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

122 lines
3.5 KiB
PHP

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use Carbon\Carbon;
use Illuminate\Console\Command;
use function Laravel\Prompts\progress;
class Repair extends Command
{
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Repair bundles';
public function __construct()
{
$this->signature = 'bundle:repair
{ --r|recursive : Also repair sub-bundles }
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to repair - Default to <info>/</info> }
';
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$this->selectDisk()
->selectBundles()
->repair();
foreach (['blog', 'liens-interessants'] as $section) {
$this->createIntermediateBundles($section);
}
}
/**
* Create intermediary bundles
*/
private function createIntermediateBundles($parent)
{
$disk = $this->sourceDisk;
foreach ($disk->directories($parent) as $dir) {
if (basename($dir) === 'data') {
continue;
}
$bundle = new Bundle($dir, $disk);
if (!$bundle->exists()) {
$bundle->markdown()->set('');
$parts = preg_split('#/#', $bundle->getPath(), -1, PREG_SPLIT_NO_EMPTY);
$count = count($parts);
switch ($count) {
case 2:
// A year is specified
$date = sprintf('%s-%s-%s', $parts[1], '01', '01');
$date = Carbon::parse($date);
$articleTitle = sprintf('Publiés en %s', $date->isoFormat('YYYY'));
break;
case 3:
// A month is specified
$date = sprintf('%s-%s-%s', $parts[1], $parts[2], '01');
$date = Carbon::parse($date);
$articleTitle = sprintf('Publiés en %s', $date->isoFormat('MMMM YYYY'));
break;
case 4:
// A day is specified
$date = sprintf('%s-%s-%s', $parts[1], $parts[2], $parts[3]);
$date = Carbon::parse($date);
$articleTitle = sprintf('Publiés le %s', $date->isoFormat('DD MMMM YYYY'));
break;
}
if (!empty($articleTitle)) {
$bundle->metadata()->set('title', $articleTitle);
}
$bundle->save();
}
$this->createIntermediateBundles($bundle->getPath());
}
}
/**
* Repair bundles
*/
private function repair()
{
progress('Repairing bundles...', $this->bundles, function (Bundle $bundle, $progress) {
$this->handleBundle($bundle, $progress);
});
}
/**
* Repair specific bundle
*/
private function handleBundle(Bundle $bundle, $progress)
{
$bundle->repair();
$progress->label(sprintf('Repaired %s', $bundle->getPath()));
}
}