Skip to content
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

Add GotenbergAssetRuntime to avoid passing Builder in context #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

smnandre
Copy link
Contributor

Passing the builder in the Twig context is a bit fragile.

It won't work when template uses any of Twig layout features (includes, embed, macros, ...) without passing the full context.

Also, starting with Twig 4.0, the include function won't pass the full context per default, with no "boolean" to do so automatically, encouraging template isolation and composition.

This PR introduce a Twig Runtime, lowering the impact of the Extension on the global scope, and allowing to create a time-limited "scope", during which the gotenberg_asset will work as expected.

Passing builder in context is fragile, as it won't work if user
uses any of Twig layout features without passing the full context.

Starting with Twig 4.0, the `include` function won't pass the
full context, to encourage template isolation and composition.

This PR introduce a Twig Runtime, lowering the impact of this extension
on the global scope, and allowing to create a time-limited "scope",
during which the `gotenberg_asset` will work as expected.
public function getAssetUrl(string $path): string
{
if (null === $this->builder) {
throw new \LogicException('You need to extend from AbstractChromiumPdfBuilder to use "gotenberg_asset" function.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new \LogicException('You need to extend from AbstractChromiumPdfBuilder to use "gotenberg_asset" function.');
throw new \LogicException('The gotenberg_asset function must be used in a Gotenberg context. You can use it via the AbstractChromiumPdfBuilder or AbstractChromiumScreenshotBuilder class.');

Should be easier to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the original one, but yeah this looks a bit weird.

To be honest i would not specify class names here at all.. It's a bit weird no ?

And add a dedicated interface/VO like "Gotenberg\Assets" (or "AssetSet" or "TemplateAssetBag" or ...) and just use $assets->add(...).

Could even use a default one if none is set.

In the end, I think this function could very much work for everyone... the AssetBag would simply not be collected after the render of the template.

WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest i would not specify class names here at all.. It's a bit weird no ?

Indeed it is not very helpful.

And add a dedicated interface/VO like "Gotenberg\Assets" (or "AssetSet" or "TemplateAssetBag" or ...) and just use $assets->add(...).

I hope I can have some time to rework builders in that way. I started to work with a HeadersBag and a BodyBag, but an AssetBag should be added too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssetBag->addAsset($asset)

Here, $asset can be:

  • A logical path: mapped by assetmapper/webpack -> Package::getUrl.
  • An absolute URL: directly accepted.
  • A local file: either streamed or converted into a signed URL.

This approach should simplify things quite a bit.

And then a second twig function to inline some assets (base64 images for instance, work good for header/footer things)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants