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

Refactor Twig registration and usage #455

Merged
merged 14 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions modules/cms/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lang;
use File;
use Event;
use Config;
use Backend;
use BackendMenu;
use BackendAuth;
Expand All @@ -17,7 +18,12 @@
use System\Classes\CombineAssets;
use Cms\Classes\Theme as CmsTheme;
use Backend\Classes\WidgetManager;
use System\Classes\MarkupManager;
use System\Classes\SettingsManager;
use Twig\Cache\FilesystemCache as TwigCacheFilesystem;
use Cms\Twig\Loader as CmsTwigLoader;
use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;

use Winter\Storm\Support\ModuleServiceProvider;

Expand All @@ -31,6 +37,7 @@ class ServiceProvider extends ModuleServiceProvider
public function register()
{
$this->registerConsole();
$this->registerTwigParser();
$this->registerAssetBundles();
$this->registerComponents();
$this->registerThemeLogging();
Expand Down Expand Up @@ -78,6 +85,43 @@ protected function registerConsole()
$this->registerConsoleCommand('theme.sync', \Cms\Console\ThemeSync::class);
}

/*
* Register Twig Environments and other Twig modifications provided by the module
*/
protected function registerTwigParser()
{
// Register CMS Twig environment
App::singleton('twig.environment.cms', function ($app) {
// Load Twig options
$useCache = !Config::get('cms.twigNoCache');
$isDebugMode = Config::get('app.debug', false);
$strictVariables = Config::get('cms.enableTwigStrictVariables', false);
$strictVariables = $strictVariables ?? $isDebugMode;
$forceBytecode = Config::get('cms.forceBytecodeInvalidation', false);

$options = [
'auto_reload' => true,
'debug' => $isDebugMode,
'strict_variables' => $strictVariables,
];

if ($useCache) {
$options['cache'] = new TwigCacheFilesystem(
storage_path().'/cms/twig',
$forceBytecode ? TwigCacheFilesystem::FORCE_BYTECODE_INVALIDATION : 0
);
}

$twig = MarkupManager::makeBaseTwigEnvironment(new CmsTwigLoader, $options);
$twig->addExtension(new CmsTwigExtension);
if ($isDebugMode) {
$twig->addExtension(new DebugExtension);
}

return $twig;
});
}

/**
* Register asset bundles
*/
Expand Down
12 changes: 2 additions & 10 deletions modules/cms/classes/CmsCompoundObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
use Lang;
use Cache;
use Config;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Components\ViewBag;
use Cms\Helpers\Cms as CmsHelpers;
use System\Twig\Extension as SystemTwigExtension;
use Winter\Storm\Halcyon\Processors\SectionParser;
use Twig\Source as TwigSource;
use Twig\Environment as TwigEnvironment;
use ApplicationException;

/**
Expand Down Expand Up @@ -107,7 +103,7 @@ public function beforeSave()
$this->code = $this->getOriginal('code');
}
}

$this->checkSafeMode();
}

Expand Down Expand Up @@ -414,11 +410,7 @@ public function getTwigContent()
*/
public function getTwigNodeTree($markup = false)
{
$loader = new TwigLoader();
$twig = new TwigEnvironment($loader, []);
$twig->addExtension(new CmsTwigExtension());
$twig->addExtension(new SystemTwigExtension);

$twig = App::make('twig.environment.cms');
$stream = $twig->tokenize(new TwigSource($markup === false ? $this->markup : $markup, 'getTwigNodeTree'));
return $twig->parse($stream);
}
Expand Down
56 changes: 8 additions & 48 deletions modules/cms/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@
use SystemException;
use BackendAuth;
use Twig\Environment as TwigEnvironment;
use Twig\Cache\FilesystemCache as TwigCacheFilesystem;
use Twig\Extension\SandboxExtension;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Models\MaintenanceSetting;
use System\Models\RequestLog;
use System\Helpers\View as ViewHelper;
use System\Classes\CombineAssets;
use System\Twig\Extension as SystemTwigExtension;
use System\Twig\SecurityPolicy;
use Winter\Storm\Exception\AjaxException;
use Winter\Storm\Exception\ValidationException;
use Winter\Storm\Parse\Bracket as TextParser;
Expand Down Expand Up @@ -54,11 +47,6 @@ class Controller
*/
protected $router;

/**
* @var \Cms\Twig\Loader A reference to the Twig template loader.
*/
protected $loader;

