Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions bundle/Templating/Twig/Runtime/RemoteMediaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
use Twig\Extension\AbstractExtension;

use function count;
use function get_debug_type;
use function in_array;
use function method_exists;
use function trigger_deprecation;

final class RemoteMediaRuntime extends AbstractExtension
{
Expand Down Expand Up @@ -113,32 +109,7 @@ public function getRemoteResourceHtmlTag(RemoteResource $resource, array $htmlAt

public function getRemoteResourceLocationHtmlTag(RemoteResourceLocation $location, array $htmlAttributes = [], bool $forceVideo = false, bool $useThumbnail = false): string
{
// The check and the fallback below will be removed in 4.0, when generateLocationHtmlTag() is added to ProviderInterface.
if (method_exists($this->provider, 'generateLocationHtmlTag')) {
return $this->provider->generateLocationHtmlTag($location, $htmlAttributes, $forceVideo, $useThumbnail);
}

trigger_deprecation(
'netgen/remote-media-bundle',
'3.1',
'Not implementing the "%s::generateLocationHtmlTag()" method in "%s" is deprecated, the method will be added to the interface in 4.0.',
ProviderInterface::class,
get_debug_type($this->provider),
);

$type = $location->getRemoteResource()->getType();
$isRenderedAsImage = $type === RemoteResource::TYPE_IMAGE
|| ($useThumbnail && in_array($type, [RemoteResource::TYPE_VIDEO, RemoteResource::TYPE_AUDIO], true));

if ($isRenderedAsImage && !($htmlAttributes['alt'] ?? null) && $location->getAltTextOverride() !== null) {
$htmlAttributes['alt'] = $location->getAltTextOverride();
}

if ($isRenderedAsImage && !($htmlAttributes['title'] ?? null) && $location->getCaptionOverride() !== null) {
$htmlAttributes['title'] = $location->getCaptionOverride();
}

return $this->provider->generateHtmlTag($location->getRemoteResource(), $htmlAttributes, $forceVideo, $useThumbnail);
return $this->provider->generateLocationHtmlTag($location, $htmlAttributes, $forceVideo, $useThumbnail);
}

public function getRemoteResourceVariationHtmlTag(
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"symfony/form": "^5.4 || ^6.2",
"symfony/serializer": "^5.4 || ^6.2",
"symfony/translation": "^5.4 || ^6.2",
"symfony/deprecation-contracts": "^2.5 || ^3.0",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.10"
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Netgen Remote Media Bundle changelog

## 4.0.0

### BC breaks

* `generateLocationHtmlTag()` is added to `ProviderInterface` (it was declared with `@method` since 3.1), custom providers which don't extend `AbstractProvider` must implement it, the TWIG runtime no longer falls back to `generateHtmlTag()`

## 3.1.0

### Added
Expand Down
7 changes: 7 additions & 0 deletions docs/UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Netgen Remote Media Bundle upgrade instructions
===============================================

Upgrade from 3.1 to 4.0
-----------------------

### Code changes

* `ProviderInterface::generateLocationHtmlTag()` is now part of the interface. Custom providers which don't extend `AbstractProvider` must implement it. In 3.1 they triggered a deprecation and the TWIG runtime applied the location overrides itself.

Upgrade from 3.0.3 to 3.1.0
---------------------------

Expand Down
10 changes: 2 additions & 8 deletions lib/API/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
use Netgen\RemoteMedia\Exception\RemoteResourceLocationNotFoundException;
use Netgen\RemoteMedia\Exception\RemoteResourceNotFoundException;

/**
* @method string generateLocationHtmlTag(RemoteResourceLocation $location, array $htmlAttributes = [], bool $forceVideo = false, bool $useThumbnail = false) Generates the HTML tag for the resource of the location, using alt text and caption overrides of the location. Not implementing it is deprecated since 3.1, it will be added to the interface in 4.0.
*/
interface ProviderInterface
{
public function getIdentifier(): string;
Expand Down Expand Up @@ -158,13 +155,10 @@ public function buildVideoThumbnailRawVariation(RemoteResource $resource, array

public function generateHtmlTag(RemoteResource $resource, array $htmlAttributes = [], bool $forceVideo = false, bool $useThumbnail = false): string;

/*
/**
* Generates the HTML tag for the resource of the location, using alt text and caption overrides of the location.
*
* Will be added to the interface in 4.0, see the @method annotation of the interface.
*
* public function generateLocationHtmlTag(RemoteResourceLocation $location, array $htmlAttributes = [], bool $forceVideo = false, bool $useThumbnail = false): string;
*/
public function generateLocationHtmlTag(RemoteResourceLocation $location, array $htmlAttributes = [], bool $forceVideo = false, bool $useThumbnail = false): string;

public function generateVariationHtmlTag(
RemoteResourceLocation $location,
Expand Down
90 changes: 2 additions & 88 deletions tests/bundle/Templating/Twig/Runtime/RemoteMediaRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Netgen\RemoteMedia\API\Values\RemoteResource;
use Netgen\RemoteMedia\API\Values\RemoteResourceLocation;
use Netgen\RemoteMedia\API\Values\RemoteResourceVariation;
use Netgen\RemoteMedia\Core\AbstractProvider;
use Netgen\RemoteMedia\Core\Provider\Cloudinary\CloudinaryProvider;
use Netgen\RemoteMedia\Core\Resolver\Variation as VariationResolver;
use Netgen\RemoteMedia\Core\Transformation\Registry;
Expand All @@ -24,12 +23,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;

use function json_encode;
use function restore_error_handler;
use function set_error_handler;

use const E_USER_DEPRECATED;

#[CoversClass(RemoteMediaRuntime::class)]
final class RemoteMediaRuntimeTest extends AbstractTestCase
{
Expand Down Expand Up @@ -785,94 +778,15 @@ public function testGetRemoteResourceLocationHtmlTag(): void
altTextOverride: 'Local alt text',
);

$provider = $this->createMock(AbstractProvider::class);
$provider
$this->providerMock
->expects(self::once())
->method('generateLocationHtmlTag')
->with($location, ['class' => 'test'], false, false)
->willReturn('<img alt="Local alt text">');

$runtime = new RemoteMediaRuntime($provider, new VariationResolver(new Registry(), null, []));

self::assertSame(
'<img alt="Local alt text">',
$runtime->getRemoteResourceLocationHtmlTag($location, ['class' => 'test']),
);
}

public function testGetRemoteResourceLocationHtmlTagWithProviderWithoutMethod(): void
{
$resource = new RemoteResource(
remoteId: 'upload|image|test_image.jpg',
type: 'image',
url: 'https://cloudinary.com/test/upload/image/test_image.jpg',
md5: 'e522f43cf89aa0afd03387c37e2b6e12',
altText: 'Cloud alt text',
caption: 'Cloud caption',
$this->runtime->getRemoteResourceLocationHtmlTag($location, ['class' => 'test']),
);

$location = new RemoteResourceLocation(
remoteResource: $resource,
altTextOverride: 'Local alt text',
captionOverride: 'Local caption',
);

$this->providerMock
->expects(self::once())
->method('generateHtmlTag')
->with($resource, ['class' => 'test', 'alt' => 'Local alt text', 'title' => 'Local caption'], false, false)
->willReturn('<img alt="Local alt text" title="Local caption">');

$deprecations = [];
set_error_handler(static function (int $level, string $message) use (&$deprecations): bool {
$deprecations[] = $message;

return true;
}, E_USER_DEPRECATED);

try {
$tag = $this->runtime->getRemoteResourceLocationHtmlTag($location, ['class' => 'test']);
} finally {
restore_error_handler();
}

self::assertSame('<img alt="Local alt text" title="Local caption">', $tag);
self::assertCount(1, $deprecations);
self::assertStringContainsString('generateLocationHtmlTag()', $deprecations[0]);
}

public function testGetRemoteResourceLocationHtmlTagWithProviderWithoutMethodKeepsExplicitAttributes(): void
{
$resource = new RemoteResource(
remoteId: 'upload|video|test_video',
type: 'video',
url: 'https://cloudinary.com/test/upload/video/test_video',
md5: 'e522f43cf89aa0afd03387c37e2b6e12',
);

$location = new RemoteResourceLocation(
remoteResource: $resource,
altTextOverride: 'Local alt text',
captionOverride: 'Local caption',
);

$this->providerMock
->expects(self::exactly(2))
->method('generateHtmlTag')
->willReturnCallback(static fn (RemoteResource $resource, array $htmlAttributes): string => json_encode($htmlAttributes));

set_error_handler(static fn (): bool => true, E_USER_DEPRECATED);

try {
// video without thumbnail is not rendered as an image, so the overrides are not used
self::assertSame('[]', $this->runtime->getRemoteResourceLocationHtmlTag($location));
// explicit attributes win over the overrides
self::assertSame(
'{"alt":"Explicit alt","title":"Local caption"}',
$this->runtime->getRemoteResourceLocationHtmlTag($location, ['alt' => 'Explicit alt'], false, true),
);
} finally {
restore_error_handler();
}
}
}
Loading