/** * Register the Blade view engine with Laravel. * * @return void */ public static function sharpen() { Event::listen(View::engine, function ($view) { // The Blade view engine should only handle the rendering of views which // end with the Blade extension. If the given view does not, we will // return false so the View can be rendered as normal. if (!str_contains($view->path, BLADE_EXT)) { return; } $compiled = Blade::compiled($view->path); // If the view doesn't exist or has been modified since the last time it // was compiled, we will recompile the view into pure PHP from it's // Blade representation, writing it to cached storage. if (!file_exists($compiled) or Blade::expired($view->view, $view->path)) { file_put_contents($compiled, Blade::compile($view)); } $view->path = $compiled; // Once the view has been compiled, we can simply set the path to the // compiled view on the view instance and call the typical "get" // method on the view to evaluate the compiled PHP view. return ltrim($view->get()); }); }
/** * Test the compilation of render statements. * * @group laravel */ public function testRendersAreCompiledCorrectly() { $blade1 = "@render('user.profile')"; $blade2 = "@render(Config::get('application.default_view', 'user.profile'))"; $this->assertEquals("<?php echo render('user.profile'); ?>", Blade::compile_string($blade1)); $this->assertEquals("<?php echo render(Config::get('application.default_view', 'user.profile')); ?>", Blade::compile_string($blade2)); }
/** * Compile the Bladed view and return the path to the compiled view. * * @return string */ protected function compile() { // For simplicity, compiled views are stored in a single directory by // the MD5 hash of their name. This allows us to avoid recreating the // entire view directory structure within the compiled directory. $compiled = STORAGE_PATH . 'views/' . md5($this->view); // The view will only be re-compiled if the view has been modified // since the last compiled version of the view was created or no // compiled view exists. Otherwise, the path will be returned // without re-compiling. if (!file_exists($compiled) or filemtime($this->path) > filemtime($compiled)) { file_put_contents($compiled, Blade::compile($this->path)); } return $compiled; }
/** * Get the path to the compiled version of the Blade view. * * @return string */ protected function compile() { // Compiled views are stored in the storage directory using the MD5 // hash of their path. This allows us to easily store the views in // the directory without worrying about structure. $compiled = path('storage') . 'views/' . md5($this->path); // The view will only be re-compiled if the view has been modified // since the last compiled version of the view was created or no // compiled view exists at all in storage. if (!file_exists($compiled) or filemtime($this->path) > filemtime($compiled)) { file_put_contents($compiled, Blade::compile($this->path)); } return $compiled; }
/** * Test the compilation of section statements. * * @group laravel */ public function testSectionsAreCompiledCorrectly() { $blade = "@section('something')\nfoo\n@endsection"; $this->assertEquals("<?php \\Laravel\\Section::start('something'); ?>\nfoo\n<?php \\Laravel\\Section::stop(); ?>", Blade::compile_string($blade)); }
public static function sharpen() { Event::listen(View::engine, function ($view) { if (!str_contains($view->path, BLADE_EXT)) { return; } $compiled = Blade::compiled($view->path); if (!file_exists($compiled) or Blade::expired($view->view, $view->path)) { file_put_contents($compiled, Blade::compile($view)); } $view->path = $compiled; return ltrim($view->get()); }); }