From d0af496555a86902517261aff622bad954f95abb Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 10 May 2019 12:54:23 +0200 Subject: [PATCH] Fix local asset twig function and allow url --- README.md | 9 +++++++-- Resources/config/services.xml | 6 +++++- Twig/LocalAssetTwigExtension.php | 19 +++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a92e28d..b63625d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,6 @@ http://wkhtmltopdf.org/downloads.html See [KnpSnappyBundle](https://github.com/KnpLabs/KnpSnappyBundle#configuration) for configuration. ## Usage -======== **Controller Trait** @@ -100,4 +99,10 @@ The `local_asset` avoids doing a http request by using `file://` instead of `htt Local Asset ``` -This will only work when `$request->getRequestFormat()` will return `pdf`. +This will only work when `$request->getRequestFormat()` will return `pdf` and not `html`. + +If you want to force using `file://` set the second parameter to true: + +```twig +Local Asset +``` diff --git a/Resources/config/services.xml b/Resources/config/services.xml index b9b0382..f1e3582 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -11,14 +11,18 @@ + + The "%service_id%" service is deprecated use massive_pdf.pdf_factory instead. - + + + %massive_pdf.public_dir% diff --git a/Twig/LocalAssetTwigExtension.php b/Twig/LocalAssetTwigExtension.php index 1c33584..e5a17b0 100644 --- a/Twig/LocalAssetTwigExtension.php +++ b/Twig/LocalAssetTwigExtension.php @@ -54,7 +54,7 @@ public function getFunctions() public function getFilters() { return [ - new \Twig_SimpleFilter('abbr_class', [$this, 'getLocalAsset']), + new \Twig_SimpleFilter('local_asset', [$this, 'getLocalAsset']), ]; } @@ -62,18 +62,25 @@ public function getFilters() * Get local asset path. * * @param string $assetUrl + * @param bool $force * * @return string */ - public function getLocalAsset($assetUrl) + public function getLocalAsset($assetUrl, $force = false) { - $request = $this->requestStack->getCurrentRequest(); + $prefix = 'file://' . $this->publicDirectory; $assetUrl = '/' . ltrim($assetUrl, '/'); - if ('html' === $request->getRequestFormat()) { - return $assetUrl; + if ($force) { + return $prefix . $assetUrl; + } + + $request = $this->requestStack->getCurrentRequest(); + + if ($request && 'html' === $request->getRequestFormat()) { + $prefix = $request->getSchemeAndHttpHost(); } - return 'file://' . $this->publicDirectory . $assetUrl; + return $prefix . $assetUrl; } }