Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7527f65

Browse files
committed
[WIP][Translator] move loading catalogue out of Translator.
1 parent 4b71fe0 commit 7527f65

7 files changed

+567
-100
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Translation;
13+
14+
use Symfony\Component\Config\ConfigCacheInterface;
15+
use Symfony\Component\Config\ConfigCacheFactoryInterface;
16+
use Symfony\Component\Config\ConfigCacheFactory;
17+
18+
/**
19+
* @author Abdellatif Ait boudad <[email protected]>
20+
*/
21+
class CacheTranslatorBag implements TranslatorBagInterface
22+
{
23+
/**
24+
* @var TranslatorBagInterface
25+
*/
26+
private $translatorBag;
27+
28+
/**
29+
* @var string|null
30+
*/
31+
private $cacheDir;
32+
33+
/**
34+
* @var bool
35+
*/
36+
private $debug;
37+
38+
/**
39+
* @var ConfigCacheFactoryInterface|null
40+
*/
41+
private $configCacheFactory;
42+
43+
/**
44+
* @param string|null $cacheDir The directory to use for the cache
45+
* @param bool $debug Use cache in debug mode ?
46+
* @param TranslatorBagInterface $translatorBag
47+
* @param ConfigCacheFactoryInterface $configCacheFactory
48+
*/
49+
public function __construct($cacheDir, $debug = false, TranslatorBagInterface $translatorBag = null, ConfigCacheFactoryInterface $configCacheFactory = null)
50+
{
51+
$this->cacheDir = $cacheDir;
52+
$this->debug = $debug;
53+
$this->translatorBag = $translatorBag ?: new FallbackTranslatorBag();
54+
$this->configCacheFactory = $configCacheFactory ?: new ConfigCacheFactory($this->debug);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function getCatalogue($locale = null)
61+
{
62+
$self = $this; // required for PHP 5.3 where "$this" cannot be use()d in anonymous functions. Change in Symfony 3.0.
63+
$cache = $this->configCacheFactory->cache($this->getCatalogueCachePath($locale),
64+
function (ConfigCacheInterface $cache) use ($self, $locale) {
65+
$self->dumpCatalogue($locale, $cache);
66+
}
67+
);
68+
69+
/* Read catalogue from cache. */
70+
return include $cache->getPath();
71+
}
72+
73+
/**
74+
* @internal
75+
*/
76+
public function initializeCatalogue($locale)
77+
{
78+
unlink($this->getCatalogueCachePath($locale));
79+
80+
return $this->getCatalogue($locale);
81+
}
82+
83+
/**
84+
* This method is public because it needs to be callable from a closure in PHP 5.3. It should be made protected (or even private, if possible) in 3.0.
85+
*
86+
* @internal
87+
*/
88+
public function dumpCatalogue($locale, ConfigCacheInterface $cache)
89+
{
90+
$catalogue = $this->translatorBag->getCatalogue($locale);
91+
$fallbackContent = $this->getFallbackContent($catalogue);
92+
93+
$content = sprintf(<<<EOF
94+
<?php
95+
96+
use Symfony\Component\Translation\MessageCatalogue;
97+
98+
\$catalogue = new MessageCatalogue('%s', %s);
99+
100+
%s
101+
return \$catalogue;
102+
103+
EOF
104+
,
105+
$locale,
106+
var_export($catalogue->all(), true),
107+
$fallbackContent
108+
);
109+
110+
$cache->write($content, $catalogue->getResources());
111+
}
112+
113+
private function getFallbackContent(MessageCatalogue $catalogue)
114+
{
115+
$fallbackContent = '';
116+
$current = '';
117+
$replacementPattern = '/[^a-z0-9_]/i';
118+
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
119+
while ($fallbackCatalogue) {
120+
$fallback = $fallbackCatalogue->getLocale();
121+
$fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
122+
$currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
123+
124+
$fallbackContent .= sprintf(<<<EOF
125+
\$catalogue%s = new MessageCatalogue('%s', %s);
126+
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
127+
128+
EOF
129+
,
130+
$fallbackSuffix,
131+
$fallback,
132+
var_export($fallbackCatalogue->all(), true),
133+
$currentSuffix,
134+
$fallbackSuffix
135+
);
136+
$current = $fallbackCatalogue->getLocale();
137+
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
138+
}
139+
140+
return $fallbackContent;
141+
}
142+
143+
private function getCatalogueCachePath($locale)
144+
{
145+
return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->translatorBag)).'.php';
146+
}
147+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Translation\Loader;
13+
14+
/**
15+
* @author Abdellatif Ait boudad <[email protected]>
16+
*/
17+
class ContainerAwareTranslatorBag extends TranslatorBag
18+
{
19+
/**
20+
* The container from where services are loaded.
21+
*
22+
* @var ContainerInterface
23+
*/
24+
private $container;
25+
26+
/**
27+
* @var array
28+
*/
29+
private $loaderIds;
30+
31+
/**
32+
* @param ContainerInterface $container A ContainerInterface instance
33+
* @param array $loaderIds
34+
*/
35+
public function __construct(ContainerInterface $container, $loaderIds)
36+
{
37+
$this->container = $container;
38+
$this->loaderIds = $loaderIds;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getLoaders()
45+
{
46+
foreach ($this->loaderIds as $id => $aliases) {
47+
foreach ($aliases as $alias) {
48+
$this->addLoader($alias, $this->container->get($id));
49+
}
50+
}
51+
52+
return parent::getLoaders();
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function getResources($locale)
59+
{
60+
if ($this->container->hasParameter('translator_resources_'.$locale)) {
61+
return $this->container->getParameter('translator_resources_'.$locale);
62+
}
63+
64+
return array();
65+
}
66+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Translation;
13+
14+
/**
15+
* @author Abdellatif Ait boudad <[email protected]>
16+
*/
17+
class FallbackTranslatorBag implements TranslatorBagInterface
18+
{
19+
private $translatorBag;
20+
21+
/**
22+
* @var array
23+
*/
24+
private $fallbackLocales = array();
25+
26+
public function __construct($fallbackLocales = array(), TranslatorBagInterface $translatorBag = null)
27+
{
28+
$this->translatorBag = $translatorBag ?: new TranslatorBag();
29+
$this->fallbackLocales = $fallbackLocales;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getCatalogue($locale = null)
36+
{
37+
$catalogue = $this->translatorBag->getCatalogue($locale);
38+
$this->loadFallbackCatalogues($catalogue, $locale);
39+
40+
return $catalogue;
41+
}
42+
43+
private function loadFallbackCatalogues($catalogue, $locale)
44+
{
45+
$current = $catalogue;
46+
47+
foreach ($this->computeFallbackLocales($locale) as $fallback) {
48+
$catalogue = $this->translatorBag->getCatalogue($fallback);
49+
50+
$current->addFallbackCatalogue($catalogue);
51+
$current = $catalogue;
52+
}
53+
}
54+
55+
private function computeFallbackLocales($locale)
56+
{
57+
$locales = array();
58+
foreach ($this->fallbackLocales as $fallback) {
59+
if ($fallback === $locale) {
60+
continue;
61+
}
62+
63+
$locales[] = $fallback;
64+
}
65+
66+
if (strrchr($locale, '_') !== false) {
67+
array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
68+
}
69+
70+
return array_unique($locales);
71+
}
72+
73+
/**
74+
* Sets the fallback locales.
75+
*
76+
* @param array $locales The fallback locales
77+
*
78+
* @throws \InvalidArgumentException If a locale contains invalid characters
79+
*
80+
* @api
81+
*/
82+
public function setFallbackLocales(array $locales)
83+
{
84+
$this->fallbackLocales = $locales;
85+
}
86+
87+
/**
88+
* Gets the fallback locales.
89+
*
90+
* @return array $locales The fallback locales
91+
*
92+
* @api
93+
*/
94+
public function getFallbackLocales()
95+
{
96+
return $this->fallbackLocales;
97+
}
98+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Translation\Tests;
13+
14+
use Symfony\Component\Translation\Translator;
15+
16+
/**
17+
* @group legacy
18+
*/
19+
class LegacyTranslatorCacheTest extends TranslatorCacheTest
20+
{
21+
protected function getTranslator($locale, $debug, $loaders = array(), $resources = array(), $fallbackLocales = array())
22+
{
23+
$translator = new Translator($locale, null, $this->tmpDir, $debug);
24+
$translator->setFallbackLocales($fallbackLocales);
25+
foreach ($loaders as $format => $loader) {
26+
$translator->addLoader($format, $loader);
27+
}
28+
29+
foreach ($resources as $resource) {
30+
$translator->addResource($resource[0], $resource[1], $resource[2], isset($resource[3]) ? $resource[3] : null);
31+
}
32+
33+
return $translator;
34+
}
35+
}

0 commit comments

Comments
 (0)