-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from discoverygarden/feature/deferred-search-a…
…pi-resolution DDST-271: Feature/deferred search api resolution
- Loading branch information
Showing
31 changed files
with
943 additions
and
52 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,8 @@ | |
"license": "GPL-3.0-only", | ||
"require": { | ||
"drupal/search_api": "^1.19" | ||
}, | ||
"conflict": { | ||
"drupal/core": "<10.2" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
dgi_image_discovery.deferred_resolution: | ||
path: '/node/{node}/dgi_image_discovery/{style}' | ||
defaults: | ||
_controller: 'dgi_image_discovery.deferred_resolution_controller:resolve' | ||
requirements: | ||
_entity_access: node.view | ||
options: | ||
parameters: | ||
style: | ||
type: entity:image_style | ||
node: | ||
type: entity:node |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\dgi_image_discovery\Attribute; | ||
|
||
use Drupal\Component\Plugin\Attribute\AttributeBase; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
|
||
/** | ||
* The deferred_resolution attribute. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class DeferredResolution extends AttributeBase { | ||
|
||
/** | ||
* Constructs a new DgiImageDiscoveryUrlGenerator instance. | ||
* | ||
* @param string $id | ||
* The plugin ID. There are some implementation bugs that make the plugin | ||
* available only if the ID follows a specific pattern. It must be either | ||
* identical to group or prefixed with the group. E.g. if the group is "foo" | ||
* the ID must be either "foo" or "foo:bar". | ||
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label | ||
* (optional) The human-readable name of the plugin. | ||
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description | ||
* (optional) A brief description of the plugin. | ||
* @param class-string|null $deriver | ||
* (optional) The deriver class. | ||
*/ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly ?TranslatableMarkup $label, | ||
public readonly ?TranslatableMarkup $description = NULL, | ||
public readonly ?string $deriver = NULL, | ||
) {} | ||
|
||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\dgi_image_discovery\Attribute; | ||
|
||
use Drupal\Component\Plugin\Attribute\AttributeBase; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
|
||
/** | ||
* The dgi_image_discovery__url_generator attribute. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class UrlGenerator extends AttributeBase { | ||
|
||
/** | ||
* Constructs a new DgiImageDiscoveryUrlGenerator instance. | ||
* | ||
* @param string $id | ||
* The plugin ID. There are some implementation bugs that make the plugin | ||
* available only if the ID follows a specific pattern. It must be either | ||
* identical to group or prefixed with the group. E.g. if the group is "foo" | ||
* the ID must be either "foo" or "foo:bar". | ||
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $label | ||
* (optional) The human-readable name of the plugin. | ||
* @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description | ||
* (optional) A brief description of the plugin. | ||
* @param class-string|null $deriver | ||
* (optional) The deriver class. | ||
*/ | ||
public function __construct( | ||
public readonly string $id, | ||
public readonly ?TranslatableMarkup $label, | ||
public readonly ?TranslatableMarkup $description = NULL, | ||
public readonly ?string $deriver = NULL, | ||
) {} | ||
|
||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace Drupal\dgi_image_discovery; | ||
|
||
use Drupal\Core\Cache\CacheableResponseInterface; | ||
use Drupal\Core\Cache\CacheableResponseTrait; | ||
use Drupal\Core\DependencyInjection\DependencySerializationTrait; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
||
/** | ||
* Cacheable binary file response. | ||
* | ||
* Loosely adapted from | ||
* https://www.drupal.org/project/drupal/issues/3227041#comment-15335922 | ||
*/ | ||
class CacheableBinaryFileResponse extends BinaryFileResponse implements CacheableResponseInterface { | ||
|
||
use CacheableResponseTrait; | ||
use DependencySerializationTrait { | ||
__sleep as traitSleep; | ||
__wakeup as traitWakeup; | ||
} | ||
|
||
/** | ||
* Serializable reference to the file. | ||
* | ||
* @var string | ||
*/ | ||
protected string $uri; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setFile(\SplFileInfo|string $file, ?string $contentDisposition = NULL, bool $autoEtag = FALSE, bool $autoLastModified = TRUE): static { | ||
$this->uri = $file instanceof \SplFileInfo ? $file->getPathname() : $file; | ||
return parent::setFile($file, $contentDisposition, $autoEtag, $autoLastModified); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __sleep() { | ||
return array_diff($this->traitSleep(), [ | ||
'file', | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __wakeup() : void { | ||
$this->traitWakeup(); | ||
$this->setFile($this->uri); | ||
} | ||
|
||
/** | ||
* Convert a BinaryFileResponse into a CacheableBinaryFileResponse. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\BinaryFileResponse $response | ||
* The response to convert. | ||
* | ||
* @return static | ||
* The converted response. | ||
*/ | ||
public static function convert(BinaryFileResponse $response) : static { | ||
return new static( | ||
$response->getFile(), | ||
$response->getStatusCode(), | ||
$response->headers->all(), | ||
/* $public, $contentDisposition, $autoEtag, $autoLastModified all accounted for in headers */ | ||
); | ||
} | ||
|
||
} |
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,78 @@ | ||
<?php | ||
|
||
namespace Drupal\dgi_image_discovery\Controller; | ||
|
||
use Drupal\Core\Cache\CacheableResponse; | ||
use Drupal\Core\Cache\CacheableResponseInterface; | ||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\Core\Http\Exception\CacheableHttpException; | ||
use Drupal\Core\Render\RenderContext; | ||
use Drupal\Core\Render\RendererInterface; | ||
use Drupal\dgi_image_discovery\DeferredResolutionPluginManagerInterface; | ||
use Drupal\image\ImageStyleInterface; | ||
use Drupal\node\NodeInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Deferred image URL resolution controller. | ||
*/ | ||
class DeferredResolutionController implements ContainerInjectionInterface { | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct( | ||
protected DeferredResolutionPluginManagerInterface $deferredResolutionPluginManager, | ||
protected RendererInterface $renderer, | ||
) {} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('plugin.manager.dgi_image_discovery.url_generator.deferred'), | ||
$container->get('renderer'), | ||
); | ||
} | ||
|
||
/** | ||
* Resolve image for the given node and style. | ||
* | ||
* @param \Drupal\image\ImageStyleInterface $style | ||
* The style of image to get. | ||
* @param \Drupal\node\NodeInterface $node | ||
* The node of which to get an image. | ||
* | ||
* @return \Drupal\Core\Cache\CacheableResponseInterface | ||
* A cacheable response. | ||
* | ||
* @throws \Drupal\Component\Plugin\Exception\PluginException | ||
*/ | ||
public function resolve(ImageStyleInterface $style, NodeInterface $node) : CacheableResponseInterface { | ||
$context = new RenderContext(); | ||
/** @var \Drupal\Core\Cache\CacheableResponseInterface $response */ | ||
$response = $this->renderer->executeInRenderContext($context, function () use ($style, $node) { | ||
// @todo Make plugin configurable? | ||
/** @var \Drupal\dgi_image_discovery\DeferredResolutionInterface $plugin */ | ||
$plugin = $this->deferredResolutionPluginManager->createInstance(getenv('DGI_IMAGE_DISCOVERY_DEFERRED_PLUGIN') ?: 'subrequest'); | ||
|
||
try { | ||
return $plugin->resolve($node, $style); | ||
} | ||
catch (CacheableHttpException $e) { | ||
return (new CacheableResponse($e->getMessage(), $e->getStatusCode())) | ||
->addCacheableDependency($e); | ||
} | ||
}); | ||
|
||
if (!$context->isEmpty()) { | ||
$metadata = $context->pop(); | ||
$response->addCacheableDependency($metadata); | ||
} | ||
|
||
return $response; | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\dgi_image_discovery; | ||
|
||
use Drupal\Core\Cache\CacheableResponseInterface; | ||
use Drupal\image\ImageStyleInterface; | ||
use Drupal\node\NodeInterface; | ||
|
||
/** | ||
* Interface for deferred_resolution plugins. | ||
*/ | ||
interface DeferredResolutionInterface { | ||
|
||
/** | ||
* Returns the translated plugin label. | ||
*/ | ||
public function label(): string; | ||
|
||
/** | ||
* Generate URL for the given node/style. | ||
* | ||
* @param \Drupal\node\NodeInterface $node | ||
* The node for which to generate a URL. | ||
* @param \Drupal\image\ImageStyleInterface $style | ||
* The style which the URL should return. | ||
* | ||
* @return \Drupal\Core\Cache\CacheableResponseInterface | ||
* Cacheable resolution response. | ||
*/ | ||
public function resolve(NodeInterface $node, ImageStyleInterface $style): CacheableResponseInterface; | ||
|
||
} |
Oops, something went wrong.