Completed
Push — master ( dcf43c...59d132 )
by Tijs
06:54
created

DefaultController::retrieveDirs()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 12
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 0
crap 20
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\Filesystem\Filesystem;
10
use JMS\TranslationBundle\Model\MessageCatalogue;
11
12
class DefaultController extends Controller
13
{
14
    /**
15
     * @Route("/locale.json")
16
     */
17
    public function generateLocaleAction(Request $request)
18
    {
19
        $translations = array();
20
21
        // fetch the translations for the fallback languages
22
        $fallbackLocales = $this->get('translator')->getFallbackLocales();
23
        foreach (array_reverse($fallbackLocales) as $locale) {
24
            if ($locale !== $request->getLocale()) {
25
                $translations = array_merge(
26
                    $translations,
27
                    $this->getAllTranslations($locale)
28
                );
29
            }
30
        }
31
32
        // overwrite the translations that exist in our current languages
33
        $translations = array_merge(
34
            $translations,
35
            $this->getAllTranslations($request->getLocale())
36
        );
37
38
        // cache the result when we're in production environment
39
        if ($this->container->get('kernel')->getEnvironment() === 'prod') {
40
            $webDir = $this->get('kernel')->getRootDir() . '/../web/';
41
            $fs = new Filesystem();
42
            $fs->dumpfile(
43
                $webDir . $request->getLocale() . '/locale.json',
44
                json_encode($translations)
45
            );
46
        }
47
48
        $response = new JsonResponse();
49
        $response->setData($translations);
50
51
        return $response;
52
    }
53
54
    private function getAllTranslations($locale)
55
    {
56
        $catalogue = new MessageCatalogue();
57
        $loader = $this->get('jms_translation.loader_manager');
58
59
        // load external resources, so current translations can be reused in the final translation
60
        foreach ($this->retrieveDirs() as $resource) {
61
            $catalogue->merge(
62
                $loader->loadFromDirectory(
63
                    $resource,
64
                    $locale
65
                )
66
            );
67
        }
68
69
        $localesArray = array();
70
        foreach ($catalogue->getDomains() as $domain => $collection) {
71
            foreach ($collection->all() as $key => $translation) {
72
                $localesArray[$key] = $translation->getLocaleString();
73
            }
74
        }
75
76
        return $localesArray;
77
    }
78
79
    /**
80
     * The following methods is derived from code of the FrameworkExtension.php file from the Symfony2 framework
81
     *
82
     * @return array
83
     */
84
    private function retrieveDirs()
85
    {
86
        // Discover translation directories
87
        $dirs = array();
88
        foreach ($this->container->getParameter('kernel.bundles') as $bundle) {
89
            $reflection = new \ReflectionClass($bundle);
90
            if (is_dir($dir = dirname($reflection->getFilename()) . '/Resources/translations')) {
91
                $dirs[] = $dir;
92
            }
93
        }
94
95
        if (is_dir($dir = $this->container->getParameter('kernel.root_dir') . '/Resources/translations')) {
96
            $dirs[] = $dir;
97
        }
98
99
        return $dirs;
100
    }
101
}
102