|
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 KoffI18nFormExtension extends Extension |
|
14
|
|
|
{ |
|
15
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
16
|
|
|
{ |
|
17
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
|
18
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(\dirname(__DIR__).'/../config')); |
|
21
|
|
|
$loader->load('services.xml'); |
|
22
|
|
|
|
|
23
|
|
|
$this->defineLocaleProvider($config, $container); |
|
24
|
|
|
$this->defineFormManipulator($config, $container); |
|
25
|
|
|
if (\array_key_exists('form_theme', $config)) { |
|
26
|
|
|
$this->defineTemplate($config, $container); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
private function defineLocaleProvider(array $config, ContainerBuilder $container) |
|
31
|
|
|
{ |
|
32
|
|
|
$localeProvider = $container->getDefinition('koff_i18n_form.locale_provider'); |
|
33
|
|
|
$localeProvider->replaceArgument(0, $config['locales']); |
|
34
|
|
|
$localeProvider->replaceArgument(1, $container->getParameter('kernel.default_locale')); |
|
35
|
|
|
$localeProvider->replaceArgument(2, $config['required_locales']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function defineFormManipulator(array $config, ContainerBuilder $container) |
|
39
|
|
|
{ |
|
40
|
|
|
$formManipulator = $container->getDefinition('koff_i18n_form.form_manipulator'); |
|
41
|
|
|
$formManipulator->replaceArgument(1, $config['excluded_fields']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function defineTemplate(array $config, ContainerBuilder $container) |
|
45
|
|
|
{ |
|
46
|
|
|
$twigFormResources = $container->getParameter('twig.form.resources'); |
|
47
|
|
|
$isFormThemeRegistered = false; |
|
48
|
|
|
|
|
49
|
|
|
array_walk( |
|
50
|
|
|
$twigFormResources, |
|
|
|
|
|
|
51
|
|
|
function ($value) use (&$isFormThemeRegistered) { |
|
52
|
|
|
$isFormThemeRegistered = false !== stripos($value, '@KoffI18nForm/'); |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
if (!$isFormThemeRegistered) { |
|
57
|
|
|
$twigFormResources[] = '@KoffI18nForm/'.$config['form_theme'].'_form.html.twig'; |
|
58
|
|
|
$container->setParameter('twig.form.resources', $twigFormResources); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|