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

WIP: twig node migration #1102

Draft
wants to merge 4 commits into
base: wip-laravel-11
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Migrate twig nodes to use yield
  • Loading branch information
mjauvin committed Apr 16, 2024
commit aa8daf36bffa79945a536e3fad1be7ecfd588644
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"winter/wn-backend-module": "dev-wip-laravel-11",
"winter/wn-cms-module": "dev-wip-laravel-11",
"laravel/framework": "^11.0",
"twig/twig": "~3.8.0",
"twig/twig": "^3.9.0",
"wikimedia/composer-merge-plugin": "~2.1.0"
},
"require-dev": {
Expand Down
4 changes: 3 additions & 1 deletion modules/cms/twig/ComponentNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class ComponentNode extends TwigNode
{
public function __construct(TwigNode $nodes, $paramNames, $lineno, $tag = 'component')
Expand All @@ -34,7 +36,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->componentFunction(")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->componentFunction(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_component_params']")
->write(");\n")
Expand Down
4 changes: 3 additions & 1 deletion modules/cms/twig/ContentNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class ContentNode extends TwigNode
{
public function __construct(TwigNode $nodes, $paramNames, $lineno, $tag = 'content')
Expand Down Expand Up @@ -36,7 +38,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->contentFunction(")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->contentFunction(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_content_params']")
->write(", true")
Expand Down
4 changes: 3 additions & 1 deletion modules/cms/twig/DefaultNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class DefaultNode extends TwigNode
{
public function __construct($lineno, $tag = 'default')
Expand All @@ -25,7 +27,7 @@ public function compile(TwigCompiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo '<!-- X_WINTER_DEFAULT_BLOCK_CONTENT -->';\n")
->write("yield '<!-- X_WINTER_DEFAULT_BLOCK_CONTENT -->';\n")
;
}
}
12 changes: 7 additions & 5 deletions modules/cms/twig/FrameworkNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use System\Models\Parameter;
use System\Classes\CombineAssets;
use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;
use Url;
Expand All @@ -12,6 +13,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class FrameworkNode extends TwigNode
{
public function __construct($name, $lineno, $tag = 'framework')
Expand Down Expand Up @@ -41,20 +43,20 @@ public function compile(TwigCompiler $compiler)
$compiler
->write("if (\$_minify) {" . PHP_EOL)
->indent()
->write("echo '<script src=\"{$basePath}/modules/system/assets/js/framework.combined-min.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->write("yield '<script src=\"{$basePath}/modules/system/assets/js/framework.combined-min.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->outdent()
->write("}" . PHP_EOL)
->write("else {" . PHP_EOL)
->indent()
->write("echo '<script src=\"{$basePath}/modules/system/assets/js/framework.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->write("echo '<script src=\"{$basePath}/modules/system/assets/js/framework.extras.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->write("yield '<script src=\"{$basePath}/modules/system/assets/js/framework.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->write("yield '<script src=\"{$basePath}/modules/system/assets/js/framework.extras.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL)
->outdent()
->write("}" . PHP_EOL)
->write("echo '<link rel=\"stylesheet\" property=\"stylesheet\" href=\"{$basePath}/modules/system/assets/css/framework.extras'.(\$_minify ? '-min' : '').'.css$cacheBust\">'.PHP_EOL;" . PHP_EOL)
->write("yield '<link rel=\"stylesheet\" property=\"stylesheet\" href=\"{$basePath}/modules/system/assets/css/framework.extras'.(\$_minify ? '-min' : '').'.css$cacheBust\">'.PHP_EOL;" . PHP_EOL)
;
}
else {
$compiler->write("echo '<script src=\"{$basePath}/modules/system/assets/js/framework'.(\$_minify ? '-min' : '').'.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL);
$compiler->write("yield '<script src=\"{$basePath}/modules/system/assets/js/framework'.(\$_minify ? '-min' : '').'.js$cacheBust\"></script>'.PHP_EOL;" . PHP_EOL);
}

$compiler->write('unset($_minify);' . PHP_EOL);
Expand Down
4 changes: 3 additions & 1 deletion modules/cms/twig/PageNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class PageNode extends TwigNode
{
public function __construct($lineno, $tag = 'page')
Expand All @@ -25,7 +27,7 @@ public function compile(TwigCompiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->pageFunction();\n")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->pageFunction();\n")
;
}
}
4 changes: 3 additions & 1 deletion modules/cms/twig/PartialNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class PartialNode extends TwigNode
{
public function __construct(TwigNode $nodes, $paramNames, $lineno, $tag = 'partial')
Expand All @@ -34,7 +36,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->partialFunction(")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->partialFunction(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__cms_partial_params']")
->write(", true")
Expand Down
6 changes: 4 additions & 2 deletions modules/cms/twig/PlaceholderNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class PlaceholderNode extends TwigNode
{
public function __construct($name, $paramValues, $body, $lineno, $tag = 'placeholder')
Expand Down Expand Up @@ -54,10 +56,10 @@ public function compile(TwigCompiler $compiler)

$compiler->addDebugInfo($this);
if (!$isText) {
$compiler->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
$compiler->write("yield \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
}
else {
$compiler->write("echo twig_escape_filter(\$this->env, \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
$compiler->write("yield twig_escape_filter(\$this->env, \$this->env->getExtension('Cms\Twig\Extension')->displayBlock(");
}

$compiler
Expand Down
6 changes: 4 additions & 2 deletions modules/cms/twig/PutNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class PutNode extends TwigNode
{
public function __construct(TwigNode $body, $name, $endType, $lineno, $tag = 'put')
Expand All @@ -25,7 +27,7 @@ public function compile(TwigCompiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->startBlock(")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->startBlock(")
->raw("'".$this->getAttribute('name')."'")
->write(");\n")
;
Expand All @@ -36,7 +38,7 @@ public function compile(TwigCompiler $compiler)

$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->endBlock(")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->endBlock(")
->raw($isOverwrite ? 'false' : 'true')
->write(");\n")
;
Expand Down
6 changes: 4 additions & 2 deletions modules/cms/twig/ScriptsNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class ScriptsNode extends TwigNode
{
public function __construct($lineno, $tag = 'scripts')
Expand All @@ -25,8 +27,8 @@ public function compile(TwigCompiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('js');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('scripts');\n")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('js');\n")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('scripts');\n")
;
}
}
10 changes: 6 additions & 4 deletions modules/cms/twig/SnowboardNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Config;
use System\Classes\CombineAssets;
use System\Models\Parameter;
use Twig\Attribute\YieldReady;
use Twig\Compiler as TwigCompiler;
use Twig\Node\Node as TwigNode;
use Url;
Expand All @@ -13,6 +14,7 @@
* @package winter\wn-cms-module
* @author Winter CMS
*/
#[YieldReady]
class SnowboardNode extends TwigNode
{
/**
Expand Down Expand Up @@ -55,25 +57,25 @@ public function compile(TwigCompiler $compiler)
if (!static::$baseLoaded) {
// Add manifest and vendor files
$compiler
->write("echo '<script data-module=\"snowboard-manifest\" src=\"{$manifestPath}{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
->write("yield '<script data-module=\"snowboard-manifest\" src=\"{$manifestPath}{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
$vendorJs = $moduleMap['vendor'];
$compiler
->write("echo '<script data-module=\"snowboard-vendor\" src=\"{$basePath}{$vendorJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
->write("yield '<script data-module=\"snowboard-vendor\" src=\"{$basePath}{$vendorJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);

// Add base script
$baseJs = $moduleMap['base'];
$baseUrl = Url::to('/');
$assetUrl = Url::asset('/');
$compiler
->write("echo '<script data-module=\"snowboard-base\" data-base-url=\"{$baseUrl}\" data-asset-url=\"{$assetUrl}\" src=\"{$basePath}{$baseJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
->write("yield '<script data-module=\"snowboard-base\" data-base-url=\"{$baseUrl}\" data-asset-url=\"{$assetUrl}\" src=\"{$basePath}{$baseJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);

static::$baseLoaded = true;
}

foreach ($modules as $module) {
$moduleJs = $moduleMap[$module];
$compiler
->write("echo '<script data-module=\"{$module}\" src=\"{$basePath}{$moduleJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
->write("yield '<script data-module=\"{$module}\" src=\"{$basePath}{$moduleJs}.js{$cacheBust}\"></script>'.PHP_EOL;" . PHP_EOL);
}
}
}
6 changes: 4 additions & 2 deletions modules/cms/twig/StylesNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Cms\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class StylesNode extends TwigNode
{
public function __construct($lineno, $tag = 'styles')
Expand All @@ -25,8 +27,8 @@ public function compile(TwigCompiler $compiler)
{
$compiler
->addDebugInfo($this)
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('css');\n")
->write("echo \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('styles');\n")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->assetsFunction('css');\n")
->write("yield \$this->env->getExtension('Cms\Twig\Extension')->displayBlock('styles');\n")
;
}
}
4 changes: 3 additions & 1 deletion modules/system/twig/MailPartialNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace System\Twig;

use Twig\Attribute\YieldReady;
use Twig\Node\Node as TwigNode;
use Twig\Compiler as TwigCompiler;

Expand All @@ -9,6 +10,7 @@
* @package winter\wn-cms-module
* @author Alexey Bobkov, Samuel Georges
*/
#[YieldReady]
class MailPartialNode extends TwigNode
{
public function __construct(TwigNode $nodes, $paramNames, $body, $lineno, $tag = 'partial')
Expand Down Expand Up @@ -48,7 +50,7 @@ public function compile(TwigCompiler $compiler)
}

$compiler
->write("echo \System\Classes\MailManager::instance()->renderPartial(")
->write("yield \System\Classes\MailManager::instance()->renderPartial(")
->subcompile($this->getNode('nodes')->getNode(0))
->write(", \$context['__system_partial_params']")
->write(");\n")
Expand Down
8 changes: 5 additions & 3 deletions modules/system/twig/SpacelessNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace System\Twig;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Node;
use Twig\Node\NodeOutputInterface;
Expand All @@ -13,6 +14,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
#[YieldReady]
class SpacelessNode extends Node implements NodeOutputInterface
{
public function __construct(Node $body, int $lineno, string $tag = 'spaceless')
Expand All @@ -26,13 +28,13 @@ public function compile(Compiler $compiler)
->addDebugInfo($this)
;
if ($compiler->getEnvironment()->isDebug()) {
$compiler->write("ob_start();\n");
$compiler->write("ob_start(); yield '';\n");
} else {
$compiler->write("ob_start(function () { return ''; });\n");
$compiler->write("ob_start(function () { return ''; }); yield '';\n");
}
$compiler
->subcompile($this->getNode('body'))
->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
->write("yield trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
;
}
}
Loading