1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koff\Bundle\I18nFormBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
8
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class I18nFormExtension. |
12
|
|
|
*/ |
13
|
|
|
class I18nFormExtension extends Extension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $configs |
17
|
|
|
* @param ContainerBuilder $container |
18
|
|
|
*/ |
19
|
|
|
public function load(array $configs, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
22
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
23
|
|
|
|
24
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources/config')); |
25
|
|
|
$loader->load('services.xml'); |
26
|
|
|
|
27
|
|
|
$this->defineLocaleProvider($config, $container); |
28
|
|
|
$this->defineFormManipulator($config, $container); |
29
|
|
|
if (array_key_exists('form_theme', $config)) { |
30
|
|
|
$this->defineTemplate($config, $container); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function defineLocaleProvider(array $config, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
$localeProvider = $container->getDefinition('koff_i18n_form.locale_provider'); |
37
|
|
|
$localeProvider->replaceArgument(0, $config['locales']); |
38
|
|
|
$localeProvider->replaceArgument(1, $container->getParameter('kernel.default_locale')); |
39
|
|
|
$localeProvider->replaceArgument(2, $config['required_locales']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function defineFormManipulator(array $config, ContainerBuilder $container) |
43
|
|
|
{ |
44
|
|
|
$formManipulator = $container->getDefinition('koff_i18n_form.form_manipulator'); |
45
|
|
|
$formManipulator->replaceArgument(1, $config['excluded_fields']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function defineTemplate(array $config, ContainerBuilder $container) |
49
|
|
|
{ |
50
|
|
|
$twigFormResources = $container->getParameter('twig.form.resources'); |
51
|
|
|
$isFormThemeRegistered = false; |
52
|
|
|
|
53
|
|
|
array_walk( |
54
|
|
|
$twigFormResources, |
55
|
|
|
function ($value) use (&$isFormThemeRegistered) { |
56
|
|
|
$isFormThemeRegistered = false !== stripos($value, '@I18nForm/'); |
57
|
|
|
} |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
if (!$isFormThemeRegistered) { |
61
|
|
|
$twigFormResources[] = '@I18nForm/'.$config['form_theme'].'_form.html.twig'; |
62
|
|
|
$container->setParameter('twig.form.resources', $twigFormResources); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|