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

137 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
2024-05-12 14:47:44 +02:00
use App\Console\Commands\Bundle\Traits\ReadsBundles;
use App\Console\Commands\Bundle\Traits\SelectsDisks;
use App\Exceptions\BundleUpdaterCannotBeFound;
use App\Exceptions\UnableToFindWikidataEntityId;
use App\Services\BundleUpdaters\Facades\BundleUpdater;
use Illuminate\Console\Command;
2024-05-12 14:47:44 +02:00
use function Laravel\Prompts\progress;
class Update extends Command
{
2024-05-12 14:47:44 +02:00
use ReadsBundles;
use SelectsDisks;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update bundles extended metadata';
2024-05-12 14:47:44 +02:00
protected $notUpdatable = [];
protected $updatedCount = 0;
protected $notUpdatedCount = 0;
public function __construct()
{
$this->signature = 'bundle:update
{ --r|recursive : Also update sub-bundles }
2024-05-12 14:47:44 +02:00
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to update - Default to <info>/</info> }
2024-05-12 14:47:44 +02:00
';
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
2024-05-12 14:47:44 +02:00
$this->selectDisk()
->selectBundles()
->update()
->showReport();
}
2024-05-12 14:47:44 +02:00
/**
* Updates specific bundle
*/
private function handleBundle(Bundle $bundle, $progress)
2024-05-12 14:47:44 +02:00
{
$progress->hint(sprintf('Updated %s', $bundle->getPath()));
2024-05-12 14:47:44 +02:00
try {
$updater = BundleUpdater::getBundleUpdaterFor($bundle);
} catch (BundleUpdaterCannotBeFound $ex) {
return;
}
2024-05-12 14:47:44 +02:00
try {
while (!$updater->canUpdateBundle()) {
$specs = $updater->formSpecs();
foreach ($specs as $key => $func) {
$result = $func();
$bundle->metadata()->set($key, $result);
$bundle->save();
}
}
2024-05-12 14:47:44 +02:00
} catch (UnableToFindWikidataEntityId $ex) {
$this->notUpdatable[$bundle->getPath()] = 'Missing Wikidata Entity Id';
2024-05-12 14:47:44 +02:00
return;
}
2024-05-12 14:47:44 +02:00
$result = $updater->update();
2024-05-12 14:47:44 +02:00
if ($result) {
$this->updatedCount++;
} else {
$this->notUpdatedCount++;
}
}
2024-05-12 14:47:44 +02:00
/**
* Performs update on selected bundles
*/
private function update(): self
{
progress('Updating bundles...', $this->bundles, function (Bundle $bundle, $progress) {
$this->handleBundle($bundle, $progress);
});
2024-05-12 14:47:44 +02:00
return $this;
}
/**
* Show a report of update procedure
*/
private function showReport()
{
$this->line(sprintf('Updated bundles: <info>%d</info>', $this->updatedCount));
$this->line(sprintf('Update not needed: <comment>%d</comment>', $this->notUpdatedCount));
$this->reportNonUpdatable();
}
/**
* Show report of non updatable bundles
*/
private function reportNonUpdatable()
{
$count = count($this->notUpdatable);
if ($count === 0) {
return;
}
$this->newLine();
$this->line(sprintf('Not updatable bundles: <comment>%d</comment>', $count));
foreach ($this->notUpdatable as $bundle => $reason) {
$this->newLine();
$this->comment($bundle);
$this->line(sprintf(' > %s', $reason));
}
}
}