I just made it for Laravel 4.2 as Linux-only version (which uses shm to create temporary files). It should work in another OS but through usual temporary files (a bit slower).
<?php
class FilesDeletionQueue {
protected $files = [];
public function add($filename) {
$this->files[] = $filename;
}
public function flush() {
$result = true;
foreach ($this->files as $filename) {
if (@unlink($filename) === false) {
$result = false;
}
}
return $result;
}
}
class Helpers {
static public function viewFromStr($tplName, $pageContent, array $data) {
global $app;
// Try to create temporary blade template file in shm memory,
// if fails, create as usual temporary file.
$tempname = tempnam('/run/shm/', 'laravel_blade');
if (@file_put_contents($tempname, $pageContent) === false) {
$tempname = tempnam(storage_path(), 'laravel_blade');
if (@file_put_contents($tempname, $pageContent) === false) {
throw new \Exception("Cannot create {$tempname} in " . __METHOD__);
}
}
// Create template from shm memory file.
$resolver = $app['view.engine.resolver'];
$finder = $app['view.finder'];
$env = new \Illuminate\View\Factory($resolver, $finder, $app['events']);
$env->setContainer($app);
$pageView = new \Illuminate\View\View(
$env,
$env->getEngineResolver()->resolve('blade'),
$tplName,
$tempname,
$data
);
$env->callCreator($pageView);
try {
$fdq = App::make('files_deletion_queue');
} catch (\Exception $e) {
App::singleton('files_deletion_queue', function() {
return new FilesDeletionQueue();
});
$fdq = App::make('files_deletion_queue');
App::shutdown(function() {
App::make('files_deletion_queue')->flush();
});
}
// Add tempname to list of files to be deleted when application ends.
$fdq->add($tempname);
return $pageView;
}
}
Then you can use it like this:
$pageView = Helpers::viewFromStr('content', $pageContent, $this->data);
$this->layout->with('content', $pageView)
->with('menus', $this->menus )
->with('page',$this->data);
instead of:
/*
$this->layout->nest('content',"pages.template.{$row->filename}",$this->data)
->with('menus', $this->menus )
->with('page',$this->data);
*
*/
It's really sad that recommended methods cannot load blade files from arbitrary OS pathes and there's no ready out of box support for compiling from RAM (I heard it's much easier for twig).