-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Handle ->getPublicUrl() on FAL and MediaHelper objects
Resolves #2591
- Loading branch information
1 parent
87985a4
commit 4ff50d1
Showing
7 changed files
with
229 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
83 changes: 83 additions & 0 deletions
83
rules/TYPO311/v3/HandlePublicFALUrlsWithRelativePathRector.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,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'); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
stubs/TYPO3/CMS/Core/Resource/Event/GeneratePublicUrlForResourceEvent.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,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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
stubs/TYPO3/CMS/Core/Resource/OnlineMedia/Helpers/OnlineMediaHelperInterface.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,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); | ||
} |
70 changes: 70 additions & 0 deletions
70
tests/Rector/v11/v3/HandlePublicFALUrlsWithRelativePathRector/Fixture/fixture.php.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,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() | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...ndlePublicFALUrlsWithRelativePathRector/HandlePublicFALUrlsWithRelativePathRectorTest.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,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'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Rector/v11/v3/HandlePublicFALUrlsWithRelativePathRector/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,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); | ||
}; |