FileCacheCatalogueLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 115
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A loadCatalogue() 0 21 3
A getCatalogueCachePath() 0 4 1
A dumpCacheContent() 0 50 2
1
<?php
2
3
/*
4
 * This file is part of the openl10n package.
5
 *
6
 * (c) Matthieu Moquet <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Openl10n\Component\Translation\MessageCatalogue;
13
14
use Doctrine\Common\Cache\Cache;
15
use Symfony\Component\Config\ConfigCache;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Symfony\Component\Translation\MessageCatalogueInterface;
18
19
/**
20
 * Port of Symfony file cache system.
21
 */
22
class FileCacheCatalogueLoader implements MessageCatalogueLoader
23
{
24
    /**
25
     * Decorated catalogue loader.
26
     *
27
     * @var MessageCatalogueLoader
28
     */
29
    private $catalogueLoader;
30
31
    /**
32
     * @var string
33
     */
34
    private $cacheDir;
35
36
    /**
37
     * @var bool
38
     */
39
    private $debug;
40
41
    /**
42
     * @param MessageCatalogueLoader $catalogueLoader
43
     * @param string                 $cacheDir
44
     * @param bool                   $debug
45
     */
46
    public function __construct(MessageCatalogueLoader $catalogueLoader, $cacheDir, $debug = false)
47
    {
48
        $this->catalogueLoader = $catalogueLoader;
49
        $this->cacheDir = $cacheDir;
50
        $this->debug = (bool) $debug;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function loadCatalogue($locale)
57
    {
58
        if (!class_exists('Symfony\Component\Config\ConfigCache')) {
59
            throw new BadMethodCallException('You must install symfony/config component to use this class');
60
        }
61
62
        $cache = new ConfigCache($this->getCatalogueCachePath($locale), $this->debug);
63
64
        if ($cache->isFresh()) {
65
            $catalogue = include $cache->getPath();
66
        } else {
67
            $catalogue = $this->catalogueLoader->loadCatalogue($locale);
68
69
            $cache->write(
70
                $this->dumpCacheContent($catalogue),
71
                $catalogue->getResources()
72
            );
73
        }
74
75
        return $catalogue;
76
    }
77
78
    private function getCatalogueCachePath($locale)
79
    {
80
        return $this->cacheDir.'/catalogue.'.$locale.'.php';
81
    }
82
83
    /**
84
     * @return string Content to write in cache file
85
     */
86
    private function dumpCacheContent(MessageCatalogueInterface $catalogue)
87
    {
88
        $currentCatalogue = $catalogue;
89
        $currentLocale = '';
90
        $fallbackContent = '';
91
        $replacementPattern = '/[^a-z0-9_]/i';
92
93
        while (null !== $fallbackCatalogue = $currentCatalogue->getFallbackCatalogue()) {
94
            $fallbackLocale = $fallbackCatalogue->getLocale();
95
96
            $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallbackLocale));
97
            $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $currentLocale));
98
99
            $fallbackContent .= sprintf(<<<EOF
100
\$catalogue%s = new MessageCatalogue('%s', %s);
101
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
102
103
104
EOF
105
                ,
106
                $fallbackSuffix,
107
                $fallbackLocale,
108
                var_export($fallbackCatalogue->all(), true),
109
                $currentSuffix,
110
                $fallbackSuffix
111
            );
112
113
            $currentCatalogue = $fallbackCatalogue;
114
            $currentLocale = $fallbackLocale;
115
        }
116
117
        $content = sprintf(<<<EOF
118
<?php
119
120
use Symfony\Component\Translation\MessageCatalogue;
121
122
\$catalogue = new MessageCatalogue('%s', %s);
123
124
%s
125
return \$catalogue;
126
127
EOF
128
            ,
129
            $catalogue->getLocale(),
130
            var_export($catalogue->all(), true),
131
            $fallbackContent
132
        );
133
134
        return $content;
135
    }
136
}
137