diff --git a/src/Resources/config/parameters.yml b/src/Resources/config/parameters.yml index 5603623..96cbea4 100644 --- a/src/Resources/config/parameters.yml +++ b/src/Resources/config/parameters.yml @@ -1,2 +1,4 @@ parameters: - webpack_manifest.webpack_manifest_path: %kernel.root_dir%/../web/assets/manifest.json \ No newline at end of file + webpack_manifest.public_path: /assets + webpack_manifest.output_path: %kernel.root_dir%/../web/assets/ + webpack_manifest.webpack_manifest_path: %webpack_manifest.output_path%/manifest.json \ No newline at end of file diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 0c92ff1..b2acbb1 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -4,6 +4,8 @@ services: class: Bluetel\WebpackManifestBundle\WebpackManifest arguments: - "%webpack_manifest.webpack_manifest_path%" + - %webpack_manifest.output_path% + - %webpack_manifest.public_path% webpack_manifest.controller: class: Bluetel\WebpackManifestBundle\Controller\WebpackManifestController diff --git a/src/Twig/WebpackManifestExtension.php b/src/Twig/WebpackManifestExtension.php index dea3717..b6d335f 100644 --- a/src/Twig/WebpackManifestExtension.php +++ b/src/Twig/WebpackManifestExtension.php @@ -25,7 +25,10 @@ public function __construct(WebpackManifest $webpackManifest) */ public function getFunctions() { - return array(new Twig_Function('webpack_manifest_asset', array($this, 'manifestAsset'))); + return array( + new Twig_Function('webpack_manifest_asset', array($this, 'manifestAsset')), + new Twig_Function('webpack_manifest_asset_inline', array($this, 'manifestAssetInline'), array('is_safe' => array('all'))), + ); } /** @@ -36,4 +39,13 @@ public function manifestAsset(string $assetFilename) { return $this->webpackManifest->getAssetPath($assetFilename); } + + /** + * @param string $assetFilename + * @return string + */ + public function manifestAssetInline(string $assetFilename) + { + return $this->webpackManifest->getAssetContents($assetFilename); + } } diff --git a/src/WebpackManifest.php b/src/WebpackManifest.php index cc762d5..bdc88b3 100644 --- a/src/WebpackManifest.php +++ b/src/WebpackManifest.php @@ -1,19 +1,38 @@ manifestPath = $manifestPath; + $this->outputPath = $outputPath; + $this->publicPath = $publicPath; } /** @@ -34,4 +53,23 @@ public function getAssetPath(string $assetFilename) return $manifest[$assetFilename]; } + + /** + * @param string $assetFilename + * @return string + */ + public function getRealAssetPath(string $assetFilename) + { + $path = $this->getAssetPath($assetFilename); + return str_replace($this->publicPath, $this->outputPath, $path); + } + + /** + * @param string $assetFilename + * @return string + */ + public function getAssetContents(string $assetFilename) + { + return file_get_contents($this->getRealAssetPath($assetFilename)); + } }