Skip to content

Instantly share code, notes, and snippets.

@vivekwaah
Last active April 16, 2024 09:56
Show Gist options
  • Save vivekwaah/9dbfe39c3e38e0431547e62583670c11 to your computer and use it in GitHub Desktop.
Save vivekwaah/9dbfe39c3e38e0431547e62583670c11 to your computer and use it in GitHub Desktop.
Display hierarchical tree structure of blade files
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ShowBladeTree extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:views-tree';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display hierarchical tree structure of blade files';
protected $colors = [
'bright-white',
'bright-green',
'bright-cyan',
'bright-magenta',
'bright-yellow',
'bright-red',
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
'default',
];
/**
* Execute the console command.
*/
public function handle()
{
$bladeDirectory = resource_path('views');
$this->traverseDirectory($bladeDirectory);
}
protected function traverseDirectory($directory, $depth = 0)
{
$color = $this->colors[$depth % count($this->colors)];
$directories = File::directories($directory);
foreach ($directories as $dir) {
$dirName = basename($dir);
$this->line("<fg={$color}>" . str_repeat('| ', $depth) . '|-- ' . $dirName . "</>");
$this->traverseDirectory($dir, $depth + 1);
}
$files = File::files($directory);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$fileName = Str::replace('.blade', '', pathinfo($file, PATHINFO_FILENAME));
$this->line(
"<fg={$color}>" .
str_repeat('| ', $depth) .
'|-- ' . $fileName . "</>"
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment