forked from ezsystems/ezpublish-kernel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-29137: As a developer, I want to replace missing images with plac…
…eholders (ezsystems#2311) * EZP-29137: As a developer, I want to replace missing images with placeholders * EZP-29137: As a developer, I want to replace missing images with placeholders (impl. generic placeholder provider) * EZP-29137: As a developer, I want to replace missing images with placeholders (impl. remote placeholder provider) * EZP-29137: As a developer, I want to replace missing images with placeholders (configuration)
- Loading branch information
Showing
18 changed files
with
1,102 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
eZ/Bundle/EzPublishCoreBundle/DependencyInjection/Compiler/PlaceholderProviderPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the eZ Publish Kernel package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler; | ||
|
||
use LogicException; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class PlaceholderProviderPass implements CompilerPassInterface | ||
{ | ||
const TAG_NAME = 'ezpublish.placeholder_provider'; | ||
const REGISTRY_DEFINITION_ID = 'ezpublish.image_alias.imagine.placeholder_provider.registry'; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition(self::REGISTRY_DEFINITION_ID)) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition(self::REGISTRY_DEFINITION_ID); | ||
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $id => $attributes) { | ||
foreach ($attributes as $attribute) { | ||
if (!isset($attribute['type'])) { | ||
throw new LogicException(self::TAG_NAME . ' service tag needs a "type" attribute to identify the placeholder provider type. None given.'); | ||
} | ||
|
||
$definition->addMethodCall( | ||
'addProvider', | ||
[$attribute['type'], new Reference($id)] | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
eZ/Bundle/EzPublishCoreBundle/Imagine/PlaceholderAliasGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the eZ Publish Kernel package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace eZ\Bundle\EzPublishCoreBundle\Imagine; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\API\Repository\Values\Content\VersionInfo; | ||
use eZ\Publish\Core\FieldType\Image\Value as ImageValue; | ||
use eZ\Publish\Core\FieldType\Value; | ||
use eZ\Publish\Core\IO\IOServiceInterface; | ||
use eZ\Publish\SPI\Variation\VariationHandler; | ||
use InvalidArgumentException; | ||
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException; | ||
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; | ||
|
||
class PlaceholderAliasGenerator implements VariationHandler | ||
{ | ||
/** | ||
* @var \eZ\Publish\SPI\Variation\VariationHandler | ||
*/ | ||
private $aliasGenerator; | ||
|
||
/** | ||
* @var \Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface | ||
*/ | ||
private $ioResolver; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\IO\IOServiceInterface | ||
*/ | ||
private $ioService; | ||
|
||
/** | ||
* @var \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider|null | ||
*/ | ||
private $placeholderProvider; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $placeholderOptions = []; | ||
|
||
/** | ||
* PlaceholderAliasGenerator constructor. | ||
* | ||
* @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator | ||
* @param \Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface $ioResolver | ||
* @param \eZ\Publish\Core\IO\IOServiceInterface $ioService | ||
*/ | ||
public function __construct( | ||
VariationHandler $aliasGenerator, | ||
ResolverInterface $ioResolver, | ||
IOServiceInterface $ioService) | ||
{ | ||
$this->aliasGenerator = $aliasGenerator; | ||
$this->ioResolver = $ioResolver; | ||
$this->ioService = $ioService; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = []) | ||
{ | ||
if ($this->placeholderProvider !== null) { | ||
/** @var \eZ\Publish\Core\FieldType\Image\Value $imageValue */ | ||
$imageValue = $field->value; | ||
if (!$this->supportsValue($imageValue)) { | ||
throw new InvalidArgumentException("Value for field #{$field->id} ($field->fieldDefIdentifier) cannot be used for image placeholder generation."); | ||
} | ||
|
||
try { | ||
$this->ioResolver->resolve($imageValue->id, IORepositoryResolver::VARIATION_ORIGINAL); | ||
} catch (NotResolvableException $e) { | ||
// Generate placeholder for original image | ||
$binary = $this->ioService->newBinaryCreateStructFromLocalFile( | ||
$this->placeholderProvider->getPlaceholder($imageValue, $this->placeholderOptions) | ||
); | ||
$binary->id = $imageValue->id; | ||
|
||
$this->ioService->createBinaryFile($binary); | ||
} | ||
} | ||
|
||
return $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters); | ||
} | ||
|
||
public function setPlaceholderProvider(PlaceholderProvider $provider, array $options = []) | ||
{ | ||
$this->placeholderProvider = $provider; | ||
$this->placeholderOptions = $options; | ||
} | ||
|
||
public function supportsValue(Value $value): bool | ||
{ | ||
return $value instanceof ImageValue; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
eZ/Bundle/EzPublishCoreBundle/Imagine/PlaceholderAliasGeneratorConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the eZ Publish Kernel package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace eZ\Bundle\EzPublishCoreBundle\Imagine; | ||
|
||
use eZ\Publish\Core\MVC\ConfigResolverInterface; | ||
|
||
class PlaceholderAliasGeneratorConfigurator | ||
{ | ||
/** | ||
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface | ||
*/ | ||
private $configResolver; | ||
|
||
/** | ||
* @var \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProviderRegistry | ||
*/ | ||
private $providerRegistry; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $providersConfig; | ||
|
||
/** | ||
* PlaceholderAliasGeneratorConfigurator constructor. | ||
* | ||
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver | ||
* @param \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProviderRegistry $providerRegistry | ||
* @param array $providersConfig | ||
*/ | ||
public function __construct(ConfigResolverInterface $configResolver, PlaceholderProviderRegistry $providerRegistry, array $providersConfig) | ||
{ | ||
$this->configResolver = $configResolver; | ||
$this->providerRegistry = $providerRegistry; | ||
$this->providersConfig = $providersConfig; | ||
} | ||
|
||
public function configure(PlaceholderAliasGenerator $generator) | ||
{ | ||
$binaryHandlerName = $this->configResolver->getParameter('io.binarydata_handler'); | ||
|
||
if (isset($this->providersConfig[$binaryHandlerName])) { | ||
$providersConfig = $this->providersConfig[$binaryHandlerName]; | ||
|
||
$provider = $this->providerRegistry->getProvider($providersConfig['provider']); | ||
$generator->setPlaceholderProvider($provider, $providersConfig['options']); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
eZ/Bundle/EzPublishCoreBundle/Imagine/PlaceholderProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* This file is part of the eZ Publish Kernel package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace eZ\Bundle\EzPublishCoreBundle\Imagine; | ||
|
||
use eZ\Publish\Core\FieldType\Image\Value as ImageValue; | ||
|
||
interface PlaceholderProvider | ||
{ | ||
/** | ||
* Provides a placeholder image path for a given Image FieldType value. | ||
* | ||
* @param \eZ\Publish\Core\FieldType\Image\Value $value | ||
* @param array $options | ||
* @return string Path to placeholder | ||
*/ | ||
public function getPlaceholder(ImageValue $value, array $options = []): string; | ||
} |
Oops, something went wrong.