From 9fd1273ad79a46bb3aa006129109c6bc72766e4b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 14 Mar 2018 16:03:01 -0500 Subject: [PATCH] add blade cache command --- .../Foundation/Console/BladeCacheCommand.php | 83 +++++++++++++++++++ .../Providers/ArtisanServiceProvider.php | 14 ++++ 2 files changed, 97 insertions(+) create mode 100644 src/Illuminate/Foundation/Console/BladeCacheCommand.php diff --git a/src/Illuminate/Foundation/Console/BladeCacheCommand.php b/src/Illuminate/Foundation/Console/BladeCacheCommand.php new file mode 100644 index 000000000000..f813a28c655b --- /dev/null +++ b/src/Illuminate/Foundation/Console/BladeCacheCommand.php @@ -0,0 +1,83 @@ +paths()->each(function ($path) { + $this->compileViews($this->bladeFilesIn([$path])); + }); + + $this->info('Blade templates cached successfully!'); + } + + /** + * Compile the given view files. + * + * @param \Illuminate\Support\Collection $views + * @return void + */ + protected function compileViews(Collection $views) + { + $compiler = $this->laravel['blade.compiler']; + + $views->map(function (SplFileInfo $file) use ($compiler) { + $compiler->compile($file->getRealPath()); + }); + } + + /** + * Get the Blade files in the given path. + * + * @param array $paths + * @return \Illuminate\Support\Collection + */ + protected function bladeFilesIn(array $paths) + { + return collect(Finder::create()-> + in($paths) + ->exclude('vendor') + ->name('*.blade.php')->files()); + } + + /** + * Get all of the possible view paths. + * + * @return \Illuminate\Support\Collection + */ + protected function paths() + { + $finder = $this->laravel['view']->getFinder(); + + return collect($finder->getPaths())->merge( + collect($finder->getHints())->flatten() + ); + } +} diff --git a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php index 6fe0655a9848..a44af2cb498a 100755 --- a/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php @@ -25,6 +25,7 @@ use Illuminate\Session\Console\SessionTableCommand; use Illuminate\Foundation\Console\PolicyMakeCommand; use Illuminate\Foundation\Console\RouteCacheCommand; +use Illuminate\Foundation\Console\BladeCacheCommand; use Illuminate\Foundation\Console\RouteClearCommand; use Illuminate\Console\Scheduling\ScheduleRunCommand; use Illuminate\Foundation\Console\ChannelMakeCommand; @@ -83,6 +84,7 @@ class ArtisanServiceProvider extends ServiceProvider * @var array */ protected $commands = [ + 'BladeCache' => 'command.blade.cache', 'CacheClear' => 'command.cache.clear', 'CacheForget' => 'command.cache.forget', 'ClearCompiled' => 'command.clear-compiled', @@ -208,6 +210,18 @@ protected function registerAuthMakeCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerBladeCacheCommand() + { + $this->app->singleton('command.blade.cache', function ($app) { + return new BladeCacheCommand; + }); + } + /** * Register the command. *