Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Resources/config/parameters.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
parameters:
webpack_manifest.webpack_manifest_path: %kernel.root_dir%/../web/assets/manifest.json
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
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Twig/WebpackManifestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))),
);
}

/**
Expand 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);
}
}
44 changes: 41 additions & 3 deletions src/WebpackManifest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
<?php
namespace Bluetel\WebpackManifestBundle;

use Throwable;

class WebpackManifest
{
/**
* @param $manifestPath
* @var string
*/
private $manifestPath;

/**
* @var string
*/
private $outputPath;

/**
* @var string
*/
private $publicPath;

/**
* @param $manifestPath
* @param $outputPath
* @param $publicPath
*/
public function __construct($manifestPath)
{
public function __construct(
$manifestPath,
$outputPath,
$publicPath
) {
$this->manifestPath = $manifestPath;
$this->outputPath = $outputPath;
$this->publicPath = $publicPath;
}

/**
Expand All @@ -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));
}
}