-
Notifications
You must be signed in to change notification settings - Fork 12
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
Handle assets from the Symfony component #125
Draft
Jean-Beru
wants to merge
1
commit into
sensiolabs:main
Choose a base branch
from
Jean-Beru:issue/121
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# CHANGELOG | ||
|
||
## 0.3.0 | ||
|
||
* [BC BREAK] Rename `sensiolabs_gotenberg.twig.asset_extension` to `sensiolabs_gotenberg.twig.gotenberg_asset_extension` |
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,19 @@ | ||
<?php | ||
|
||
use Sensiolabs\GotenbergBundle\Twig\AssetExtension; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\param; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('sensiolabs_gotenberg.twig.asset_extension', AssetExtension::class) | ||
->decorate('twig.extension.assets') | ||
->args([ | ||
service('.inner'), | ||
service('assets.packages'), | ||
param('kernel.project_dir'), | ||
]) | ||
; | ||
}; |
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
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
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
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,7 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Exception; | ||
|
||
class RenderingException extends RuntimeException | ||
{ | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Exception; | ||
|
||
class RuntimeException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Twig; | ||
|
||
use Sensiolabs\GotenbergBundle\Builder\Pdf\AbstractChromiumPdfBuilder; | ||
use Sensiolabs\GotenbergBundle\Builder\Screenshot\AbstractChromiumScreenshotBuilder; | ||
use Sensiolabs\GotenbergBundle\Exception\RenderingException; | ||
use Symfony\Component\Asset\Packages; | ||
use Symfony\Bridge\Twig\Extension\AssetExtension as TwigAssetExtension; | ||
use Symfony\Component\Asset\PathPackage; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class AssetExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private readonly TwigAssetExtension $inner, | ||
private readonly Packages $packages, | ||
private readonly string $projectDir, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
$functions = []; | ||
|
||
foreach ($this->inner->getFunctions() as $function) { | ||
if ('asset' === $function->getName()) { | ||
$function = new TwigFunction('asset', $this->getAsset(...), ['needs_context' => true]); | ||
} | ||
$functions[$function->getName()] = $function; | ||
} | ||
|
||
return $functions; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function getAsset(array $context, string $path, string|null $packageName = null): string | ||
{ | ||
$builder = $context['_builder'] ?? null; | ||
|
||
if (!$builder) { | ||
return $this->inner->getAssetUrl($path, $packageName); | ||
} | ||
|
||
$package = $this->packages->getPackage($packageName); | ||
if (!$package instanceof PathPackage) { | ||
return $this->inner->getAssetUrl($path, $packageName); | ||
} | ||
|
||
if (!$builder instanceof AbstractChromiumPdfBuilder && !$builder instanceof AbstractChromiumScreenshotBuilder) { | ||
throw new RenderingException('The gotenberg_asset function must be used with a class extending AbstractChromiumPdfBuilder or AbstractChromiumScreenshotBuilder.'); | ||
} | ||
|
||
$name = uniqid(); | ||
|
||
$builder->addAsset($this->projectDir.'/public'.$package->getBasePath().$path, $name); | ||
|
||
return $name; | ||
} | ||
} |
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
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.
So this extension will decorate my entire app usage of "asset" ? ... well...
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.
Since the
PathPackage
does not include the host,asset
cannot work in a Gotenberg context.How can we make it working without decorating it ? I was also thinking about the
<base>
tag to solve this problem but it's not recommended.