Skip to content

Commit eaa4b87

Browse files
committed
updated new files from master
1 parent 187edfd commit eaa4b87

File tree

14 files changed

+188
-38
lines changed

14 files changed

+188
-38
lines changed

Config/Config.php

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
namespace Lof\Webp2\Config;
55

66
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Framework\Exception\NoSuchEntityException;
78
use Magento\Framework\View\Element\Block\ArgumentInterface;
9+
use Magento\Store\Model\ScopeInterface;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Lof\Webp2\Exception\InvalidConvertorException;
812

913
class Config implements ArgumentInterface
1014
{
@@ -13,31 +17,39 @@ class Config implements ArgumentInterface
1317
*/
1418
private $scopeConfig;
1519

20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
1625
/**
1726
* Config constructor.
1827
*
1928
* @param ScopeConfigInterface $scopeConfig
29+
* @param StoreManagerInterface $storeManager
2030
*/
2131
public function __construct(
22-
ScopeConfigInterface $scopeConfig
32+
ScopeConfigInterface $scopeConfig,
33+
StoreManagerInterface $storeManager
2334
) {
2435
$this->scopeConfig = $scopeConfig;
36+
$this->storeManager = $storeManager;
2537
}
2638

2739
/**
2840
* @return bool
2941
*/
3042
public function enabled(): bool
3143
{
32-
return (bool)$this->scopeConfig->getValue('lof_webp2/settings/enabled');
44+
return (bool)$this->getValue('lof_webp2/settings/enabled');
3345
}
3446

3547
/**
3648
* @return int
3749
*/
3850
public function getQualityLevel(): int
3951
{
40-
$qualityLevel = (int)$this->scopeConfig->getValue('lof_webp2/settings/quality_level');
52+
$qualityLevel = (int)$this->getValue('lof_webp2/settings/quality_level');
4153
if ($qualityLevel > 100) {
4254
return 100;
4355
}
@@ -51,9 +63,77 @@ public function getQualityLevel(): int
5163

5264
/**
5365
* @return string[]
66+
* @throws InvalidConvertorException
5467
*/
5568
public function getConvertors(): array
5669
{
57-
return ['cwebp', 'gd', 'imagick', 'wpc', 'ewww'];
70+
$allConvertors = ['cwebp', 'gd', 'imagick', 'wpc', 'ewww'];
71+
$storedConvertors = $this->getValue('lof_webp2/settings/convertors');
72+
$storedConvertors = $this->stringToArray((string)$storedConvertors);
73+
if (empty($storedConvertors)) {
74+
return $allConvertors;
75+
}
76+
77+
foreach ($storedConvertors as $storedConvertor) {
78+
if (!in_array($storedConvertor, $allConvertors)) {
79+
throw new InvalidConvertorException('Invalid convertor: "' . $storedConvertor . '"');
80+
}
81+
}
82+
83+
return $storedConvertors;
84+
}
85+
86+
/**
87+
* @return string
88+
* @throws InvalidConvertorException
89+
*/
90+
public function getEncoding(): string
91+
{
92+
$allEncoding = ['lossy', 'lossless', 'auto'];
93+
$storedEncoding = (string)$this->getValue('lof_webp2/settings/encoding');
94+
if (empty($storedEncoding)) {
95+
return 'lossy';
96+
}
97+
98+
if (!in_array($storedEncoding, $allEncoding)) {
99+
throw new InvalidConvertorException('Invalid encoding: "' . $storedEncoding . '"');
100+
}
101+
102+
return $storedEncoding;
103+
}
104+
105+
/**
106+
* @param string $path
107+
* @return mixed
108+
*/
109+
private function getValue(string $path)
110+
{
111+
try {
112+
return $this->scopeConfig->getValue(
113+
$path,
114+
ScopeInterface::SCOPE_STORE,
115+
$this->storeManager->getStore()
116+
);
117+
} catch (NoSuchEntityException $e) {
118+
return null;
119+
}
120+
}
121+
122+
/**
123+
* @param string $string
124+
* @return array
125+
*/
126+
private function stringToArray(string $string): array
127+
{
128+
$array = [];
129+
$strings = explode(',', $string);
130+
foreach ($strings as $string) {
131+
$string = trim($string);
132+
if ($string) {
133+
$array[] = $string;
134+
}
135+
}
136+
137+
return $array;
58138
}
59139
}

Image/ConvertWrapper.php renamed to Convertor/ConvertWrapper.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Lof\Webp2\Image;
4+
namespace Lof\Webp2\Convertor;
55

6+
use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\InvalidImageTypeException;
67
use WebPConvert\Convert\Exceptions\ConversionFailedException;
78
use WebPConvert\WebPConvert;
89
use Lof\Webp2\Config\Config;
10+
use Lof\Webp2\Exception\InvalidConvertorException;
911

1012
/**
1113
* Class ConvertWrapper to wrap third party wrapper for purpose of preference overrides and testing
@@ -31,21 +33,25 @@ public function __construct(
3133
* @param string $sourceImageFilename
3234
* @param string $destinationImageFilename
3335
* @throws ConversionFailedException
36+
* @throws InvalidConvertorException
37+
* @throws InvalidImageTypeException
3438
*/
3539
public function convert(string $sourceImageFilename, string $destinationImageFilename): void
3640
{
3741
WebPConvert::convert($sourceImageFilename, $destinationImageFilename, $this->getOptions());
3842
}
3943

4044
/**
41-
* @return mixed[]
45+
* @return array
46+
* @throws InvalidConvertorException
4247
*/
4348
public function getOptions(): array
4449
{
4550
return [
4651
'quality' => 'auto',
4752
'max-quality' => $this->config->getQualityLevel(),
4853
'converters' => $this->config->getConvertors(),
54+
'encoding' => $this->config->getEncoding(),
4955
];
5056
}
5157
}

