Last active
April 16, 2024 09:56
-
-
Save vivekwaah/9dbfe39c3e38e0431547e62583670c11 to your computer and use it in GitHub Desktop.
Display hierarchical tree structure of blade files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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