These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TranslatorBundle\DependencyInjection; |
||
4 | |||
5 | use Symfony\Component\Config\FileLocator; |
||
6 | use Symfony\Component\Config\Resource\DirectoryResource; |
||
7 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
8 | use Symfony\Component\DependencyInjection\Loader; |
||
9 | use Symfony\Component\Finder\Finder; |
||
10 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
||
11 | use Symfony\Component\HttpKernel\Kernel; |
||
12 | |||
13 | /** |
||
14 | * This is the class that loads and manages your bundle configuration |
||
15 | * |
||
16 | * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
||
17 | */ |
||
18 | class KunstmaanTranslatorExtension extends Extension |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 5 | public function load(array $configs, ContainerBuilder $container) |
|
24 | { |
||
25 | 5 | $configuration = new Configuration(); |
|
26 | 5 | $config = $this->processConfiguration($configuration, $configs); |
|
27 | |||
28 | 5 | if ($config['enabled'] === false) { |
|
29 | 1 | return; |
|
30 | } |
||
31 | |||
32 | 4 | if (!$container->hasParameter('requiredlocales')) { |
|
33 | 1 | $container->setParameter('requiredlocales', ['nl', 'fr', 'en']); |
|
34 | } |
||
35 | 4 | $container->setParameter('kuma_translator.enabled', $config['enabled']); |
|
36 | 4 | $container->setParameter('kuma_translator.default_bundle', $config['default_bundle']); |
|
37 | 4 | $container->setParameter('kuma_translator.bundles', $config['bundles']); |
|
38 | 4 | $container->setParameter('kuma_translator.cache_dir', $config['cache_dir']); |
|
39 | 4 | if (empty($config['managed_locales']) && $container->hasParameter('requiredlocales')) { |
|
40 | $config['managed_locales'] = explode('|', $container->getParameter('requiredlocales')); |
||
41 | } |
||
42 | 4 | $container->setParameter('kuma_translator.managed_locales', $config['managed_locales']); |
|
43 | 4 | $container->setParameter('kuma_translator.file_formats', $config['file_formats']); |
|
44 | 4 | $container->setParameter('kuma_translator.storage_engine.type', $config['storage_engine']['type']); |
|
45 | 4 | $container->setParameter('kuma_translator.profiler', $container->getParameter('kernel.debug')); |
|
46 | 4 | $container->setParameter('kuma_translator.debug', \is_null($config['debug']) ? $container->getParameter('kernel.debug') : $config['debug']); |
|
47 | |||
48 | 4 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
49 | 4 | $loader->load('services.yml'); |
|
50 | 4 | $loader->load('repositories.yml'); |
|
51 | 4 | $loader->load('commands.yml'); |
|
52 | |||
53 | 4 | $this->setTranslationConfiguration($config, $container); |
|
54 | 4 | $container->getDefinition('kunstmaan_translator.datacollector')->setDecoratedService('translator'); |
|
55 | 4 | } |
|
56 | |||
57 | 4 | public function setTranslationConfiguration($config, $container) |
|
58 | { |
||
59 | 4 | $container->setAlias('translator', 'kunstmaan_translator.service.translator.translator')->setPublic(true); |
|
60 | 4 | $container->setAlias('translator.default', 'kunstmaan_translator.service.translator.translator')->setPublic(true); |
|
61 | 4 | $translator = $container->getDefinition('kunstmaan_translator.service.translator.translator'); |
|
62 | 4 | $this->registerTranslatorConfiguration($config, $container); |
|
63 | |||
64 | // overwrites everything |
||
65 | 4 | $translator->addMethodCall('addDatabaseResources', []); |
|
66 | |||
67 | 4 | $translator->addMethodCall('setFallbackLocales', [['en']]); |
|
68 | |||
69 | 4 | if ($container->hasParameter('kunstmaan_admin.default_locale')) { |
|
70 | 4 | $translator->addMethodCall('setFallbackLocales', [[$container->getParameter('kunstmaan_admin.default_locale')]]); |
|
71 | } |
||
72 | 4 | } |
|
73 | |||
74 | /** |
||
75 | * Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension |
||
76 | * $this->registerTranslatorConfiguration($config['translator'], $container); |
||
77 | * Used to load all other translation files |
||
78 | */ |
||
79 | 4 | public function registerTranslatorConfiguration($config, $container) |
|
80 | { |
||
81 | 4 | $translator = $container->getDefinition('kunstmaan_translator.service.translator.translator'); |
|
82 | |||
83 | 4 | $dirs = []; |
|
84 | 4 | View Code Duplication | if (class_exists('Symfony\Component\Validator\Validation')) { |
0 ignored issues
–
show
|
|||
85 | 4 | $r = new \ReflectionClass('Symfony\Component\Validator\Validation'); |
|
86 | |||
87 | 4 | $dirs[] = \dirname($r->getFileName()).'/Resources/translations'; |
|
88 | } |
||
89 | 4 | View Code Duplication | if (class_exists('Symfony\Component\Form\Form')) { |
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
90 | 4 | $r = new \ReflectionClass('Symfony\Component\Form\Form'); |
|
91 | |||
92 | 4 | $dirs[] = \dirname($r->getFileName()).'/Resources/translations'; |
|
93 | } |
||
94 | |||
95 | 4 | View Code Duplication | if (Kernel::VERSION_ID < 4000) { |
96 | $overridePath = $container->getParameter('kernel.root_dir').'/Resources/%s/translations'; |
||
97 | } else { |
||
98 | 4 | $overridePath = $container->getParameter('kernel.project_dir').'/translations'; |
|
99 | } |
||
100 | |||
101 | 4 | foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { |
|
102 | 4 | $reflection = new \ReflectionClass($class); |
|
103 | 4 | if (is_dir($dir = \dirname($reflection->getFileName()).'/Resources/translations')) { |
|
104 | 4 | $dirs[] = $dir; |
|
105 | } |
||
106 | 4 | if (is_dir($dir = sprintf($overridePath, $bundle))) { |
|
107 | 3 | $dirs[] = $dir; |
|
108 | } |
||
109 | } |
||
110 | |||
111 | 4 | View Code Duplication | if (Kernel::VERSION_ID < 4000) { |
112 | $dir = $container->getParameter('kernel.root_dir').'/Resources/translations'; |
||
113 | } else { |
||
114 | 4 | $dir = $container->getParameter('kernel.project_dir').'/translations'; |
|
115 | } |
||
116 | |||
117 | 4 | if (is_dir($dir)) { |
|
118 | 3 | $dirs[] = $dir; |
|
119 | } |
||
120 | |||
121 | // Register translation resources |
||
122 | 4 | if (\count($dirs) > 0) { |
|
123 | 4 | foreach ($dirs as $dir) { |
|
124 | 4 | $container->addResource(new DirectoryResource($dir)); |
|
125 | } |
||
126 | |||
127 | 4 | $finder = Finder::create(); |
|
128 | 4 | $finder->files(); |
|
129 | |||
130 | 4 | $finder->filter( |
|
131 | function (\SplFileInfo $file) { |
||
132 | 4 | return 2 === substr_count($file->getBasename(), '.') && preg_match('/\.\w+$/', $file->getBasename()); |
|
133 | 4 | } |
|
134 | ); |
||
135 | |||
136 | 4 | $finder->in($dirs); |
|
137 | |||
138 | 4 | foreach ($finder as $file) { |
|
139 | // filename is domain.locale.format |
||
140 | 4 | list($domain, $locale, $format) = explode('.', $file->getBasename()); |
|
141 | 4 | $translator->addMethodCall('addResource', [$format, (string) $file, $locale, $domain]); |
|
142 | } |
||
143 | } |
||
144 | 4 | } |
|
145 | } |
||
146 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.