Skip to content

Commit

Permalink
Refactor Twig Compiler Pass
Browse files Browse the repository at this point in the history
Switched to relying on configuration to pick the template

Signed-off-by: Daniel Zauner <[email protected]>
  • Loading branch information
dTatham committed Jan 12, 2018
1 parent f51b45a commit 639d53d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
19 changes: 11 additions & 8 deletions DependencyInjection/Compiler/FormTwigTemplateCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
class FormTwigTemplateCompilerPass implements CompilerPassInterface
{
private $telLayout = '@MisdPhoneNumber/Form/tel.html.twig';
private $telBootstrapLayout = '@MisdPhoneNumber/Form/tel_bootstrap.html.twig';

/**
* {@inheritdoc}
*/
Expand All @@ -32,21 +29,27 @@ public function process(ContainerBuilder $container)
}

$parameter = $container->getParameter('twig.form.resources');
$template = $container->getParameter('phone_number.template');

if (in_array($this->telLayout, $parameter)) {
if (in_array($template, $parameter)) {
return;
}


// Insert right after base template if it exists.
if (($key = array_search('bootstrap_3_horizontal_layout.html.twig', $parameter)) !== false) {
array_splice($parameter, ++$key, 0, array($this->telBootstrapLayout));
array_splice($parameter, ++$key, 0, array($template));
} elseif (($key = array_search('bootstrap_3_layout.html.twig', $parameter)) !== false) {
array_splice($parameter, ++$key, 0, array($this->telBootstrapLayout));
array_splice($parameter, ++$key, 0, array($template));
} elseif (($key = array_search('bootstrap_4_layout.html.twig', $parameter)) !== false) {
array_splice($parameter, ++$key, 0, array($template));
} elseif (($key = array_search('bootstrap_4_horizontal_layout.html.twig', $parameter)) !== false) {
array_splice($parameter, ++$key, 0, array($template));
} elseif (($key = array_search('form_div_layout.html.twig', $parameter)) !== false) {
array_splice($parameter, ++$key, 0, array($this->telLayout));
array_splice($parameter, ++$key, 0, array($template));
} else {
// Put it in first position.
array_unshift($parameter, array($this->telLayout));
array_unshift($parameter, $template);
}

$container->setParameter('twig.form.resources', $parameter);
Expand Down
37 changes: 37 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Created by dTatham
* Date: 02.12.17
* Time: 21:50
*/

namespace Misd\PhoneNumberBundle\DependencyInjection;


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

class Configuration implements ConfigurationInterface
{

/**
* Generates the configuration tree builder.
*
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();

$builder
->root('misd_phone_number')
->addDefaultsIfNotSet()
->children()
->scalarNode('template')
->defaultValue('@MisdPhoneNumberBundle/Form/tel.html.twig')
->end()
->end();

return $builder;
}
}
9 changes: 9 additions & 0 deletions DependencyInjection/MisdPhoneNumberExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Misd\PhoneNumberBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -28,6 +29,8 @@ class MisdPhoneNumberExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');

Expand All @@ -36,6 +39,12 @@ public function load(array $configs, ContainerBuilder $container)
$this->setFactory($container->getDefinition('libphonenumber.short_number_info'));
$this->setFactory($container->getDefinition('libphonenumber.phone_number_to_carrier_mapper'));
$this->setFactory($container->getDefinition('libphonenumber.phone_number_to_time_zones_mapper'));

$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);

$container->setParameter('phone_number.template', $config['template']);

}

/**
Expand Down

0 comments on commit 639d53d

Please sign in to comment.