2019-11-20 02:45:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Templating questions relative class
|
2020-01-17 18:19:25 +01:00
|
|
|
* Not a standard controller as it's not linked to a model in any way.
|
2019-11-20 02:45:00 +01:00
|
|
|
*/
|
|
|
|
class Templating
|
|
|
|
{
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Twig environment.
|
2019-11-20 02:45:00 +01:00
|
|
|
*/
|
|
|
|
private $sandbox;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
public function __construct()
|
2019-11-20 02:45:00 +01:00
|
|
|
{
|
|
|
|
$tags = [
|
|
|
|
'if',
|
|
|
|
'for',
|
|
|
|
'apply',
|
2020-06-04 12:54:31 +02:00
|
|
|
'set',
|
2019-11-20 02:45:00 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$filters = [
|
|
|
|
'abs', 'capitalize', 'country_name', 'currency_name',
|
2020-04-03 21:01:18 +02:00
|
|
|
'currency_symbol', 'date', 'date_modify', 'default', 'escape',
|
2019-11-20 02:45:00 +01:00
|
|
|
'first', 'format', 'format_currency', 'format_datetime',
|
2020-04-03 21:01:18 +02:00
|
|
|
'format_number', 'join', 'json_encode', 'keys', 'language_name',
|
2019-11-20 02:45:00 +01:00
|
|
|
'last', 'length', 'locale_name', 'lower', 'number_format',
|
|
|
|
'replace', 'reverse', 'round', 'slice',
|
|
|
|
'sort', 'spaceless', 'split', 'timezone_name',
|
|
|
|
'title', 'trim', 'upper', 'url_encode',
|
|
|
|
];
|
|
|
|
|
|
|
|
$methods = [];
|
|
|
|
$properties = [];
|
|
|
|
$functions = [
|
|
|
|
'date', 'max', 'min', 'random',
|
2020-01-17 18:19:25 +01:00
|
|
|
'range',
|
2019-11-20 02:45:00 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$policy = new \Twig\Sandbox\SecurityPolicy($tags, $filters, $methods, $properties, $functions);
|
|
|
|
$this->sandbox = new \Twig\Extension\SandboxExtension($policy, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Render a string as a twig template.
|
|
|
|
*
|
2019-11-20 02:45:00 +01:00
|
|
|
* @param string $template : Template string
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param array $data : Data to pass to the template
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-20 02:45:00 +01:00
|
|
|
* @return array : keys, success, error, result
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function render(string $template, array $data = [])
|
2019-11-20 02:45:00 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
try
|
2019-11-20 02:45:00 +01:00
|
|
|
{
|
|
|
|
$loader = new \Twig\Loader\ArrayLoader([
|
|
|
|
'template' => $template,
|
|
|
|
]);
|
|
|
|
|
2020-04-03 21:01:18 +02:00
|
|
|
$twig = new \Twig\Environment($loader, [
|
|
|
|
'debug' => false,
|
|
|
|
'charset' => 'utf-8',
|
|
|
|
'cache' => false,
|
|
|
|
'auto_reload' => false,
|
|
|
|
'strict_variables' => false,
|
|
|
|
'autoescape' => false,
|
|
|
|
'optimizations' => -1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$twig->addExtension($this->sandbox);
|
2021-01-17 03:16:57 +01:00
|
|
|
$result = $twig->render('template', $data);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
'result' => $result,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'result' => $e->getMessage(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|