Skip to content

Commit aea2404

Browse files
committed
Merge branch 'MAGETWO-65480' into 2.1.6-develop-pr14
2 parents 2a9eee8 + 0c07794 commit aea2404

File tree

5 files changed

+177
-54
lines changed

5 files changed

+177
-54
lines changed

app/code/Magento/Catalog/Block/Product/ImageBlockBuilder.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@
88
use Magento\Catalog\Model\View\Asset\ImageFactory as AssetImageFactory;
99
use Magento\Framework\View\ConfigInterface;
1010
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
11-
use Magento\Framework\App\CacheInterface;
1211
use Magento\Catalog\Model\View\Asset\Image as AssetImage;
12+
use Magento\Catalog\Model\Product\Image\SizeCache;
1313

1414
/**
1515
* Used to build product image blocks in product list blocks.
1616
*/
1717
class ImageBlockBuilder
1818
{
19-
/**
20-
* @var string
21-
*/
22-
private $cachePrefix = 'IMG_INFO';
23-
2419
/**
2520
* @var ConfigInterface
2621
*/
@@ -42,50 +37,51 @@ class ImageBlockBuilder
4237
private $imageParamsBuilder;
4338

4439
/**
45-
* @var CacheInterface
40+
* @var SizeCache
4641
*/
47-
private $cache;
42+
private $sizeCache;
4843

4944
/**
5045
* @param ConfigInterface $presentationConfig
5146
* @param AssetImageFactory $viewAssetImageFactory
5247
* @param ImageFactory $imageBlockFactory
5348
* @param ParamsBuilder $imageParamsBuilder
54-
* @param CacheInterface $cache
49+
* @param SizeCache $sizeCache
5550
*/
5651
public function __construct(
5752
ConfigInterface $presentationConfig,
5853
AssetImageFactory $viewAssetImageFactory,
5954
ImageFactory $imageBlockFactory,
6055
ParamsBuilder $imageParamsBuilder,
61-
CacheInterface $cache
56+
SizeCache $sizeCache
6257
) {
6358
$this->presentationConfig = $presentationConfig->getViewConfig();
6459
$this->viewAssetImageFactory = $viewAssetImageFactory;
6560
$this->imageBlockFactory = $imageBlockFactory;
6661
$this->imageParamsBuilder = $imageParamsBuilder;
67-
$this->cache = $cache;
62+
$this->sizeCache = $sizeCache;
6863
}
6964

7065
/**
7166
* Get image size
7267
*
7368
* @param AssetImage $imageAsset
7469
* @return array
70+
* @throws \Exception
7571
*/
7672
private function getImageSize(AssetImage $imageAsset)
7773
{
78-
$key = $this->cachePrefix . $imageAsset->getPath();
79-
$size = $this->cache->load($key);
74+
$imagePath = $imageAsset->getPath();
75+
$size = $this->sizeCache->load($imagePath);
8076
if (!$size) {
81-
$size = getimagesize($imageAsset->getPath());
82-
$this->cache->save(
83-
serialize(['width' => $size[0], 'height' => $size[1]]),
84-
$key
85-
);
86-
} else {
87-
$size = unserialize($size);
77+
$size = getimagesize($imagePath);
78+
if (!$size) {
79+
throw new \Exception('An error occurred while reading file: ' . $imagePath);
80+
}
81+
$this->sizeCache->save($size[0], $size[1], $imagePath);
82+
$size = ['width' => $size[0], 'height' => $size[1]];
8883
}
84+
8985
return $size;
9086
}
9187

app/code/Magento/Catalog/Model/Product/Image.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
/**
8-
* Catalog product link model
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
11-
*/
126
namespace Magento\Catalog\Model\Product;
137

148
use Magento\Framework\App\Filesystem\DirectoryList;
159
use Magento\Framework\App\ObjectManager;
1610
use Magento\Framework\Image as MagentoImage;
1711
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
12+
use Magento\Catalog\Model\Product\Image\SizeCache;
1813

1914
/**
2015
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -26,11 +21,6 @@
2621
*/
2722
class Image extends \Magento\Framework\Model\AbstractModel
2823
{
29-
/**
30-
* @var string
31-
*/
32-
private $cachePrefix = 'IMG_INFO';
33-
3424
/**
3525
* @var int
3626
*/
@@ -196,6 +186,11 @@ class Image extends \Magento\Framework\Model\AbstractModel
196186
*/
197187
private $paramsBuilder;
198188

189+
/**
190+
* @var SizeCache
191+
*/
192+
private $sizeCache;
193+
199194
/**
200195
* @param \Magento\Framework\Model\Context $context
201196
* @param \Magento\Framework\Registry $registry
@@ -213,6 +208,7 @@ class Image extends \Magento\Framework\Model\AbstractModel
213208
* @param \Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory
214209
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory
215210
* @param ParamsBuilder $paramsBuilder
211+
* @param SizeCache $sizeCache
216212
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
217213
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
218214
*/
@@ -232,7 +228,8 @@ public function __construct(
232228
array $data = [],
233229
\Magento\Catalog\Model\View\Asset\ImageFactory $assetImageFactory = null,
234230
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $assetPlaceholderFactory = null,
235-
ParamsBuilder $paramsBuilder = null
231+
ParamsBuilder $paramsBuilder = null,
232+
SizeCache $sizeCache = null
236233
) {
237234
$this->_storeManager = $storeManager;
238235
$this->_catalogProductMediaConfig = $catalogProductMediaConfig;
@@ -256,6 +253,7 @@ public function __construct(
256253
);
257254
}
258255
$this->paramsBuilder = $paramsBuilder ?: ObjectManager::getInstance()->get(ParamsBuilder::class);
256+
$this->sizeCache = $sizeCache ?: ObjectManager::getInstance()->get(SizeCache::class);
259257
}
260258

261259
/**
@@ -678,23 +676,15 @@ public function saveFile()
678676
$filename = $this->getBaseFile() ? $this->imageAsset->getPath() : null;
679677
$this->getImageProcessor()->save($filename);
680678
$this->_coreFileStorageDatabase->saveFile($filename);
681-
$this->saveImageSize();
679+
$this->sizeCache->save(
680+
$this->getWidth(),
681+
$this->getHeight(),
682+
$this->imageAsset->getPath()
683+
);
682684

683685
return $this;
684686
}
685687

686-
/**
687-
* Save size to cache
688-
* @return void
689-
*/
690-
private function saveImageSize()
691-
{
692-
$this->_cacheManager->save(
693-
serialize(['width' => $this->getWidth(), 'height' => $this->getHeight()]),
694-
$this->cachePrefix . $this->imageAsset->getPath()
695-
);
696-
}
697-
698688
/**
699689
* @return string
700690
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Product\Image;
7+
8+
use Magento\Framework\App\CacheInterface;
9+
10+
/**
11+
* Image size cache
12+
*/
13+
class SizeCache
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $cachePrefix = 'IMG_INFO';
19+
20+
/**
21+
* Application Cache Manager
22+
*
23+
* @var CacheInterface
24+
*/
25+
protected $cacheManager;
26+
27+
public function __construct(
28+
CacheInterface $cacheManager
29+
) {
30+
$this->cacheManager = $cacheManager;
31+
}
32+
33+
/**
34+
* Save image size to cache
35+
*
36+
* @param int $width
37+
* @param int $height
38+
* @param string $path
39+
* @return void
40+
*/
41+
public function save($width, $height, $path)
42+
{
43+
$this->cacheManager->save(
44+
serialize(['width' => $width, 'height' => $height]),
45+
$this->cachePrefix . $path
46+
);
47+
}
48+
49+
/**
50+
* Load image size from cache
51+
*
52+
* @param string $path
53+
* @return array ['width' => '...', 'height' => '...']
54+
*/
55+
public function load($path)
56+
{
57+
$key = $this->cachePrefix . $path;
58+
$size = $this->cacheManager->load($key);
59+
$size = $size ? unserialize($size) : null;
60+
61+
return $size;
62+
}
63+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Model\Product\Image;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
use Magento\Catalog\Model\Product\Image\SizeCache;
10+
use Magento\Framework\App\CacheInterface;
11+
12+
class SizeCacheTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
16+
*/
17+
protected $objectManager;
18+
19+
/**
20+
* @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $cacheManager;
23+
/**
24+
* @var SizeCache
25+
*/
26+
protected $model;
27+
28+
protected function setUp()
29+
{
30+
$this->cacheManager = $this->getMockBuilder(CacheInterface::class)
31+
->getMockForAbstractClass();
32+
33+
$this->objectManager = new ObjectManager($this);
34+
$this->model = $this->objectManager->getObject(
35+
SizeCache::class,
36+
[
37+
'cacheManager' => $this->cacheManager,
38+
]
39+
);
40+
}
41+
42+
/**
43+
* Test save() method
44+
*/
45+
public function testSave()
46+
{
47+
$width = 100;
48+
$height = 100;
49+
$path = '\tmp\img.jpg';
50+
$this->cacheManager->expects($this->once())
51+
->method('save')
52+
->with(serialize(['width' => $width, 'height' => $height]), 'IMG_INFO' . $path)
53+
->willReturn(true);
54+
55+
$this->model->save($width, $height, $path);
56+
}
57+
58+
/**
59+
* Test load() method
60+
*/
61+
public function testLoad()
62+
{
63+
$path = '\tmp\img.jpg';
64+
$this->cacheManager->expects($this->once())
65+
->method('load')
66+
->with('IMG_INFO' . $path)
67+
->willReturn(null);
68+
69+
$this->model->load($path);
70+
}
71+
}

