Created
January 5, 2019 15:43
-
-
Save adelf/744203649419492b5b50ed5b8bff4c07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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