Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Twig Compiler Pass #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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