Skip to content

Commit

Permalink
Use built in method to render a string with Blade (librenms#13688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jellyfrog authored Jan 26, 2022
1 parent 7734113 commit 1debe5e
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 82 deletions.
9 changes: 5 additions & 4 deletions LibreNMS/Alert/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace LibreNMS\Alert;

use App\Models\AlertTemplate;
use Illuminate\Support\Facades\Blade;
use LibreNMS\Enum\AlertState;

class Template
Expand Down Expand Up @@ -74,9 +75,9 @@ public function bladeBody($data)
{
$alert['alert'] = new AlertData($data['alert']);
try {
return view(['template' => $data['template']->template], $alert)->__toString();
return Blade::render($data['template']->template, $alert);
} catch (\Exception $e) {
return view(['template' => $this->getDefaultTemplate()], $alert)->__toString();
return Blade::render($this->getDefaultTemplate(), $alert);
}
}

Expand All @@ -90,9 +91,9 @@ public function bladeTitle($data)
{
$alert['alert'] = new AlertData($data['alert']);
try {
return view(['template' => $data['title']], $alert)->__toString();
return Blade::render($data['title'], $alert);
} catch (\Exception $e) {
return $data['title'] ?: view(['template' => 'Template ' . $data['name']], $alert)->__toString();
return $data['title'] ?: Blade::render('Template ' . $data['name'], $alert);
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use LibreNMS\Config;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Graph;
Expand Down Expand Up @@ -166,7 +167,7 @@ private function deviceLinkMenu(Device $device)
foreach (array_values(Arr::wrap(Config::get('html.device.links'))) as $index => $link) {
$device_links['custom' . ($index + 1)] = [
'icon' => $link['icon'] ?? 'fa-external-link',
'url' => view(['template' => $link['url']], ['device' => $device])->__toString(),
'url' => Blade::render($link['url'], ['device' => $device]),
'title' => $link['title'],
'external' => $link['external'] ?? true,
];
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Table/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use LibreNMS\Config;
use LibreNMS\Util\Rewrite;
use LibreNMS\Util\Time;
Expand Down Expand Up @@ -341,7 +342,7 @@ private function getActions(Device $device): array
foreach (array_values(Arr::wrap(Config::get('html.device.links'))) as $index => $custom) {
if ($custom['action'] ?? false) {
$row = $this->isDetailed() ? $index % 2 : 0;
$custom['href'] = view(['template' => $custom['url']], ['device' => $device])->__toString(); // @phpstan-ignore-line
$custom['href'] = Blade::render($custom['url'], ['device' => $device]);
$actions[$row][] = $custom;
}
}
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"rmccue/requests": "^1.7",
"symfony/yaml": "^4.0",
"tecnickcom/tcpdf": "^6.4",
"tightenco/ziggy": "^0.9",
"wpb/string-blade-compiler": "^6.0"
"tightenco/ziggy": "^0.9"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
Expand Down
60 changes: 2 additions & 58 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion includes/html/forms/alert-templates.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$create = true;

$name = $vars['name'];
if (isset($vars['template']) && empty(view(['template' => $vars['template']], [])->__toString())) {
if (isset($vars['template']) && empty(Blade::render($vars['template']))) {
$message = 'Template failed to be parsed, please check the syntax';
} elseif (! empty($name)) {
if ($vars['template'] && is_numeric($vars['template_id'])) {
Expand Down
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@ parameters:
count: 1
path: LibreNMS/Alert/Template.php

-
message: "#^Parameter \\#1 \\$view of function view expects view\\-string\\|null, array\\<string, mixed\\> given\\.$#"
count: 2
path: LibreNMS/Alert/Template.php

-
message: "#^Parameter \\#1 \\$view of function view expects view\\-string\\|null, array\\<string, string\\> given\\.$#"
count: 2
path: LibreNMS/Alert/Template.php

-
message: "#^Property LibreNMS\\\\Alert\\\\Template\\:\\:\\$template has no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -7810,11 +7800,6 @@ parameters:
count: 1
path: app/Http/Controllers/DeviceController.php

-
message: "#^Parameter \\#1 \\$view of function view expects view\\-string\\|null, array\\<string, mixed\\> given\\.$#"
count: 1
path: app/Http/Controllers/DeviceController.php

-
message: "#^Property App\\\\Http\\\\Controllers\\\\DeviceController\\:\\:\\$tabs has no type specified\\.$#"
count: 1
Expand Down

0 comments on commit 1debe5e

Please sign in to comment.