Skip to content

Commit

Permalink
Refactor blade string compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Mar 8, 2020
1 parent 4fff6bd commit 5053977
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions src/Services/Blocks/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@

class BladeCompiler
{
public static function render($string, $data)
/**
* @param $data
* @return mixed
*/
protected static function absorbApplicationEnvironment($data)
{
$php = Blade::compileString($string);

$data['__env'] = app(Factory::class);

$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
return $data;
}

/**
* @param string $php
* @param array $data
* @throws \Symfony\Component\Debug\Exception\FatalThrowableError
*/
protected static function compile(string $php, array $data)
{
$obLevel = self::initializeOutputBuffering();

try {
extract(self::absorbApplicationEnvironment($data), EXTR_SKIP);

eval('?' . '>' . $php);
} catch (Exception $e) {
while (ob_get_level() > $obLevel) {
Expand All @@ -33,9 +45,38 @@ public static function render($string, $data)
}
throw new FatalThrowableError($e);
}
}

$compiled = ob_get_clean();
/**
* @return false|string
*/
protected static function getRendered()
{
return ob_get_clean();
}

/**
* @return int
*/
protected static function initializeOutputBuffering()
{
$obLevel = ob_get_level();

ob_start();

return $obLevel;
}

/**
* @param $string
* @param $data
* @return false|string
* @throws \Symfony\Component\Debug\Exception\FatalThrowableError
*/
public static function render($string, $data)
{
self::compile(Blade::compileString($string), $data);

return $compiled;
return self::getRendered();
}
}

0 comments on commit 5053977

Please sign in to comment.