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

Cms module refactor #3575

Closed
wants to merge 8 commits into from
Closed
Changes from 4 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
51 changes: 51 additions & 0 deletions modules/cms/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
<?php namespace Cms;

use App;
use Config;
use Event;
use Backend;
use BackendMenu;
use BackendAuth;
use Twig_Environment;
use Twig_Cache_Filesystem;
use Backend\Models\UserRole;
use Backend\Classes\WidgetManager;
use October\Rain\Support\ModuleServiceProvider;
use System\Classes\SettingsManager;
use System\Twig\Extension as SystemTwigExtension;
use Cms\Classes\ComponentManager;
use Cms\Classes\Page as CmsPage;
use Cms\Classes\CmsObject;
use Cms\Models\ThemeData;
use Cms\Models\ThemeLog;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;

class ServiceProvider extends ModuleServiceProvider
{
@@ -26,6 +33,7 @@ public function register()
{
parent::register('cms');

$this->registerTwig();
$this->registerComponents();
$this->registerThemeLogging();
$this->registerCombinerEvents();
@@ -55,6 +63,49 @@ public function boot()
$this->bootRichEditorEvents();
}

/**
* Register Twig templating engine.
*/
protected function registerTwig()
{
App::singleton('cms.twig.loader', function () {
return new TwigLoader;
});

App::singleton('cms.twig.environment', function ($app) {
$loader = $app->make('cms.twig.loader');

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

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

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

$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new CmsTwigExtension);
$twig->addExtension(new SystemTwigExtension);

if ($isDebugMode) {
$twig->addExtension(new DebugExtension);
}

return $twig;
});
}

/**
* Register components.
*/
11 changes: 2 additions & 9 deletions modules/cms/classes/CmsCompoundObject.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php namespace Cms\Classes;

use App;
use Ini;
use Lang;
use Cache;
use Config;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Components\ViewBag;
use System\Twig\Extension as SystemTwigExtension;
use October\Rain\Halcyon\Processors\SectionParser;
use Twig_Source;
use Twig_Environment;
use ApplicationException;

/**
@@ -409,11 +406,7 @@ public function getTwigContent()
*/
public function getTwigNodeTree($markup = false)
{
$loader = new TwigLoader();
$twig = new Twig_Environment($loader, []);
$twig->addExtension(new CmsTwigExtension());
$twig->addExtension(new SystemTwigExtension);

$twig = App::make('cms.twig.environment');
$stream = $twig->tokenize(new Twig_Source($markup === false ? $this->markup : $markup, 'getTwigNodeTree'));
return $twig->parse($stream);
}
30 changes: 2 additions & 28 deletions modules/cms/classes/Controller.php
Original file line number Diff line number Diff line change
@@ -585,34 +585,8 @@ 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 Twig_Cache_Filesystem(
storage_path().'/cms/twig',
$forceBytecode ? Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION : 0
);
}

$this->twig = new Twig_Environment($this->loader, $options);
$this->twig->addExtension(new CmsTwigExtension($this));
$this->twig->addExtension(new SystemTwigExtension);

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