/**
* @var \Cms\Classes\Page A reference to the CMS page template being processed.
*/
Expand Down Expand Up @@ -429,8 +417,8 @@ public function runPage($page, $useAjax = true)
* Render the page
*/
CmsException::mask($this->page, 400);
$this->loader->setObject($this->page);
$template = $this->twig->loadTemplate($this->page->getFilePath());
$this->getLoader()->setObject($this->page);
$template = $this->getTwig()->load($this->page->getFilePath());
$this->pageContents = $template->render($this->vars);
CmsException::unmask();
}
Expand All @@ -439,8 +427,8 @@ public function runPage($page, $useAjax = true)
* Render the layout
*/
CmsException::mask($this->layout, 400);
$this->loader->setObject($this->layout);
$template = $this->twig->loadTemplate($this->layout->getFilePath());
$this->getLoader()->setObject($this->layout);
$template = $this->getTwig()->load($this->layout->getFilePath());
$result = $template->render($this->vars);
CmsException::unmask();

Expand Down Expand Up @@ -595,35 +583,7 @@ protected function postProcessResult($page, $url, $content)
*/
protected function initTwigEnvironment()
{
$this->loader = new TwigLoader;

$useCache = !Config::get('cms.twigNoCache');
$isDebugMode = Config::get('app.debug', false);
$strictVariables = Config::get('cms.enableTwigStrictVariables', false);
$strictVariables = $strictVariables ?? $isDebugMode;
$forceBytecode = Config::get('cms.forceBytecodeInvalidation', false);

$options = [
'auto_reload' => true,
'debug' => $isDebugMode,
'strict_variables' => $strictVariables,
];

if ($useCache) {
$options['cache'] = new TwigCacheFilesystem(
storage_path().'/cms/twig',
$forceBytecode ? TwigCacheFilesystem::FORCE_BYTECODE_INVALIDATION : 0
);
}

$this->twig = new TwigEnvironment($this->loader, $options);
$this->twig->addExtension(new CmsTwigExtension($this));
$this->twig->addExtension(new SystemTwigExtension);
$this->twig->addExtension(new SandboxExtension(new SecurityPolicy, true));

if ($isDebugMode) {
$this->twig->addExtension(new DebugExtension($this));
}
$this->twig = App::make('twig.environment.cms');
}

/**
Expand Down Expand Up @@ -1096,8 +1056,8 @@ public function renderPartial($name, $parameters = [], $throwException = true)
* Render the partial
*/
CmsException::mask($partial, 400);
$this->loader->setObject($partial);
$template = $this->twig->loadTemplate($partial->getFilePath());
$this->getLoader()->setObject($partial);
$template = $this->getTwig()->load($partial->getFilePath());
$partialContent = $template->render(array_merge($this->vars, $parameters));
CmsException::unmask();

Expand Down Expand Up @@ -1274,7 +1234,7 @@ public function getTwig()
*/
public function getLoader()
{
return $this->loader;
return $this->getTwig()->getLoader();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->componentFunction(")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->componentFunction(\$context,")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_component_params']")
->write(");\n")
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/twig/ContentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->contentFunction(")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->contentFunction(\$context,")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_content_params']")
->write(");\n")
Expand Down
15 changes: 0 additions & 15 deletions modules/cms/twig/DebugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Twig\Extension\AbstractExtension as TwigExtension;
use Twig\Environment as TwigEnvironment;
use Twig\TwigFunction as TwigSimpleFunction;
use Cms\Classes\Controller;
use Cms\Classes\ComponentBase;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
Expand All @@ -19,11 +18,6 @@ class DebugExtension extends TwigExtension
const OBJECT_CAPTION = 'Object variables';
const COMPONENT_CAPTION = 'Component variables';

/**
* @var \Cms\Classes\Controller A reference to the CMS controller.
*/
protected $controller;

/**
* @var integer Helper for rendering table row styles.
*/
Expand Down Expand Up @@ -52,15 +46,6 @@ class DebugExtension extends TwigExtension
'offsetUnset'
];

/**
* Creates the extension instance.
* @param \Cms\Classes\Controller $controller The CMS controller object.
*/
public function __construct(Controller $controller)
{
$this->controller = $controller;
}

/**
* Returns a list of global functions to add to the existing list.
* @return array An array of global functions
Expand Down
Loading