Completed
Push — theme-bundle ( 69304b...bce4cb )
by Kamil
18:15
created

ThemeAwareSourcesPass::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ThemeBundle\Translation\DependencyInjection\Compiler;
13
14
use SplFileInfo;
15
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
16
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
17
use Sylius\Bundle\ThemeBundle\Translation\Loader\ThemeAwareLoader;
18
use Sylius\Bundle\ThemeBundle\Translation\TranslationFilesFinderInterface;
19
use Symfony\Component\Config\Resource\DirectoryResource;
20
use Symfony\Component\Config\Resource\FileResource;
21
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Definition;
24
use Symfony\Component\DependencyInjection\DefinitionDecorator;
25
use Symfony\Component\DependencyInjection\Reference;
26
use Symfony\Component\Finder\Finder;
27
28
/**
29
 * @author Kamil Kokot <[email protected]>
30
 */
31
final class ThemeAwareSourcesPass implements CompilerPassInterface
32
{
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function process(ContainerBuilder $container)
37
    {
38
        $this->addThemeAwareTranslationSources($container);
39
    }
40
41
    /**
42
     * @param ContainerBuilder $container
43
     */
44
    private function addThemeAwareTranslationSources(ContainerBuilder $container)
45
    {
46
        $files = $this->getTranslationFiles($container);
47
48
        if (empty($files)) {
49
            return;
50
        }
51
52
        $groupedFiles = $this->groupFilesByLocale($files);
53
54
        $translator = $container->findDefinition('translator.default');
55
56
        $options = array_merge_recursive(
57
            $translator->getArgument(3),
58
            ['resource_files' => $groupedFiles]
59
        );
60
61
        $translator->replaceArgument(3, $options);
62
63
        $this->addContainerResources($container, $files);
64
    }
65
66
    private function getTranslationFiles(ContainerBuilder $container)
67
    {
68
        /** @var TranslationFilesFinderInterface $translationFilesFinder */
69
        $translationFilesFinder = $container->get('sylius.theme.translation.files_finder');
70
        $themes = $container->get('sylius.theme.repository')->findAll();
71
72
        $files = [];
73
        foreach ($themes as $theme) {
74
            $files = array_merge(
75
                $files,
76
                $translationFilesFinder->findTranslationFiles($theme)
77
            );
78
        }
79
80
        return $files;
81
    }
82
83
    /**
84
     * @param ContainerBuilder $container
85
     * @param array $files
86
     */
87
    private function addContainerResources(ContainerBuilder $container, $files)
88
    {
89
        foreach ($files as $file) {
90
            $container->addResource(new FileResource($file));
91
        }
92
    }
93
94
    /**
95
     * @param array $files
96
     *
97
     * @return array
98
     */
99
    private function groupFilesByLocale($files)
100
    {
101
        $groupedFiles = [];
102
        foreach ($files as $file) {
103
            $locale = explode('.', basename($file), 3)[1];
104
            if (!isset($groupedFiles[$locale])) {
105
                $groupedFiles[$locale] = [];
106
            }
107
108
            $groupedFiles[$locale][] = $file;
109
        }
110
111
        return $groupedFiles;
112
    }
113
}
114