From 22c7c5df0b0dda6367c63ea5b1af795bd51b0e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 11 May 2023 15:52:56 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Add=20getRaw=E2=80=A6()=20methods=20for=20f?= =?UTF-8?q?orwards=20compatibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpstan.neon.dist | 2 ++ src/Picture.php | 20 ++++++++++++++++++++ src/PictureInterface.php | 8 ++++++++ tests/PictureTest.php | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index c2c9ab7..975a738 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -31,3 +31,5 @@ parameters: - message: '#ResizeCoordinates::isEqualTo\(\) expects .*, stdClass given#' path: %currentWorkingDirectory%/tests/ResizeCoordinatesTest.php + - + message: '#Access to an undefined property XMLReader::#' diff --git a/src/Picture.php b/src/Picture.php index 51902ab..1668a94 100644 --- a/src/Picture.php +++ b/src/Picture.php @@ -45,6 +45,8 @@ public function __construct(array $img, array $sources) public function getImg(string $rootDir = null, string $prefix = ''): array { if (null === $rootDir) { + trigger_deprecation('contao/image', '1.2', 'Passing NULL as $rootDir is deprecated and will no longer work in version 2.0. Use the getRawImg() method instead.'); + if ('' !== $prefix) { throw new InvalidArgumentException(sprintf('Prefix must no be specified if rootDir is null, given "%s"', $prefix)); } @@ -55,12 +57,22 @@ public function getImg(string $rootDir = null, string $prefix = ''): array return $this->buildUrls($this->img, $rootDir, $prefix); } + /** + * {@inheritdoc} + */ + public function getRawImg(): array + { + return $this->img; + } + /** * {@inheritdoc} */ public function getSources(string $rootDir = null, string $prefix = ''): array { if (null === $rootDir) { + trigger_deprecation('contao/image', '1.2', 'Passing NULL as $rootDir is deprecated and will no longer work in version 2.0. Use the getRawSources() method instead.'); + if ('' !== $prefix) { throw new InvalidArgumentException(sprintf('Prefix must no be specified if rootDir is null, given "%s"', $prefix)); } @@ -76,6 +88,14 @@ function ($source) use ($rootDir, $prefix) { ); } + /** + * {@inheritdoc} + */ + public function getRawSources(): array + { + return $this->sources; + } + /** * Converts image objects in an attributes array to URLs. * diff --git a/src/PictureInterface.php b/src/PictureInterface.php index 8a07964..e53be26 100644 --- a/src/PictureInterface.php +++ b/src/PictureInterface.php @@ -12,15 +12,23 @@ namespace Contao\Image; +/** + * @method array getRawImg() + * @method array getRawSources() + */ interface PictureInterface { /** * Returns the image tag attributes. + * + * @param string $rootDir passing NULL is deprecated */ public function getImg(string $rootDir = null, string $prefix = ''): array; /** * Returns the source tags attributes. + * + * @param string $rootDir passing NULL is deprecated */ public function getSources(string $rootDir = null, string $prefix = ''): array; } diff --git a/tests/PictureTest.php b/tests/PictureTest.php index c0d8cf0..a52052a 100644 --- a/tests/PictureTest.php +++ b/tests/PictureTest.php @@ -18,15 +18,18 @@ use Contao\Image\Picture; use Imagine\Image\ImagineInterface; use PHPUnit\Framework\TestCase; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Filesystem\Filesystem; class PictureTest extends TestCase { + use ExpectDeprecationTrait; + public function testGetImg(): void { $picture = $this->createPicture(null, '/path/to/a/filename with special&<>"\'chars.jpeg'); - $this->assertInstanceOf(ImageInterface::class, $picture->getImg()['src']); + $this->assertInstanceOf(ImageInterface::class, $picture->getRawImg()['src']); $this->assertSame( 'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg', @@ -53,7 +56,7 @@ public function testGetImg(): void $picture->getImg('/path/to', 'https://example.com/images/')['src'] ); - $this->assertInstanceOf(ImageInterface::class, $picture->getImg()['srcset'][0][0]); + $this->assertInstanceOf(ImageInterface::class, $picture->getRawImg()['srcset'][0][0]); $this->assertSame( 'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg 1x', @@ -80,8 +83,22 @@ public function testGetImg(): void $picture->getImg('/path/to', 'https://example.com/images/')['srcset'] ); - $this->assertSame('custom attribute', $picture->getImg()['data-custom']); + $this->assertSame('custom attribute', $picture->getRawImg()['data-custom']); $this->assertSame('custom attribute', $picture->getImg('/')['data-custom']); + } + + /** + * @group legacy + */ + public function testDeprecatedGetImg(): void + { + $picture = $this->createPicture(null, '/path/to/image.jpg'); + + $this->expectDeprecation('Passing NULL as $rootDir is deprecated%s'); + + $this->assertInstanceOf(ImageInterface::class, $picture->getImg()['src']); + $this->assertInstanceOf(ImageInterface::class, $picture->getImg()['srcset'][0][0]); + $this->assertSame('custom attribute', $picture->getImg()['data-custom']); $this->expectException(InvalidArgumentException::class); @@ -92,7 +109,7 @@ public function testGetSources(): void { $picture = $this->createPicture(null, '/path/to/a/filename with special&<>"\'chars.jpeg'); - $this->assertInstanceOf(ImageInterface::class, $picture->getSources()[0]['srcset'][0][0]); + $this->assertInstanceOf(ImageInterface::class, $picture->getRawSources()[0]['srcset'][0][0]); $this->assertSame( 'path/to/a/filename%20with%20special%26%3C%3E%22%27chars.jpeg 1x', @@ -119,8 +136,21 @@ public function testGetSources(): void $picture->getSources('/path/to', 'https://example.com/images/')[0]['srcset'] ); - $this->assertSame('custom attribute', $picture->getSources()[0]['data-custom']); + $this->assertSame('custom attribute', $picture->getRawSources()[0]['data-custom']); $this->assertSame('custom attribute', $picture->getSources('/')[0]['data-custom']); + } + + /** + * @group legacy + */ + public function testDeprecatedGetSources(): void + { + $picture = $this->createPicture(null, '/path/to/image.jpg'); + + $this->expectDeprecation('Passing NULL as $rootDir is deprecated%s'); + + $this->assertInstanceOf(ImageInterface::class, $picture->getSources()[0]['srcset'][0][0]); + $this->assertSame('custom attribute', $picture->getSources()[0]['data-custom']); $this->expectException(InvalidArgumentException::class); From 5a4e65643720fd5dfea44b3c5dfc3dd2b574521a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 12 May 2023 15:14:52 +0200 Subject: [PATCH 2/3] CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2cfa1d4..5783b64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.2 extensions: exif, dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib tools: prestissimo, flex coverage: pcov @@ -47,7 +47,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.1 + php-version: 8.2 extensions: exif, dom, fileinfo, filter, gd, hash, intl, json, mbstring, pcre, pdo, zlib tools: prestissimo, flex coverage: none From 6b4efb5424a6799342a7b3dce99478fbdb340162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Sat, 27 May 2023 18:08:08 +0200 Subject: [PATCH 3/3] Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6e3536..37ee6c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm * Add EXIF, IPTC, XMP metadata handling for JPEG, PNG, WEBP, GIF, HEIC, JXL, AVIF. [#93], [#95], [#96] * Improve file name hashing algorithm. [#90] * Fix wrong exception class being thrown. + * Add new methods `getRawImg()` and `getRawSources()` to Picture. [#98] ## [1.1.2] (2022-08-16) @@ -158,6 +159,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm [0.2.0]: https://github.com/contao/image/compare/0.1.0...0.2.0 [0.1.0]: https://github.com/contao/image/commits/0.1.0 +[#98]: https://github.com/contao/image/issues/98 [#96]: https://github.com/contao/image/issues/96 [#95]: https://github.com/contao/image/issues/95 [#93]: https://github.com/contao/image/issues/93