|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Foundation\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use Illuminate\Support\Facades\View; |
| 8 | +use Symfony\Component\Finder\Finder; |
| 9 | +use Symfony\Component\Finder\SplFileInfo; |
| 10 | + |
| 11 | +class BladeCacheCommand extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'blade:cache'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = "Compile all of the application's Blade templates"; |
| 26 | + |
| 27 | + /** |
| 28 | + * Execute the console command. |
| 29 | + * |
| 30 | + * @return mixed |
| 31 | + */ |
| 32 | + public function handle() |
| 33 | + { |
| 34 | + $this->paths()->each(function ($path) { |
| 35 | + $this->compileViews($this->bladeFilesIn([$path])); |
| 36 | + }); |
| 37 | + |
| 38 | + $this->info('Blade templates cached successfully!'); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Compile the given view files. |
| 43 | + * |
| 44 | + * @param \Illuminate\Support\Collection $views |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function compileViews(Collection $views) |
| 48 | + { |
| 49 | + $compiler = $this->laravel['blade.compiler']; |
| 50 | + |
| 51 | + $views->map(function (SplFileInfo $file) use ($compiler) { |
| 52 | + $compiler->compile($file->getRealPath()); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the Blade files in the given path. |
| 58 | + * |
| 59 | + * @param array $paths |
| 60 | + * @return \Illuminate\Support\Collection |
| 61 | + */ |
| 62 | + protected function bladeFilesIn(array $paths) |
| 63 | + { |
| 64 | + return collect(Finder::create()-> |
| 65 | + in($paths) |
| 66 | + ->exclude('vendor') |
| 67 | + ->name('*.blade.php')->files()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get all of the possible view paths. |
| 72 | + * |
| 73 | + * @return \Illuminate\Support\Collection |
| 74 | + */ |
| 75 | + protected function paths() |
| 76 | + { |
| 77 | + $finder = $this->laravel['view']->getFinder(); |
| 78 | + |
| 79 | + return collect($finder->getPaths())->merge( |
| 80 | + collect($finder->getHints())->flatten() |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
0 commit comments