Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Smarty and Twig template engines #3452

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -30,7 +30,8 @@
"robmorgan/phinx": "^0.13.4",
"symfony/http-foundation": "^5.4.17",
"joypixels/emoji-toolkit": "^7.0",
"php-di/php-di": "^6.4"
"php-di/php-di": "^6.4",
"twig/twig": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "1.6.9",
Expand Down
10 changes: 5 additions & 5 deletions core/classes/Core/Module.php
Expand Up @@ -4,7 +4,7 @@
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr13
* @version 2.2.0
* @license MIT
*/
abstract class Module {
Expand Down Expand Up @@ -51,12 +51,12 @@ public function __construct(
* @param User $user User viewing the page.
* @param Pages $pages Instance of pages class.
* @param Cache $cache Instance of cache to pass.
* @param Smarty $smarty Instance of smarty to pass.
* @param FakeSmarty|Smarty|null $smarty Instance of Smarty to pass
* @param Navigation[] $navs Array of loaded navigation menus.
* @param Widgets $widgets Instance of widget class to pass.
* @param TemplateBase $template Template to pass.
*/
public static function loadPage(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, TemplateBase $template): void {
public static function loadPage(User $user, Pages $pages, Cache $cache, $smarty, iterable $navs, Widgets $widgets, TemplateBase $template): void {
foreach (self::getModules() as $module) {
$module->onPageLoad($user, $pages, $cache, $smarty, $navs, $widgets, $template);
}
Expand All @@ -74,12 +74,12 @@ public static function getModules(): iterable {
* @param User $user User viewing the page.
* @param Pages $pages Instance of pages class.
* @param Cache $cache Instance of cache to pass.
* @param Smarty $smarty Instance of smarty to pass.
* @param FakeSmarty|Smarty $smarty Instance of smarty to pass, to be removed in 2.3.0
* @param Navigation[] $navs Array of loaded navigation menus.
* @param Widgets $widgets Instance of widget class to pass.
* @param TemplateBase|null $template Active template to render.
*/
abstract public function onPageLoad(User $user, Pages $pages, Cache $cache, Smarty $smarty, iterable $navs, Widgets $widgets, ?TemplateBase $template);
abstract public function onPageLoad(User $user, Pages $pages, Cache $cache, $smarty, iterable $navs, Widgets $widgets, ?TemplateBase $template);

/**
* Determine loading arrangement of modules.
Expand Down
2 changes: 2 additions & 0 deletions core/classes/Database/PhinxAdapter.php
Expand Up @@ -19,7 +19,9 @@
?string $migrationDir = null,
bool $returnResults = false
) {
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary as I use the same database as another WIP branch


$module = strtolower($module);

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.0)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 7.4)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.1)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.0)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.2)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.1)

Unreachable statement - code above always terminates.

Check failure on line 24 in core/classes/Database/PhinxAdapter.php

View workflow job for this annotation

GitHub Actions / phpstan (ubuntu-latest, 8.2)

Unreachable statement - code above always terminates.

if ($module === 'core') {
$table = 'nl2_phinxlog';
Expand Down
28 changes: 22 additions & 6 deletions core/classes/Misc/DebugBarHelper.php
@@ -1,20 +1,25 @@
<?php

use DebugBar\Bridge\NamespacedTwigProfileCollector;
use DebugBar\DataCollector\ConfigCollector;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DebugBar;
use DebugBar\DataCollector\PDO\PDOCollector;
use Junker\DebugBar\Bridge\SmartyCollector;
use Twig\Environment;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;

/**
* Class to help integrate the PHPDebugBar with NamelessMC.
*
* @package NamelessMC\Misc
* @author Aberdeener
* @version 2.0.0-pr13
* @version 2.2.0
* @license MIT
*/
class DebugBarHelper extends Instanceable {
Expand All @@ -24,7 +29,7 @@ class DebugBarHelper extends Instanceable {
/**
* Enable the PHPDebugBar
*/
public function enable(Smarty $smarty): void {
public function enable(): void {
$debugbar = new DebugBar();

$debugbar->addCollector(new TimeDataCollector());
Expand All @@ -46,16 +51,27 @@ public function enable(Smarty $smarty): void {
$pdoCollector->setRenderSqlWithParams(true, '`');
$debugbar->addCollector($pdoCollector);

$smartyCollector = new SmartyCollector($smarty);
$smartyCollector->useHtmlVarDumper();
$debugbar->addCollector($smartyCollector);

$debugbar->addCollector(new PhpInfoCollector());
$debugbar->addCollector(new MemoryCollector());

$this->_debugBar = $debugbar;
}

public function addCollector(DataCollector $collector): void {
$this->_debugBar->addCollector($collector);
}

public function addSmartyCollector(Smarty $smarty): void {
$smartyCollector = new SmartyCollector($smarty);
$smartyCollector->useHtmlVarDumper();
$this->addCollector($smartyCollector);
}

public function addTwigCollector(Environment $twig, Profile $profile): void {
$twig->addExtension(new ProfilerExtension($profile));
$this->addCollector(new NamespacedTwigProfileCollector($profile));
}

public function getDebugBar(): ?DebugBar {
return $this->_debugBar;
}
Expand Down
28 changes: 28 additions & 0 deletions core/classes/Templates/FakeSmarty.php
@@ -0,0 +1,28 @@
<?php
/**
* Fake Smarty class to help with migration to 2.2.0 template system
* It aims to be wrapper around TemplateBase to ensure $smarty->assign still works until 2.3.0, when this will be removed
*
* @author Samerton
* @license MIT
* @version 2.2.0
* @deprecated
*/

class FakeSmarty {
private TemplateEngine $_engine;

public function __construct(TemplateEngine $engine) {
$this->_engine = $engine;
}

public function assign($key, $value = null) {
if (is_string($key)) {
$this->_engine->addVariable($key, $value);
}

if (is_array($key)) {
$this->_engine->addVariables($key);
}
}
}
25 changes: 25 additions & 0 deletions core/classes/Templates/SmartyTemplateBase.php
@@ -0,0 +1,25 @@
<?php
/**
* Base class which templates should extend to add functionality
* Uses Smarty template engine
*
* @package NamelessMC\Templates
* @author Samerton
* @version 2.2.0
* @license MIT
*/
abstract class SmartyTemplateBase extends TemplateBase {

/**
* @param string $name
* @param string $version
* @param string $nameless_version
* @param string $author
* @param bool $panelTemplate
*/
public function __construct(string $name, string $version, string $nameless_version, string $author, bool $panelTemplate = false) {
parent::__construct($name, $version, $nameless_version, $author);

$this->_engine = new SmartyTemplateEngine($name, $panelTemplate);
}
}
81 changes: 81 additions & 0 deletions core/classes/Templates/SmartyTemplateEngine.php
@@ -0,0 +1,81 @@
<?php
/**
* Smarty template engine
*
* @author Samerton
* @license MIT
* @version 2.2.0
*/

class SmartyTemplateEngine extends TemplateEngine {
private Smarty $_smarty;

/**
* @param string $template Template name to load
* @param bool $panelTemplate Whether this is a panel template or not
*/
public function __construct(string $template, bool $panelTemplate = false) {
$smarty = new Smarty();

$securityPolicy = new Smarty_Security($smarty);
$securityPolicy->php_modifiers = [
'escape',
'count',
'key',
'round',
'ucfirst',
'defined',
'date',
'explode',
'implode',
'strtolower',
'strtoupper'
];
$securityPolicy->php_functions = [
'isset',
'empty',
'count',
'sizeof',
'in_array',
'is_array',
'time',
'nl2br',
'is_numeric',
'file_exists',
'array_key_exists'
];
$securityPolicy->secure_dir = [ROOT_PATH . '/custom/templates', ROOT_PATH . '/custom/panel_templates'];
$smarty->enableSecurity($securityPolicy);

$smarty->setCompileDir(ROOT_PATH . '/cache/templates_c');

if ($panelTemplate) {
$smarty->setTemplateDir(ROOT_PATH . '/custom/panel_templates/' . $template);
} else {
$smarty->setTemplateDir(ROOT_PATH . '/custom/templates/' . $template);
}

if (defined('PHPDEBUGBAR')) {
DebugBarHelper::getInstance()->addSmartyCollector($smarty);
}

$this->_smarty = $smarty;

parent::__construct();
}

public function render(string $templateFile): void {
echo $this->fetch($templateFile);
}

public function fetch(string $templateFile): string {
$templateFile = str_replace('.tpl', '', $templateFile);

$this->_smarty->assign($this->getVariables());
return $this->_smarty->fetch("$templateFile.tpl");
}

public function clearCache(): void {
$this->_smarty->clearAllCache();
}
}
40 changes: 30 additions & 10 deletions core/classes/Templates/TemplateBase.php
Expand Up @@ -46,6 +46,9 @@ abstract class TemplateBase {
*/
protected array $_js = [];

/** @var TemplateEngine Template engine instance */
protected TemplateEngine $_engine;

public function __construct(string $name, string $version, string $nameless_version, string $author) {
$this->_name = $name;
$this->_version = $version;
Expand Down Expand Up @@ -170,29 +173,31 @@ public function getSettings(): string {
}

/**
* Render this template with Smarty engine.
* Render this template.
*
* @param string $template Template file to render, relative to template base directory
*/
public function displayTemplate(string $template, Smarty $smarty): void {
public function displayTemplate(string $template): void {
[$css, $js] = $this->assets()->compile();

// Put the assets at the start of the arrays, so they load first (SBAdmin requires JQuery first, etc.)
array_unshift($this->_css, ...$css);
array_unshift($this->_js, ...$js);

$smarty->assign([
$this->_engine->addVariables([
'TEMPLATE_CSS' => $this->getCSS(),
'TEMPLATE_JS' => $this->getJS()
'TEMPLATE_JS' => $this->getJS(),
]);

if (defined('PHPDEBUGBAR') && PHPDEBUGBAR) {
$debugBar = DebugBarHelper::getInstance()->getDebugBar()->getJavascriptRenderer();
$smarty->assign([
$this->_engine->addVariables([
'DEBUGBAR_JS' => $debugBar->renderHead(),
'DEBUGBAR_HTML' => $debugBar->render()
'DEBUGBAR_HTML' => $debugBar->render(),
]);
}

$smarty->display($template);
$this->_engine->render($template);
}

/**
Expand All @@ -213,12 +218,27 @@ public function getJS(): array {
return $this->_js;
}

public function getTemplate(string $template, Smarty $smarty): string {
$smarty->assign([
/**
* Fetches template HTML instead of rendering it
*
* @param string $template
* @return string Generated HTML
*/
public function getTemplate(string $template): string {
$this->_engine->addVariables([
'TEMPLATE_CSS' => $this->getCSS(),
'TEMPLATE_JS' => $this->getJS()
]);

return $smarty->fetch($template);
return $this->_engine->fetch($template);
}

/**
* Get template engine
*
* @return TemplateEngine
*/
public function getEngine(): TemplateEngine {
return $this->_engine;
}
}