Skip to content

Commit

Permalink
[FEATURE] Provide rules to migrate v:extension.path.absolute and .rel…
Browse files Browse the repository at this point in the history
…ative
  • Loading branch information
kitzberger committed Jul 17, 2023
1 parent fb46b9d commit 1d5230b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/replace-vhs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathAbsoluteFluidRector;
use Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathRelativeFluidRector;
use Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceFormatJsonEncodeFluidRector;
use Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceLFluidRector;
use Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceMediaImageFluidRector;
Expand All @@ -12,6 +14,8 @@

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/config.php');
$rectorConfig->rule(ReplaceExtensionPathAbsoluteFluidRector::class);
$rectorConfig->rule(ReplaceExtensionPathRelativeFluidRector::class);
$rectorConfig->rule(ReplaceFormatJsonEncodeFluidRector::class);
$rectorConfig->rule(ReplaceLFluidRector::class);
$rectorConfig->rule(ReplaceMediaImageFluidRector::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs;

use Nette\Utils\Strings;
use Rector\Core\ValueObject\Application\File;
use Ssch\TYPO3Rector\Contract\FileProcessor\Fluid\Rector\FluidRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ReplaceExtensionPathAbsoluteFluidRector implements FluidRectorInterface
{
/**
* @var string
*/
private const PATTERN_INLINE = '#{(v|vhs):extension\.path\.absolute\(extensionName:(\s?)["\']([a-z0-9_]+)["\']\)}Resources\/Public\/(\S+)\.([a-z0-9]{2,4})#ims';
/**
* @var string
*/
private const PATTERN_LEFTOVERS = '#(v|vhs):(extension\.path\.absolute)#ims';

public function transform(File $file): void
{
$content = $file->getFileContent();

$content = Strings::replace(
$content,
self::PATTERN_INLINE,
'{f:uri.resource(extensionName:\'\3\',path:\'\4.\5\',absolute:1)}'
);

if (Strings::matchAll($content, self::PATTERN_LEFTOVERS)) {
$this->rectorOutputStyle->warning('There\'s occurrences of v:extension.path.absolute that couldn\'t be migrated automatically. Migrate them manually!');

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathAbsoluteFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathAbsoluteFluidRector::$rectorOutputStyle.

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathAbsoluteFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathAbsoluteFluidRector::$rectorOutputStyle.

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathAbsoluteFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathAbsoluteFluidRector::$rectorOutputStyle.
}

$file->changeFileContent($content);
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use <f:uri.resource> instead of <v:extension.path.absolute>', [
new CodeSample(
<<<'CODE_SAMPLE'
{v:extension.path.absolute(extensionName:'my_extension')}Resources/Public/Css/style.css
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
{f:uri.resource(extensionName:'my_extension',path:'Css/style.css',absolute:1)}
CODE_SAMPLE
),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs;

use Nette\Utils\Strings;
use Rector\Core\ValueObject\Application\File;
use Ssch\TYPO3Rector\Contract\FileProcessor\Fluid\Rector\FluidRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ReplaceExtensionPathRelativeFluidRector implements FluidRectorInterface
{
/**
* @var string
*/
private const PATTERN_INLINE = '#{(v|vhs):extension\.path\.relative\(extensionName:(\s?)["\']([a-z0-9_]+)["\']\)}Resources\/Public\/(\S+)\.([a-z0-9]{2,4})#ims';
/**
* @var string
*/
private const PATTERN_LEFTOVERS = '#(v|vhs):(extension\.path\.relative)#ims';

public function transform(File $file): void
{
$content = $file->getFileContent();

$content = Strings::replace(
$content,
self::PATTERN_INLINE,
'{f:uri.resource(extensionName:\'\3\',path:\'\4.\5\')}'
);

if (Strings::matchAll($content, self::PATTERN_LEFTOVERS)) {
$this->rectorOutputStyle->warning('There\'s occurrences of v:extension.path.relative that couldn\'t be migrated automatically. Migrate them manually!');

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathRelativeFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathRelativeFluidRector::$rectorOutputStyle.

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathRelativeFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathRelativeFluidRector::$rectorOutputStyle.

Check failure on line 36 in src/FileProcessor/Fluid/Rector/vhs/ReplaceExtensionPathRelativeFluidRector.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\vhs\ReplaceExtensionPathRelativeFluidRector::$rectorOutputStyle.
}

$file->changeFileContent($content);
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use <f:uri.resource> instead of <v:extension.path.relative>', [
new CodeSample(
<<<'CODE_SAMPLE'
{v:extension.path.relative(extensionName:'my_extension')}Resources/Public/Css/style.css
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
{f:uri.resource(extensionName:'my_extension',path:'Css/style.css')}
CODE_SAMPLE
),
]);
}
}

0 comments on commit 1d5230b

Please sign in to comment.