Skip to content

Commit

Permalink
Merge pull request #50 from neos/removed-fusion-prototypes
Browse files Browse the repository at this point in the history
Replace removed Fusion prototypes
  • Loading branch information
bwaidelich authored Apr 17, 2024
2 parents d7c832e + a770c89 commit 0ed18d8
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
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'),
]);
};

0 comments on commit 0ed18d8

Please sign in to comment.