Skip to content

Commit 9312065

Browse files
committed
Added automatic version detecion
1 parent 5055d87 commit 9312065

File tree

11 files changed

+1535
-491
lines changed

11 files changed

+1535
-491
lines changed

DependencyInjection/Compiler/FormPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class FormPass implements CompilerPassInterface
2727
public function process(ContainerBuilder $container)
2828
{
2929
if (($template = $container->getParameter('mopa_bootstrap.form.templating')) !== false) {
30+
if(strpos($template, "%d") !== false){
31+
$version = $container->getParameter('mopa_bootstrap.version');
32+
$template = sprintf($template, $version);
33+
}
3034
$resources = $container->getParameter('twig.form.resources');
3135
# Ensure it wasnt already aded via config
3236
if (!in_array($template, $resources)) {

DependencyInjection/Compiler/VersionPass.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,51 @@
55
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
66
use Mopa\Bridge\Composer\Adapter\ComposerAdapter;
77
use Mopa\Bridge\Composer\Util\ComposerPathFinder;
8+
use Symfony\Component\Config\ConfigCache;
9+
use Symfony\Component\Config\Resource\FileResource;
810

911

1012
class VersionPass implements CompilerPassInterface
1113
{
14+
/**
15+
* Fetch bootstrap version from composer and cache result!
16+
*/
1217
public function process(ContainerBuilder $container)
1318
{
1419
if (!$container->hasParameter('mopa_bootstrap.version') || is_null($container->hasParameter('mopa_bootstrap.version'))) {
15-
/*if (false !== $composer = ComposerAdapter::getComposer()) {
16-
$util = new ComposerPathFinder($composer);
17-
$package = $util->findPackage("twbs/bootstrap");
18-
$version = split("-", $package->getVersion());
19-
$container->setParameter('mopa_bootstrap.version', floatVal($version[0]));
20-
} else {
21-
throw new \RuntimeException("Could not find composer and mopa_bootstrap.version not set in config!!");
22-
}*/
20+
21+
22+
$cachePath = $container->getParameter('kernel.cache_dir').'/mopa_bootstrap_version.cache';
23+
24+
// the second argument indicates whether or not you want to use debug mode
25+
$versionCache = new ConfigCache($cachePath, $container->getParameter("kernel.debug"));
26+
27+
if (!$versionCache->isFresh()) {
28+
// make sure this also works for we requests !
29+
$oldWorkingDir = getcwd();
30+
$split = explode(DIRECTORY_SEPARATOR, $oldWorkingDir);
31+
if ( $split[count($split)-1] == "web") {
32+
array_pop($split);
33+
chdir(implode(DIRECTORY_SEPARATOR, $split));
34+
}
35+
putenv("COMPOSER_HOME=".getcwd());
36+
37+
if (false !== $composer = ComposerAdapter::getComposer()) {
38+
$util = new ComposerPathFinder($composer);
39+
$package = $util->findPackage("twbs/bootstrap");
40+
$version = split("-", $package->getVersion());
41+
$targetPackagePath = $composer->getInstallationManager()->getInstallPath($package);
42+
$twbscomposer = $targetPackagePath ."/composer.json";
43+
44+
$resources = array(new FileResource($twbscomposer));
45+
46+
$versionCache->write($version[0], $resources);
47+
} else {
48+
throw new \RuntimeException("Could not find composer and mopa_bootstrap.version not set in config!!");
49+
}
50+
}
51+
$version = file_get_contents($cachePath);
52+
$container->setParameter('mopa_bootstrap.version', floatVal($version));
2353
}
2454
}
2555
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected function addVersionConfig(ArrayNodeDefinition $rootNode)
4343
->children()
4444
->scalarNode('version')
4545
->defaultValue(null)
46+
->cannotBeEmpty()
4647
->end();
4748
}
4849
protected function addFormConfig(ArrayNodeDefinition $rootNode)
@@ -53,7 +54,7 @@ protected function addFormConfig(ArrayNodeDefinition $rootNode)
5354
->addDefaultsIfNotSet()
5455
->children()
5556
->scalarNode('templating')
56-
->defaultValue("MopaBootstrapBundle:Form:fields.html.twig")
57+
->defaultValue("MopaBootstrapBundle:Form:fields_bs_%d.html.twig")
5758
->end()
5859
->scalarNode('horizontal_label_class')
5960
->defaultValue("col-3")

DependencyInjection/MopaBootstrapExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ public function load(array $configs, ContainerBuilder $container)
3232
$config = $this->processConfiguration($configuration, $configs);
3333

3434
$yamlloader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
35-
$yamlloader->load("form_extensions.yml");
35+
if(!isset($config['version'])){
36+
#throw new \RuntimeException("You need to specify mopa_bootstrap.version!");
37+
}
38+
else{
39+
$container->setParameter('mopa_bootstrap.version', $config['version']);
40+
}
3641
$yamlloader->load('twig_extensions.yml');
42+
$yamlloader->load("form_extensions.yml");
3743

3844
if (isset($config['form'])) {
3945
foreach ($config['form'] as $key => $value) {

MopaBootstrapBundle.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ class MopaBootstrapBundle extends Bundle
1313
public function build(ContainerBuilder $container)
1414
{
1515
parent::build($container);
16-
16+
$container->addCompilerPass(new VersionPass());
1717
$container->addCompilerPass(new FormPass());
1818
$container->addCompilerPass(new NavbarPass());
19-
$container->addCompilerPass(new VersionPass());
2019
}
2120
}

Resources/doc/1-installation.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,14 @@ Installation
208208
}
209209
```
210210

211-
3. If you like configure your config.yml (not mandatory)
211+
3. You need to set the bootstrap version until we get composer working (mandatory!)
212+
213+
``` yaml
214+
mopa_bootstrap:
215+
version: 3 # bootstrap major version currently only 2 or 3 are supported!
216+
```
217+
218+
4. If you like further tweak your config.yml (not mandatory)
212219

213220
``` yaml
214221
mopa_bootstrap:

0 commit comments

Comments
 (0)