-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from neos/removed-fusion-prototypes
Replace removed Fusion prototypes
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/Generic/ValueObject/FusionPrototypeNameReplacement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) { | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Generic/Rules/FusionPrototypeNameReplacement/Fixture/some_file.fusion.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /> | ||
` | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Generic/Rules/FusionPrototypeNameReplacement/FusionPrototypeNameReplacementTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Generic/Rules/FusionPrototypeNameReplacement/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]); | ||
}; |