Skip to content

Commit

Permalink
Add custom bundle configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fracz committed Jul 10, 2017
1 parent c86faa3 commit 4098dbf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 8 deletions.
4 changes: 1 addition & 3 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: config_build.yml }
- { resource: config_build.yml, ignore_errors: true }

doctrine:
dbal:
Expand Down Expand Up @@ -84,6 +84,4 @@ twig:
debug: "%kernel.debug%"
form_themes:
- "SuplaBundle:Form:fields.html.twig"
globals:
application_version: "%application_version%"
strict_variables: "%kernel.debug%"
2 changes: 1 addition & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ services:
public: false
arguments:
- "%use_webpack_dev_server%"
- "%webpack_hashes%"
- "%supla.webpack_hashes%"

supla.api.ouath_storage_with_legacy_password_support:
class: SuplaApiBundle\Provider\OAuthStorageWithLegacyPasswordSupport
Expand Down
4 changes: 2 additions & 2 deletions src/Frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ if (process.env.NODE_ENV === 'production') {
// https://webpack.github.io/docs/long-term-caching.html#get-filenames-from-stats
this.plugin("done", function (stats) {
var hashes = stats.toJson().assetsByChunkName;
var phpConfig = "# Config generated automatically by running npm run build in src/Frontend directory.\n\nparameters:\n";
phpConfig += ' application_version: ' + (require('./package.json').version) + "\n";
var phpConfig = "# Config generated automatically by running npm run build in src/Frontend directory.\n\nsupla:\n";
phpConfig += ' version: ' + (require('./package.json').version) + "\n";
phpConfig += ' webpack_hashes:\n';
for (var chunkName in hashes) {
phpConfig += ` ${chunkName}: "${hashes[chunkName][0]}"\n`;
Expand Down
4 changes: 3 additions & 1 deletion src/SuplaBundle/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public function viewAction() {

return $this->render(
'SuplaBundle:Account:view.html.twig',
['user' => $user,
[
'user' => $user,
'version' => $this->getParameter('supla.version'),
]
);
}
Expand Down
20 changes: 20 additions & 0 deletions src/SuplaBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace SuplaBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface {
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('supla');
// @formatter:off because indentation makes config structure way clearer
$rootNode
->children()
->scalarNode('version')->defaultValue('?.?.?')->end()
->arrayNode('webpack_hashes')->defaultValue([])->useAttributeAsKey('name')->prototype('scalar')->end()
->end();
// @formatter:on
return $treeBuilder;
}
}
12 changes: 12 additions & 0 deletions src/SuplaBundle/DependencyInjection/SuplaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace SuplaBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

class SuplaExtension extends ConfigurableExtension {
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) {
$container->setParameter('supla.version', $mergedConfig['version']);
$container->setParameter('supla.webpack_hashes', $mergedConfig['webpack_hashes']);
}
}
2 changes: 1 addition & 1 deletion src/SuplaBundle/Resources/views/Account/view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

</svg>

<span class="supla-member">supla cloud {{ application_version }}</span>
<span class="supla-member">supla cloud {{ version }}</span>
<div class="user-account" id="user-account">
<h1>{{ user.email }}</h1>

Expand Down
4 changes: 4 additions & 0 deletions src/SuplaBundle/Twig/WebpackAssetVersionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class WebpackAssetVersionStrategy implements VersionStrategyInterface {

public function __construct(bool $webpackDevServer, array $hashes) {
$this->webpackDevServer = $webpackDevServer;
if (count($hashes) === 0) {
throw new \RuntimeException('Invalid frontend configuration. '
. 'Please build the frontend code with the following command: composer run-script webpack');
}
$this->hashes = $hashes;
}

Expand Down

0 comments on commit 4098dbf

Please sign in to comment.