Skip to content

Commit

Permalink
simplify getting candidates + remove duplicate check
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vo committed Sep 28, 2020
1 parent b73ba71 commit 4f30e2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 50 deletions.
48 changes: 15 additions & 33 deletions core-bundle/src/Image/Studio/ImageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,21 @@ public function createIfDeferred(): void
{
$picture = $this->getPicture();

$array_flatten = static function (array $array): array {
return array_reduce($array, 'array_merge', []);
};

$findDeferredImages = static function (array $item) use ($array_flatten): array {
$candidates = array_merge(
[$item['src'] ?? null],
$array_flatten($item['srcset'] ?? [])
);

return array_filter(
$candidates,
static function ($image): bool {
return $image instanceof DeferredImageInterface;
}
);
};

// Create an array of deferred images by looking through the 'src' and
// 'srcset' attributes of both the 'img' and all 'sources' tags.
$deferredImages = array_merge(
$findDeferredImages($picture->getImg()),
$array_flatten(array_map($findDeferredImages, $picture->getSources()))
$candidates = [];

foreach (array_merge([$picture->getImg()], $picture->getSources()) as $source) {
$candidates[] = $source['src'] ?? null;

foreach ($source['srcset'] ?? [] as $srcset) {
$candidates[] = $srcset[0] ?? null;
}
}

$deferredImages = array_filter(
$candidates,
static function ($image): bool {
return $image instanceof DeferredImageInterface;
}
);

if (empty($deferredImages)) {
Expand All @@ -191,17 +183,7 @@ static function ($image): bool {
throw new \RuntimeException('The "contao.image.resizer" service does not support deferred resizing.');
}

$resizedPaths = [];

/** @var DeferredImageInterface $deferredImage */
foreach ($deferredImages as $deferredImage) {
// Skip already processed images
if (isset($resizedPaths[$deferredImage->getPath()])) {
continue;
}

$resizedPaths[$deferredImage->getPath()] = true;

$resizer->resizeDeferredImage($deferredImage);
}
}
Expand Down
25 changes: 8 additions & 17 deletions core-bundle/tests/Image/Studio/ImageResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,16 @@ public function testCreateIfDeferred(array $img, array $sources, array $expected

$deferredResizer = $this->createMock(DeferredResizerInterface::class);
$deferredResizer
->expects($this->exactly(\count($expectedDeferredImages)))
->expects(
empty($expectedDeferredImages) ?
$this->never() : $this->atLeast(\count($expectedDeferredImages))
)
->method('resizeDeferredImage')
->with($this->callback(
static function ($deferredImage) use (&$expectedDeferredImages) {
unset($expectedDeferredImages[array_search($deferredImage, $expectedDeferredImages, true)]);
if (false !== ($key = array_search($deferredImage, $expectedDeferredImages, true))) {
unset($expectedDeferredImages[$key]);
}

return true;
}
Expand Down Expand Up @@ -273,21 +278,7 @@ public function provideDeferredImages(): \Generator
[],
];

yield 'img and sources with deferred images (without duplicates)' => [
[
'src' => $deferredImage1,
'srcset' => [[$deferredImage2, 'foo']],
],
[
[
'src' => $deferredImage3,
'srcset' => [[$deferredImage4]],
],
],
[$deferredImage1, $deferredImage2, $deferredImage3, $deferredImage4],
];

yield 'img and sources with deferred images (with duplicates)' => [
yield 'img and sources with deferred images' => [
[
'src' => $deferredImage1,
'srcset' => [[$deferredImage2, 'foo'], [$deferredImage3]],
Expand Down

0 comments on commit 4f30e2f

Please sign in to comment.