app/code/Magento/Catalog/Test/Unit/Model/Product/ImageTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\Filesystem\DirectoryList;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1515
use Magento\Framework\View\Asset\ContextInterface;
16+
use Magento\Catalog\Model\Product\Image\SizeCache;
1617

1718
/**
1819
* Class ImageTest
@@ -101,6 +102,11 @@ class ImageTest extends \PHPUnit_Framework_TestCase
101102
*/
102103
protected $paramsBuilder;
103104

105+
/**
106+
* @var SizeCache|\PHPUnit_Framework_MockObject_MockObject
107+
*/
108+
protected $sizeCache;
109+
104110
protected function setUp()
105111
{
106112
$this->context = $this->getMock(\Magento\Framework\Model\Context::class, [], [], '', false);
@@ -153,6 +159,10 @@ protected function setUp()
153159
->disableOriginalConstructor()
154160
->getMock();
155161

162+
$this->sizeCache = $this->getMockBuilder(SizeCache::class)
163+
->disableOriginalConstructor()
164+
->getMock();
165+
156166
$this->image = new \Magento\Catalog\Model\Product\Image(
157167
$context,
158168
$this->registry,
@@ -169,7 +179,8 @@ protected function setUp()
169179
[],
170180
$this->viewAssetImageFactory,
171181
$this->viewAssetPlaceholderFactory,
172-
$this->paramsBuilder
182+
$this->paramsBuilder,
183+
$this->sizeCache
173184
);
174185

175186
//Settings for backward compatible property
@@ -182,14 +193,6 @@ protected function setUp()
182193
'imageAsset',
183194
$this->imageAsset
184195
);
185-
$cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
186-
->disableOriginalConstructor()
187-
->getMockForAbstractClass();
188-
$objectManagerHelper->setBackwardCompatibleProperty(
189-
$this->image,
190-
'_cacheManager',
191-
$cacheManager
192-
);
193196
}
194197

195198
public function testSetGetQuality()

0 commit comments

Comments
 (0)