Skip to content

Commit 9fd1273

Browse files
committed
add blade cache command
1 parent 82d8165 commit 9fd1273

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Illuminate\Session\Console\SessionTableCommand;
2626
use Illuminate\Foundation\Console\PolicyMakeCommand;
2727
use Illuminate\Foundation\Console\RouteCacheCommand;
28+
use Illuminate\Foundation\Console\BladeCacheCommand;
2829
use Illuminate\Foundation\Console\RouteClearCommand;
2930
use Illuminate\Console\Scheduling\ScheduleRunCommand;
3031
use Illuminate\Foundation\Console\ChannelMakeCommand;
@@ -83,6 +84,7 @@ class ArtisanServiceProvider extends ServiceProvider
8384
* @var array
8485
*/
8586
protected $commands = [
87+
'BladeCache' => 'command.blade.cache',
8688
'CacheClear' => 'command.cache.clear',
8789
'CacheForget' => 'command.cache.forget',
8890
'ClearCompiled' => 'command.clear-compiled',
@@ -208,6 +210,18 @@ protected function registerAuthMakeCommand()
208210
});
209211
}
210212

213+
/**
214+
* Register the command.
215+
*
216+
* @return void
217+
*/
218+
protected function registerBladeCacheCommand()
219+
{
220+
$this->app->singleton('command.blade.cache', function ($app) {
221+
return new BladeCacheCommand;
222+
});
223+
}
224+
211225
/**
212226
* Register the command.
213227
*

0 commit comments

Comments
 (0)