/**
2 changes: 1 addition & 1 deletion modules/cms/twig/ComponentNode.php
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public function compile(Twig_Compiler $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")
2 changes: 1 addition & 1 deletion modules/cms/twig/ContentNode.php
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ public function compile(Twig_Compiler $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")
14 changes: 0 additions & 14 deletions modules/cms/twig/DebugExtension.php
Original file line number Diff line number Diff line change
@@ -19,11 +19,6 @@ class DebugExtension extends Twig_Extension
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.
*/
@@ -52,15 +47,6 @@ class DebugExtension extends Twig_Extension
'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
54 changes: 20 additions & 34 deletions modules/cms/twig/Extension.php
Original file line number Diff line number Diff line change
@@ -15,20 +15,6 @@
*/
class Extension extends Twig_Extension
{
/**
* @var \Cms\Classes\Controller A reference to the CMS controller.
*/
protected $controller;

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

/**
* Returns a list of functions to add to the existing list.
*
@@ -37,10 +23,10 @@ public function __construct(Controller $controller = null)
public function getFunctions()
{
return [
new Twig_SimpleFunction('page', [$this, 'pageFunction'], ['is_safe' => ['html']]),
new Twig_SimpleFunction('partial', [$this, 'partialFunction'], ['is_safe' => ['html']]),
new Twig_SimpleFunction('content', [$this, 'contentFunction'], ['is_safe' => ['html']]),
new Twig_SimpleFunction('component', [$this, 'componentFunction'], ['is_safe' => ['html']]),
new Twig_SimpleFunction('page', [$this, 'pageFunction'], ['is_safe' => ['html'], 'needs_context' => true]),
new Twig_SimpleFunction('partial', [$this, 'partialFunction'], ['is_safe' => ['html'], 'needs_context' => true]),
new Twig_SimpleFunction('content', [$this, 'contentFunction'], ['is_safe' => ['html'], 'needs_context' => true]),
new Twig_SimpleFunction('component', [$this, 'componentFunction'], ['is_safe' => ['html'], 'needs_context' => true]),
new Twig_SimpleFunction('placeholder', [$this, 'placeholderFunction'], ['is_safe' => ['html']]),
];
}
@@ -53,8 +39,8 @@ public function getFunctions()
public function getFilters()
{
return [
new Twig_SimpleFilter('page', [$this, 'pageFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('theme', [$this, 'themeFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('page', [$this, 'pageFilter'], ['is_safe' => ['html'], 'needs_context' => true]),
new Twig_SimpleFilter('theme', [$this, 'themeFilter'], ['is_safe' => ['html'], 'needs_context' => true]),
];
}

@@ -85,9 +71,9 @@ public function getTokenParsers()
* This function should be used in the layout code to output the requested page.
* @return string Returns the page contents.
*/
public function pageFunction()
public function pageFunction($context)
{
return $this->controller->renderPage();
return $context['this']['controller']->renderPage();
}

/**
@@ -97,9 +83,9 @@ public function pageFunction()
* @param bool $throwException Throw an exception if the partial is not found.
* @return string Returns the partial contents.
*/
public function partialFunction($name, $parameters = [], $throwException = false)
public function partialFunction($context, $name, $parameters = [], $throwException = false)
{
return $this->controller->renderPartial($name, $parameters, $throwException);
return $context['this']['controller']->renderPartial($name, $parameters, $throwException);
}

/**
@@ -108,9 +94,9 @@ public function partialFunction($name, $parameters = [], $throwException = false
* @param array $parameters A optional list of parameters to pass to the content.
* @return string Returns the file contents.
*/
public function contentFunction($name, $parameters = [])
public function contentFunction($context, $name, $parameters = [])
{
return $this->controller->renderContent($name, $parameters);
return $context['this']['controller']->renderContent($name, $parameters);
}

/**
@@ -119,18 +105,18 @@ public function contentFunction($name, $parameters = [])
* @param array $parameters A optional list of parameters to pass to the component.
* @return string Returns the component default contents.
*/
public function componentFunction($name, $parameters = [])
public function componentFunction($context, $name, $parameters = [])
{
return $this->controller->renderComponent($name, $parameters);
return $context['this']['controller']->renderComponent($name, $parameters);
}

/**
* Renders registered assets of a given type
* @return string Returns the component default contents.
*/
public function assetsFunction($type = null)
public function assetsFunction($context, $type = null)
{
return $this->controller->makeAssets($type);
return $context['this']['controller']->makeAssets($type);
}

/**
@@ -156,9 +142,9 @@ public function placeholderFunction($name, $default = null)
* when creating the URL, set to false to disable this feature.
* @return string
*/
public function pageFilter($name, $parameters = [], $routePersistence = true)
public function pageFilter($context, $name, $parameters = [], $routePersistence = true)
{
return $this->controller->pageUrl($name, $parameters, $routePersistence);
return $context['this']['controller']->pageUrl($name, $parameters, $routePersistence);
}

/**
@@ -167,9 +153,9 @@ public function pageFilter($name, $parameters = [], $routePersistence = true)
* @param mixed $url Specifies the theme-relative URL
* @return string
*/
public function themeFilter($url)
public function themeFilter($context, $url)
{
return $this->controller->themeUrl($url);
return $context['this']['controller']->themeUrl($url);
}

/**
2 changes: 1 addition & 1 deletion modules/cms/twig/PageNode.php
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public function compile(Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->pageFunction();\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->pageFunction(\$context);\n")
;
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/PartialNode.php
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public function compile(Twig_Compiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->partialFunction(")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->partialFunction(\$context,")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_partial_params']")
->write(", true")
2 changes: 1 addition & 1 deletion modules/cms/twig/ScriptsNode.php
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public function compile(Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('js');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction(\$context, 'js');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('scripts');\n")
;
}
2 changes: 1 addition & 1 deletion modules/cms/twig/StylesNode.php
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public function compile(Twig_Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('css');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction(\$context, 'css');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('styles');\n")
;
}