2024-04-24 20:53:37 +02:00
|
|
|
<?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;
|
2024-04-24 20:53:37 +02:00
|
|
|
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;
|
2024-04-24 20:53:37 +02:00
|
|
|
|
|
|
|
class Update extends Command
|
|
|
|
{
|
2024-05-12 14:47:44 +02:00
|
|
|
use ReadsBundles;
|
|
|
|
use SelectsDisks;
|
2024-04-24 20:53:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2024-05-13 23:08:13 +02:00
|
|
|
{ --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> }
|
2024-05-13 23:08:13 +02:00
|
|
|
{ path? : Path to a specific bundle to update - Default to <info>/</info> }
|
2024-05-12 14:47:44 +02:00
|
|
|
';
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2024-04-24 20:53:37 +02:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-05-12 14:47:44 +02:00
|
|
|
$this->selectDisk()
|
|
|
|
->selectBundles()
|
|
|
|
->update()
|
|
|
|
->showReport();
|
|
|
|
}
|
2024-04-24 20:53:37 +02:00
|
|
|
|
2024-05-12 14:47:44 +02:00
|
|
|
/**
|
|
|
|
* Updates specific bundle
|
|
|
|
*/
|
2024-05-13 23:08:13 +02:00
|
|
|
private function handleBundle(Bundle $bundle, $progress)
|
2024-05-12 14:47:44 +02:00
|
|
|
{
|
|
|
|
$progress->hint(sprintf('Updated %s', $bundle->getPath()));
|
2024-04-24 20:53:37 +02:00
|
|
|
|
2024-05-12 14:47:44 +02:00
|
|
|
try {
|
|
|
|
$updater = BundleUpdater::getBundleUpdaterFor($bundle);
|
|
|
|
} catch (BundleUpdaterCannotBeFound $ex) {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-24 20:53:37 +02:00
|
|
|
|
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-04-24 20:53:37 +02:00
|
|
|
}
|
2024-05-12 14:47:44 +02:00
|
|
|
} catch (UnableToFindWikidataEntityId $ex) {
|
|
|
|
$this->notUpdatable[$bundle->getPath()] = 'Missing Wikidata Entity Id';
|
2024-04-24 20:53:37 +02:00
|
|
|
|
2024-05-12 14:47:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-04-24 20:53:37 +02:00
|
|
|
|
2024-05-12 14:47:44 +02:00
|
|
|
$result = $updater->update();
|
2024-04-24 20:53:37 +02:00
|
|
|
|
2024-05-12 14:47:44 +02:00
|
|
|
if ($result) {
|
|
|
|
$this->updatedCount++;
|
|
|
|
} else {
|
|
|
|
$this->notUpdatedCount++;
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 20:53:37 +02:00
|
|
|
|
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-04-24 20:53:37 +02:00
|
|
|
|
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));
|
2024-04-24 20:53:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|