Skip to content

Commit

Permalink
[FEATURE] Handle ->getPublicUrl() on FAL and MediaHelper objects
Browse files Browse the repository at this point in the history
Resolves #2591
  • Loading branch information
helsner authored and simonschaufi committed Apr 23, 2024
1 parent 87985a4 commit 4ff50d1
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/v11/typo3-113.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\TYPO311\v3\HandlePublicFALUrlsWithRelativePathRector;
use Ssch\TYPO3Rector\TYPO311\v3\MigrateHttpUtilityRedirectRector;
use Ssch\TYPO3Rector\TYPO311\v3\SubstituteExtbaseRequestGetBaseUriRector;
use Ssch\TYPO3Rector\TYPO311\v3\SubstituteMethodRmFromListOfGeneralUtilityRector;
Expand All @@ -16,4 +17,5 @@
$rectorConfig->rule(SubstituteExtbaseRequestGetBaseUriRector::class);
$rectorConfig->rule(UseNormalizedParamsToGetRequestUrlRector::class);
$rectorConfig->rule(MigrateHttpUtilityRedirectRector::class);
$rectorConfig->rule(HandlePublicFALUrlsWithRelativePathRector::class);
};
83 changes: 83 additions & 0 deletions rules/TYPO311/v3/HandlePublicFALUrlsWithRelativePathRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO311\v3;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.3/Deprecation-94193-PublicUrlWithRelativePathsInFALAPI.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\HandlePublicFALUrlsWithRelativePathRector\HandlePublicFALUrlsWithRelativePathRectorTest
*/
final class HandlePublicFALUrlsWithRelativePathRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param Node\Expr\MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

if ($this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Resource\FileInterface')
)) {
$node->args = [];
}

if ($this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface')
)) {
$node->args = [$node->args[0]];
}

return $node;
}

/**
* @codeCoverageIgnore
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Handles public FAL urls with relative paths', [new CodeSample(
<<<'CODE_SAMPLE'
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
CODE_SAMPLE
)]);
}

private function shouldSkip(MethodCall $node): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Resource\FileInterface')
)
&& ! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface')
)) {
return true;
}

return ! $this->isName($node->name, 'getPublicUrl');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace TYPO3\CMS\Core\Resource;

if (class_exists('TYPO3\CMS\Core\Resource\Event\GeneratePublicUrlForResourceEvent')) {
return;
}

class GeneratePublicUrlForResourceEvent
{
public function isRelativeToCurrentScript(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TYPO3\CMS\Core\Resource\OnlineMedia\Helpers;

use TYPO3\CMS\Core\Resource\File;

if (interface_exists('TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface')) {
return;
}

interface OnlineMediaHelperInterface
{
public function getPublicUrl(File $file, $relativeToCurrentScript = false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\HandlePublicFALUrlsWithRelativePathRector\Fixture;

use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class TestFile implements FileInterface
{
public function getPublicUrl(bool $relativeToCurrentScript)
{
return 'foo';
}
}

class MyCustomMediaHelper implements OnlineMediaHelperInterface
{
public function getPublicUrl(File $file, $relativeToCurrentScript = false)
{
return 'foo';
}
}


$file = GeneralUtility::makeInstance(TestFile::class);
$foo = $file->getPublicUrl(true);

$helper = GeneralUtility::makeInstance(MyCustomMediaHelper::class);
$bar = $helper->getPublicUrl($file, true);

// $event = GeneralUtility::makeInstance()

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\HandlePublicFALUrlsWithRelativePathRector\Fixture;

use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class TestFile implements FileInterface
{
public function getPublicUrl(bool $relativeToCurrentScript)
{
return 'foo';
}
}

class MyCustomMediaHelper implements OnlineMediaHelperInterface
{
public function getPublicUrl(File $file, $relativeToCurrentScript = false)
{
return 'foo';
}
}

$file = GeneralUtility::makeInstance(TestFile::class);
$foo = $file->getPublicUrl();

$helper = GeneralUtility::makeInstance(MyCustomMediaHelper::class);
$bar = $helper->getPublicUrl($file);

// $event = GeneralUtility::makeInstance()

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\HandlePublicFALUrlsWithRelativePathRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

/**
* @return Iterator<array<string>>
*/
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

declare(strict_types=1);

use Ssch\TYPO3Rector\TYPO311\v3\HandlePublicFALUrlsWithRelativePathRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../../config/config_test.php');

$services = $containerConfigurator->services();
$services->set(HandlePublicFALUrlsWithRelativePathRector::class);
};

0 comments on commit 4ff50d1

Please sign in to comment.