Skip to content

Instantly share code, notes, and snippets.

@adelf
Created January 5, 2019 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelf/744203649419492b5b50ed5b8bff4c07 to your computer and use it in GitHub Desktop.
Save adelf/744203649419492b5b50ed5b8bff4c07 to your computer and use it in GitHub Desktop.
<?php
namespace App\Services;
use App\Contracts\IConferenceService;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CustomViewCompiler
{
public function getBladeResult($name, $bladeContents, array $data = [], $flushCache = false)
{
$viewPath = storage_path('custom_views/' . $name . '.php');
if($flushCache || !file_exists($viewPath))
{
$dirname = dirname($viewPath);
if(!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
file_put_contents($viewPath, \Blade::compileString($bladeContents));
}
$data['currentEvent'] = $this->event;
return $this->getIncludeResult($viewPath, $data);
}
private function getIncludeResult($file, array $data)
{
$obLevel = ob_get_level();
ob_start();
try {
extract($data, EXTR_SKIP);
include $file;
}
catch (\Exception $e)
{
while (ob_get_level() > $obLevel)
{
ob_end_clean();
}
throw $e;
}
catch (\Throwable $e)
{
while (ob_get_level() > $obLevel)
{
ob_end_clean();
}
throw new FatalThrowableError($e);
}
return ob_get_clean();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment