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

79 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class DeadLinks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:dead-links
{ --c|clear : Clear current links.json file }
{ --r|recursive : Also check sub-bundles }
{ path? : Specific bundle for which to find dead links }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Find dead links in bundles';
/**
* 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) {
$bundle->load();
if ($this->option('clear')) {
$bundle->metadata('links')->clear();
$bundle->metadata('links')->save();
continue;
}
$deadLinks = [];
foreach ($bundle->metadata('links')->all() as $url => $data) {
if ($data['isDead']) {
$deadLinks[$url] = $data['reason'];
}
}
if (!empty($deadLinks)) {
$this->output->write(sprintf('Looking in %s... ', $bundle->getPath()));
$this->error('Dead links');
foreach ($deadLinks as $url => $reason) {
$this->line(sprintf(' - [%s]: %s', $url, $reason));
}
$this->newLine();
}
}
if ($this->option('clear')) {
$this->call('cache:clear');
$this->comment('links.json file(s) have been removed.');
$this->line('Remember to re-render bundles to check for dead links.');
}
}
}