Skip to content

Commit

Permalink
TASK: Migrate fusion context.currentRenderingMode statements
Browse files Browse the repository at this point in the history
Statements accessing `context.currentRenderingMode.edit|preview|name|title|fusionPath|options` on `node|site|documentNode` are migrated. Other uses of `context.currentRenderingMode` are marked with a comment.

Resolves: #23
  • Loading branch information
mficzel committed Oct 4, 2023
1 parent 24d8ffc commit 963f7a0
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Neos\Rector\ContentRepository90\Rules;

use Neos\Rector\Core\FusionProcessing\EelExpressionTransformer;
use Neos\Rector\Core\FusionProcessing\FusionRectorInterface;
use Neos\Rector\Utility\CodeSampleLoader;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class FusionContextCurrentRenderingModeRector implements FusionRectorInterface
{

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('Fusion: Rewrite node.context.currentRenderingMode... to renderingMode...', __CLASS__);
}

public function refactorFileContent(string $fileContent): string
{
return EelExpressionTransformer::parse($fileContent)
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.name',
'renderingMode.name',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.edit',
'renderingMode.isEdit',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.preview',
'renderingMode.isPreview',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.fusionPath',
'renderingMode.fusionPath',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.options',
'renderingMode.options',
$eelExpression
))
->process(fn(string $eelExpression) => preg_replace(
'/(node|documentNode|site)\.context\.currentRenderingMode\.title',
'renderingMode.title',
$eelExpression
))
->addCommentsIfRegexMatches(
'/\.context\.currentRenderingMode/',
'// TODO 9.0 migration: Line %LINE: You very likely need to rewrite "VARIABLE.context.currentRenderingMode..." to "renderingMode...". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.'
)->getProcessedContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) {

renderer = Neos.Fusion:Component {

#
# pass down props
#
nodeAttributes = ${node.context.currentRenderingMode.edit || node.currentRenderingMode.preview || node.context.currentRenderingMode.title || node.context.currentRenderingMode.name || node.context.currentRenderingMode.fusionPath || node.context.currentRenderingMode.options['foo']}
siteAttributes = ${site.context.currentRenderingMode.edit || site.currentRenderingMode.preview || site.context.currentRenderingMode.title || site.context.currentRenderingMode.name || site.context.currentRenderingMode.fusionPath || site.context.currentRenderingMode.options['foo']}
documentNodeAttributes = ${documentNode.context.currentRenderingMode.edit || documentNode.currentRenderingMode.preview || documentNode.context.currentRenderingMode.title || documentNode.context.currentRenderingMode.name || documentNode.context.currentRenderingMode.fusionPath || documentNode.context.currentRenderingMode.options['foo']}
other = ${other.context.currentRenderingMode.edit || other.currentRenderingMode.preview || other.context.currentRenderingMode.title || other.context.currentRenderingMode.name || other.context.currentRenderingMode.fusionPath || other.context.currentRenderingMode.options['foo']}

renderer = afx`
<input
type="checkbox"
name={node.context.currentRenderingMode.name}
value={node.context.currentRenderingMode.title}
checked={node.context.currentRenderingMode.edit}
{...node.context.currentRenderingMode.options}
/>
`
}
}
-----
// TODO 9.0 migration: Line 26: You very likely need to rewrite "VARIABLE.context.currentRenderingMode..." to "renderingMode...". We did not auto-apply this migration because we cannot be sure whether the variable is a Node.
prototype(Neos.Fusion.Form:Checkbox) < prototype(Neos.Fusion.Form:Component.Field) {

renderer = Neos.Fusion:Component {

nodeAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']}
siteAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']}
documentNodeAttributes = ${renderingMode.isEdit || renderingMode.isPreview || renderingMode.title || renderingMode.name || renderingMode.fusionPath || renderingMode.options['foo']}
other = ${other.context.currentRenderingMode.edit || other.currentRenderingMode.preview || other.context.currentRenderingMode.title || other.context.currentRenderingMode.name || other.context.currentRenderingMode.fusionPath || other.context.currentRenderingMode.options['foo']}

renderer = afx`
<input
type="checkbox"
name={renderingMode.name}
value={renderingMode.title}
checked={renderingMode.isEdit}
{...renderingMode.options}
/>
`
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\ContentRepository90\Rules\FusionContextCurrentRenderingModeRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FusionContextCurrentRenderingModeRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $fileInfo): void
{
$this->doTestFile($fileInfo);
}

/**
* @return \Iterator<string>
*/
public function provideData(): \Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture', '*.fusion.inc');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare (strict_types=1);
use Neos\Rector\ContentRepository90\Rules\FusionContextCurrentRenderingModeRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig) : void {
$services = $rectorConfig->services();
$services->defaults()
->public()
->autowire()
->autoconfigure();
$services->set(\Neos\Rector\Core\FusionProcessing\FusionFileProcessor::class);
$rectorConfig->disableParallel(); // does not work for fusion files - see https://github.com/rectorphp/rector-src/pull/2597#issuecomment-1190120688

$rectorConfig->rule(FusionContextCurrentRenderingModeRector::class);
};

0 comments on commit 963f7a0

Please sign in to comment.