1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Utils/Traits/MakesInvoiceHtml.php

88 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\Traits;
use Illuminate\Support\Facades\Blade;
use Symfony\Component\Debug\Exception\FatalThrowableError;
/**
* Class MakesInvoiceHtml.
*/
trait MakesInvoiceHtml
{
/**
* Generate the HTML invoice parsing variables
* and generating the final invoice HTML
*
* @param string $design either the path to the design template, OR the full design template string
* @param Collection $invoice The invoice object
*
* @return string The invoice string in HTML format
*/
public function generateInvoiceHtml($design, $invoice) :string
{
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
$design = str_replace(array_keys($variables), array_values($variables), $design);
$data['invoice'] = $invoice;
return $this->renderView($design, $data);
//return view($design, $data)->render();
}
/**
* Parses the blade file string and processes the template variables
*
* @param string $string The Blade file string
* @param array $data The array of template variables
* @return string The return HTML string
*
*/
public function renderView($string, $data) :string
{
if (!$data) {
$data = [];
}
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} 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();
}
}