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

102 lines
3.1 KiB
PHP

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class Repair extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:repair { --r|recursive : Also repair sub-bundles } { path? : Specific bundle to repair }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Repair articles';
/**
* Execute the console command.
*/
public function handle()
{
$disk = Storage::disk(env('CONTENT_DISK'));
$path = $this->argument('path') ?? '/';
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($disk, $path, true);
} else {
$bundles = [new Bundle($path, $disk)];
}
foreach ($bundles as $bundle) {
$this->output->write(sprintf('Repairing %s... ', $bundle->getPath()));
$bundle->repair();
$this->info('OK');
}
foreach (['blog', 'liens-interessants'] as $section) {
$this->createIntermediateBundles($section);
}
}
protected function createIntermediateBundles($parent)
{
$disk = Storage::disk(env('CONTENT_DISK'));
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());
}
}
}
}