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

Replace removed Fusion prototypes #50

Merged
merged 2 commits into from
Apr 17, 2024
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
9 changes: 9 additions & 0 deletions config/set/contentrepository-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
use Rector\Transform\ValueObject\MethodCallToPropertyFetch;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenAfterDateTimeRector;
use Neos\Rector\ContentRepository90\Rules\FusionNodeHiddenBeforeDateTimeRector;
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;

return static function (RectorConfig $rectorConfig): void {
// Register FusionFileProcessor. All Fusion Rectors will be auto-registered at this processor.
Expand Down Expand Up @@ -97,6 +99,13 @@
'Neos\ContentRepository\Domain\Model\Workspace' => \Neos\ContentRepository\Core\Projection\Workspace\Workspace::class,
]);

$rectorConfig->ruleWithConfiguration(FusionReplacePrototypeNameRector::class, [
new FusionPrototypeNameReplacement('Neos.Fusion:Array' , 'Neos.Fusion:Join'),
new FusionPrototypeNameReplacement('Neos.Fusion:RawArray' , 'Neos.Fusion:DataStructure'),
new FusionPrototypeNameReplacement('Neos.Fusion:Collection' , 'Neos.Fusion:Loop'),
new FusionPrototypeNameReplacement('Neos.Fusion:RawCollection', 'Neos.Fusion:Map'),
]);


/** @var $methodCallToPropertyFetches MethodCallToPropertyFetch[] */
$methodCallToPropertyFetches = [];
Expand Down
45 changes: 45 additions & 0 deletions src/Generic/Rules/FusionReplacePrototypeNameRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Neos\Rector\Generic\Rules;

use Neos\Rector\Core\FusionProcessing\FusionRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Neos\Rector\Utility\CodeSampleLoader;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Neos\Rector\Generic\ValueObject\FusionNodePropertyPathToWarningComment;
use Webmozart\Assert\Assert;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;

class FusionReplacePrototypeNameRector implements FusionRectorInterface, ConfigurableRectorInterface
{

/**
* @var FusionPrototypeNameReplacement[]
*/
private array $fusionPrototypeNameReplacements;

public function getRuleDefinition(): RuleDefinition
{
return CodeSampleLoader::fromFile('Fusion: Rewrite prototype names form e.g Foo.Bar:Boo to Boo.Bar:Foo', __CLASS__);
}

public function refactorFileContent(string $fileContent): string
{
foreach ($this->fusionPrototypeNameReplacements as $fusionPrototypeNameReplacement) {
$pattern = '/(^|[=\s\(<\/])(' .$fusionPrototypeNameReplacement->oldName. ')([\s\{\)\/>]|$)/';
$replacement = '$1'.$fusionPrototypeNameReplacement->newName.'$3';
$fileContent = preg_replace($pattern, $replacement, $fileContent);
}

return $fileContent;
}

/**
* @param FusionNodePropertyPathToWarningComment[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, FusionPrototypeNameReplacement::class);
$this->fusionPrototypeNameReplacements = $configuration;
}
}
13 changes: 13 additions & 0 deletions src/Generic/ValueObject/FusionPrototypeNameReplacement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Neos\Rector\Generic\ValueObject;

class FusionPrototypeNameReplacement
{
public function __construct(
public readonly string $oldName,
public readonly string $newName,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
prototype(Neos.Neos:SomethingOld) < prototype(Neos.Neos:Raw) {

raw = Neos.Neos:Raw
rawer = Neos.Neos:Rawer

renderer = Neos.Neos:Raw {

old = Neos.Neos:SomethingOld

renderer = afx`
<Neos.Neos:SomethingOld foo=""></Neos.Neos:SomethingOld>
<Neos.Neos:Rawer />
<Neos.Neos:Raw />
`
}
}
-----
prototype(Neos.Neos:SomethingNew) < prototype(Neos.Neos:NewRaw) {

raw = Neos.Neos:NewRaw
rawer = Neos.Neos:Rawer

renderer = Neos.Neos:NewRaw {

old = Neos.Neos:SomethingNew

renderer = afx`
<Neos.Neos:SomethingNew foo=""></Neos.Neos:SomethingNew>
<Neos.Neos:Rawer />
<Neos.Neos:NewRaw />
`
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Neos\Rector\Tests\Generic\Rules\FusionPrototypeNameReplacement;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FusionPrototypeNameReplacementTest 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,23 @@
<?php

declare (strict_types=1);

use Neos\Rector\Core\FusionProcessing\FusionFileProcessor;
use Rector\Config\RectorConfig;
use Neos\Rector\Generic\ValueObject\FusionPrototypeNameReplacement;
use Neos\Rector\Generic\Rules\FusionReplacePrototypeNameRector;

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

$rectorConfig->ruleWithConfiguration(FusionReplacePrototypeNameRector::class, [
new FusionPrototypeNameReplacement('Neos.Neos:Raw', 'Neos.Neos:NewRaw'),
new FusionPrototypeNameReplacement('Neos.Neos:SomethingOld', 'Neos.Neos:SomethingNew'),
]);
};
Loading