Convertor/Convertor.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Lof\Webp2\Convertor;
44

5+
use Magento\Framework\Exception\FileSystemException;
6+
use Magento\Framework\Exception\NoSuchEntityException;
57
use Magento\Framework\Filesystem\Driver\File as FileDriver;
68
use Magento\Framework\Filesystem\File\ReadFactory as FileReadFactory;
9+
use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\InvalidImageTypeException;
710
use WebPConvert\Convert\Exceptions\ConversionFailedException;
811
use Lof\NextGenImages\Convertor\ConvertorInterface;
912
use Lof\NextGenImages\Exception\ConvertorException;
@@ -12,7 +15,8 @@
1215
use Lof\NextGenImages\Image\SourceImageFactory;
1316
use Lof\NextGenImages\Logger\Debugger;
1417
use Lof\Webp2\Config\Config;
15-
use Lof\Webp2\Image\ConvertWrapper;
18+
use Lof\Webp2\Exception\InvalidConvertorException;
19+
use WebPConvert\Exceptions\InvalidInput\InvalidImageTypeException as InvalidInputImageTypeException;
1620

1721
class Convertor implements ConvertorInterface
1822
{
@@ -83,6 +87,8 @@ public function __construct(
8387
* @param string $imageUrl
8488
* @return SourceImage
8589
* @throws ConvertorException
90+
* @throws FileSystemException
91+
* @throws NoSuchEntityException
8692
* @deprecated Use getSourceImage() instead
8793
*/
8894
public function convertByUrl(string $imageUrl): SourceImage
@@ -94,6 +100,8 @@ public function convertByUrl(string $imageUrl): SourceImage
94100
* @param string $imageUrl
95101
* @return SourceImage
96102
* @throws ConvertorException
103+
* @throws FileSystemException
104+
* @throws NoSuchEntityException
97105
*/
98106
public function getSourceImage(string $imageUrl): SourceImage
99107
{
@@ -126,10 +134,10 @@ public function convert(string $sourceImageUri, ?string $destinationImageUri = n
126134
$sourceImageFilename = $this->imageFile->resolve($sourceImageUri);
127135
$destinationImageFilename = $this->imageFile->resolve($destinationImageUri);
128136

129-
if(!$this->imageFile->fileExists($sourceImageFilename)) {
137+
if (!$this->imageFile->fileExists($sourceImageFilename)) {
130138
throw new ConvertorException('Source cached image does not exists ' . $sourceImageUri);
131139
}
132-
140+
133141
if (!$this->imageFile->needsConversion($sourceImageFilename, $destinationImageFilename)) {
134142
return true;
135143
}
@@ -140,7 +148,9 @@ public function convert(string $sourceImageUri, ?string $destinationImageUri = n
140148

141149
try {
142150
$this->convertWrapper->convert($sourceImageFilename, $destinationImageFilename);
143-
} catch (ConversionFailedException $e) {
151+
} catch (InvalidImageTypeException | InvalidInputImageTypeException $e) {
152+
return false;
153+
} catch (ConversionFailedException | InvalidConvertorException $e) {
144154
throw new ConvertorException($destinationImageFilename . ': ' . $e->getMessage());
145155
}
146156

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lof\Webp2\Exception;
4+
5+
use Exception;
6+
7+
class InvalidConvertorException extends Exception
8+
{
9+
}

Model/Config/Source/Encoding.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lof\Webp2\Model\Config\Source;
4+
5+
use Magento\Framework\Data\OptionSourceInterface;
6+
7+
class Encoding implements OptionSourceInterface
8+
{
9+
public function toOptionArray()
10+
{
11+
return [
12+
['value' => 'lossy', 'label' => 'Lossy'],
13+
['value' => 'lossless', 'label' => 'Lossless'],
14+
['value' => 'auto', 'label' => 'Auto (both lossy and lossless)'],
15+
];
16+
}
17+
}

Plugin/AddWebpToGalleryImagesJson.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ public function afterGetGalleryImagesJson(Gallery $subject, string $galleryImage
5959
private function appendImages(array $images): array
6060
{
6161
foreach ($images as $id => $imageData) {
62-
$imageData['thumb_webp'] = $this->getWebpUrl($imageData['thumb']);
63-
$imageData['img_webp'] = $this->getWebpUrl($imageData['img']);
64-
$imageData['full_webp'] = $this->getWebpUrl($imageData['full']);
62+
foreach (['thumb', 'img', 'full'] as $imageType) {
63+
if (empty($imageData[$imageType])) {
64+
continue;
65+
}
66+
$imageData["{$imageType}_webp"] = $this->getWebpUrl($imageData[$imageType]);
67+
}
6568
$images[$id] = $imageData;
6669
}
6770

Plugin/AddWebpToSwatchesAjaxData.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function afterGetProductMediaGallery(SwatchesDataHelper $subject, array $
4848
$images[$type . '_webp'] = $this->getWebpUrl($images[$type]);
4949
}
5050

51+
if (!isset($images['gallery'])) {
52+
return $images;
53+
}
54+
5155
foreach ($images['gallery'] as $galleryIndex => $galleryImages) {
5256
foreach ($types as $type) {
5357
if (!isset($images[$type])) {

Test/Integration/Common.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\View\LayoutInterface;
1010
use Magento\TestFramework\TestCase\AbstractController;
1111
use RuntimeException;
12-
use Lof\Webp2\Image\ConvertWrapper;
12+
use Lof\Webp2\Convertor\ConvertWrapper;
1313
use Lof\Webp2\Test\Utils\ImageProvider;
1414
use Lof\Webp2\Test\Utils\ConvertWrapperStub;
1515

@@ -70,8 +70,9 @@ protected function getImageProvider(): ImageProvider
7070

7171
/**
7272
* @param string $body
73+
* @param array $images
7374
*/
74-
protected function assertImageTagsExist(string $body, $images)
75+
protected function assertImageTagsExist(string $body, array $images)
7576
{
7677
foreach ($images as $image) {
7778
$webPImage = preg_replace('/\.(png|jpg)$/', '.webp', $image);

Test/Unit/Config/ConfigTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lof\Webp2\Test\Unit\Config;
44

55
use Magento\Framework\App\Config\ScopeConfigInterface;
6+
use Magento\Store\Model\StoreManagerInterface;
67
use PHPUnit\Framework\TestCase;
78
use Lof\Webp2\Config\Config;
89

@@ -11,7 +12,8 @@ class ConfigTest extends TestCase
1112
public function testEnabled()
1213
{
1314
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
14-
$config = new Config($scopeConfig);
15+
$storeManager = $this->createMock(StoreManagerInterface::class);
16+
$config = new Config($scopeConfig, $storeManager);
1517
$this->assertFalse($config->enabled());
1618

1719
$scopeConfig->method('getValue')->willReturn(1);
@@ -21,29 +23,31 @@ public function testEnabled()
2123
public function testGetQualityLevel()
2224
{
2325
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
24-
$config = new Config($scopeConfig);
26+
$storeManager = $this->createMock(StoreManagerInterface::class);
27+
$config = new Config($scopeConfig, $storeManager);
2528
$this->assertEquals(1, $config->getQualityLevel());
2629

2730
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
28-
$config = new Config($scopeConfig);
31+
$config = new Config($scopeConfig, $storeManager);
2932
$scopeConfig->method('getValue')->willReturn(42);
3033
$this->assertEquals(42, $config->getQualityLevel());
3134

3235
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
33-
$config = new Config($scopeConfig);
3436
$scopeConfig->method('getValue')->willReturn(142);
37+
$config = new Config($scopeConfig, $storeManager);
3538
$this->assertEquals(100, $config->getQualityLevel());
3639

3740
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
38-
$config = new Config($scopeConfig);
3941
$scopeConfig->method('getValue')->willReturn(0);
42+
$config = new Config($scopeConfig, $storeManager);
4043
$this->assertEquals(1, $config->getQualityLevel());
4144
}
4245

4346
public function testGetConvertors()
4447
{
4548
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
46-
$config = new Config($scopeConfig);
49+
$storeManager = $this->createMock(StoreManagerInterface::class);
50+
$config = new Config($scopeConfig, $storeManager);
4751
$this->assertNotEmpty($config->getConvertors());
4852
}
4953
}

Test/Unit/Convertor/ConvertorTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Lof\NextGenImages\Logger\Debugger;
1313
use Lof\Webp2\Config\Config;
1414
use Lof\Webp2\Convertor\Convertor;
15-
use Lof\Webp2\Image\ConvertWrapper;
15+
use Lof\Webp2\Convertor\ConvertWrapper;
1616

1717
class ConvertorTest extends TestCase
1818
{
@@ -35,16 +35,8 @@ public function testGetSourceImage()
3535
public function testConvert()
3636
{
3737
$convertor = $this->getConvertor();
38-
$this->assertFalse($convertor->convert('/images/test.jpg', '/images/test.webp'));
39-
}
40-
41-
/**
42-
* Test for Lof\Webp2\Convertor\Convertor::urlExists
43-
*/
44-
public function testUrlExists()
45-
{
46-
$convertor = $this->getConvertor();
47-
$this->assertFalse($convertor->urlExists('http://localhost/test.webp'));
38+
$this->expectException(ConvertorException::class);
39+
$convertor->convert('/images/test.jpg', '/images/test.webp');
4840
}
4941

5042
/**

0 commit comments

Comments
 (0)