-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DDST-271: Feature/deferred search api resolution #24
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7e5f655
Misc pre-existing coding standards issues.
adam-vessey 4e070d1
Theoretical plugin definitions for deferring image generation.
adam-vessey ff08d17
DRYing up, drop `final` and adjust some comments.
adam-vessey 56bd4db
Basic integration.
adam-vessey ad8cb5b
Get subrequest stuff working.
adam-vessey 77651cc
Merge remote-tracking branch 'origin/main' into feature/deferred-sear…
adam-vessey 12934b9
Flesh out README some more.
adam-vessey 33f6de0
Slight simplification.
adam-vessey 08dc5f5
The use of attribute-based plugins was only introduced with 10.2.
adam-vessey d0db017
Throw exceptions when failing lookup an image, to instead 404.
adam-vessey 58b3034
Merge remote-tracking branch 'origin/feature/deferred-search-api-reso…
adam-vessey 31961c3
Cache/simplify error response.
adam-vessey 184b392
Use more specific exception, and prevent explosions with pre-generation.
adam-vessey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,70 @@ | ||
<?php | ||
|
||
namespace Drupal\dgi_image_discovery\Controller; | ||
|
||
use Drupal\Core\Cache\CacheableResponseInterface; | ||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
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) { | ||
adam-vessey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// @todo Make plugin configurable? | ||
/** @var \Drupal\dgi_image_discovery\DeferredResolutionInterface $plugin */ | ||
$plugin = $this->deferredResolutionPluginManager->createInstance(getenv('DGI_IMAGE_DISCOVERY_DEFERRED_PLUGIN') ?: 'subrequest'); | ||
|
||
return $plugin->resolve($node, $style); | ||
}); | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Largely inconsequential, but should've been in the pre-existing coding standards commit; however, I spec'd the extensions as
php
, so it didn't touch the.module
... whoops! Anyway.