Completed
Push — master ( 80ba2b...8bd5d2 )
by Craig
14:31
created

generatePhpTranslationConfigs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 31
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\SettingsModule\DependencyInjection;
15
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
22
use Zikula\ExtensionsModule\AbstractExtension;
23
24
class ZikulaSettingsExtension extends Extension implements PrependExtensionInterface
25
{
26
    public function prepend(ContainerBuilder $container)
27
    {
28
        $configs = $container->getExtensionConfig($this->getAlias());
29
        $zikulaSettingsConfig = $this->processConfiguration(new Configuration(), $configs);
30
        if (isset($container->getExtensions()['framework'])) {
31
            $container->prependExtensionConfig('framework', [
32
                'default_locale' => $zikulaSettingsConfig['locale']
33
            ]);
34
        }
35
        if (isset($container->getExtensions()['bazinga_js_translation'])) {
36
            $container->prependExtensionConfig('bazinga_js_translation', [
37
                'locale_fallback' => $zikulaSettingsConfig['locale'],
38
                'active_locales' => $zikulaSettingsConfig['locales']
39
            ]);
40
        }
41
        if (isset($container->getExtensions()['jms_i18n_routing'])) {
42
            $container->prependExtensionConfig('jms_i18n_routing', [
43
                'default_locale' => $zikulaSettingsConfig['locale'],
44
                'locales' => $zikulaSettingsConfig['locales']
45
            ]);
46
        }
47
        if (isset($container->getExtensions()['translation'])) {
48
            $container->prependExtensionConfig('translation', [
49
                'locales' => $zikulaSettingsConfig['locales'],
50
                'configs' => $this->generatePhpTranslationConfigs($container)
51
            ]);
52
        }
53
    }
54
55
    public function load(array $configs, ContainerBuilder $container)
56
    {
57
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config'));
58
        $loader->load('services.yaml');
59
60
        $configuration = new Configuration();
61
        $config = $this->processConfiguration($configuration, $configs);
62
63
        $container->setParameter('locale', $config['locale']);
64
        $container->setParameter('localisation.locales', $config['locales']);
65
    }
66
67
    private function generatePhpTranslationConfigs(ContainerBuilder $container): array
68
    {
69
        $transConfigNew = $this->getBaseConfig();
70
71
        if (file_exists($container->getParameter('kernel.project_dir') . '/src/system')) {
72
            // monorepo: core bundles and system modules are in "src/"
73
            $transConfigNew['zikula']['dirs'] = [
74
                '%kernel.project_dir%/templates',
75
                '%kernel.project_dir%/src/system',
76
                '%kernel.project_dir%/src/Zikula'
77
            ];
78
            // do not set in a distribution package when core components are in "vendor/"
79
        }
80
81
        $bundles = array_filter($container->getParameter('kernel.bundles'), function($bundleClassName, $name) use ($container) {
82
            if (ZikulaKernel::isCoreExtension($name)) {
83
                return false;
84
            }
85
86
            return $container->getReflectionClass($bundleClassName)->isSubclassOf(AbstractExtension::class);
87
        }, ARRAY_FILTER_USE_BOTH);
88
        foreach ($bundles as $name => $bundleClassName) {
89
            $bundleConfig = $this->getConfigTemplate();
90
            $bundleDir = dirname($container->getReflectionClass($bundleClassName)->getFileName());
91
            $translationDirectory = $bundleDir . '/Resources/translations';
92
            $bundleConfig['output_dir'] = $translationDirectory;
93
            $bundleConfig['external_translations_dir'] = $translationDirectory;
94
            $transConfigNew[mb_strtolower($name)] = $bundleConfig;
95
        }
96
97
        return $transConfigNew;
98
    }
99
100
    private function getBaseConfig(): array
101
    {
102
        $config = [
103
            'zikula' => $this->getConfigTemplate(),
104
            'extension' => $this->getConfigTemplate()
105
        ];
106
        $config['zikula']['output_dir'] = '%kernel.project_dir%/translations';
107
        $config['zikula']['excluded_dirs'] = ['vendor', 'cache', 'data', 'log'];
108
        $config['extension']['excluded_dirs'] = ['vendor'];
109
110
        return $config;
111
    }
112
113
    private function getConfigTemplate(): array
114
    {
115
        return [
116
            'excluded_names' => ['*TestCase.php', '*Test.php'],
117
            'excluded_dirs' => ['vendor'],
118
            'output_format' => 'yaml',
119
            'local_file_storage_options' => [
120
                'default_output_format' => 'yaml'
121
            ]
122
        ];
123
    }
124
}
125