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

Skip to content

Commit 7cbca33

Browse files
committed
Refactoring of the Bundle
1 parent 374c2af commit 7cbca33

33 files changed

Lines changed: 2605 additions & 312 deletions

DependencyInjection/Compiler/GuesserCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class GuesserCompilerPass implements CompilerPassInterface
2323
{
2424
/**
25-
* Compilerpass for Timezone Guessers
25+
* Compilerpass for Locale Guessers
2626
*
2727
* @param ContainerBuilder $container
2828
*/

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function getConfigTreeBuilder()
2929

3030
$rootNode
3131
->children()
32+
->scalarNode('strict_mode')
33+
->defaultFalse()
34+
->end()
3235
->arrayNode('allowed_locales')
3336
->requiresAtLeastOneElement()
3437
->prototype('scalar')->end()

DependencyInjection/LuneticsLocaleExtension.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Symfony\Component\Config\FileLocator;
1414
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1515
use Symfony\Component\DependencyInjection\Loader;
16-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
16+
use Symfony\Component\Yaml\Parser as YamlParser;
1717

1818
/**
1919
* This is the class that loads and manages your bundle configuration
@@ -30,11 +30,33 @@ public function load(array $configs, ContainerBuilder $container)
3030
$configuration = new Configuration();
3131
$config = $this->processConfiguration($configuration, $configs);
3232
$this->bindParameters($container, $this->getAlias(), $config);
33-
$container->setParameter('lunetics_locale.intl_extension_installed', extension_loaded('intl'));
33+
34+
// Fallback for missing intl extension
35+
$intlExtensionInstalled = extension_loaded('intl');
36+
$container->setParameter('lunetics_locale.intl_extension_installed', $intlExtensionInstalled);
37+
$iso3166 = array();
38+
$iso639one = array();
39+
$iso639two = array();
40+
$localeScript = array();
41+
if (!$intlExtensionInstalled) {
42+
$yamlParser = new YamlParser();
43+
$file = new FileLocator(__DIR__ . '/../Resources/config');
44+
$iso3166 = $yamlParser->parse(file_get_contents($file->locate('iso3166-1-alpha-2.yml')));
45+
$iso639one = $yamlParser->parse(file_get_contents($file->locate('iso639-1.yml')));
46+
$iso639two = $yamlParser->parse(file_get_contents($file->locate('iso639-2.yml')));
47+
$localeScript = $yamlParser->parse(file_get_contents($file->locate('locale_script.yml')));
48+
}
49+
$container->setParameter('lunetics_locale.intl_extension_fallback.iso3166', $iso3166);
50+
$mergedValues = array_merge($iso639one, $iso639two);
51+
$container->setParameter('lunetics_locale.intl_extension_fallback.iso639', $mergedValues);
52+
$container->setParameter('lunetics_locale.intl_extension_fallback.script', $localeScript);
53+
54+
3455
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
56+
$loader->load('validator.xml');
57+
$loader->load('guessers.xml');
3558
$loader->load('services.xml');
3659
}
37-
3860
/**
3961
* {@inheritDoc}
4062
*/

EventListener/LocaleListener.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\EventDispatcher\Event;
1818
use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager;
1919
use Lunetics\LocaleBundle\Cookie\LocaleCookie;
20-
use Lunetics\LocaleBundle\Validator\LocaleValidator;
2120

2221
/**
2322
* Locale Listener
@@ -27,25 +26,43 @@
2726
*/
2827
class LocaleListener
2928
{
29+
/**
30+
* @var string Default framework locale
31+
*/
3032
private $defaultLocale;
3133

34+
/**
35+
* @var LocaleGuesserManager
36+
*/
3237
private $guesserManager;
3338

39+
/**
40+
* @var LoggerInterface
41+
*/
3442
private $logger;
3543

44+
/**
45+
* @var EventDispatcher
46+
*/
3647
private $dispatcher;
3748

49+
/**
50+
* @var LocaleCookie
51+
*/
3852
private $localeCookie;
3953

54+
/**
55+
* @var string
56+
*/
4057
private $identifiedLocale;
4158

4259
/**
4360
* Construct the guessermanager
4461
*
45-
* @param string $defaultLocale
46-
* @param LocaleGuesserManager $guesserManager
47-
* @param LocaleCookie $localeCookie
48-
* @param LoggerInterface $logger
62+
* @param string $defaultLocale Framework default locale
63+
* @param LocaleGuesserManager $guesserManager Locale Guesser Manager
64+
* @param LocaleCookie $localeCookie Locale Cookie
65+
* @param LoggerInterface $logger Logger
4966
*/
5067
public function __construct($defaultLocale = 'en', LocaleGuesserManager $guesserManager, LocaleCookie $localeCookie, LoggerInterface $logger = null)
5168
{
@@ -77,8 +94,6 @@ public function onKernelRequest(GetResponseEvent $event)
7794

7895
$manager = $this->guesserManager;
7996
if ($locale = $manager->runLocaleGuessing($request)) {
80-
$validator = new LocaleValidator();
81-
$validator->validate($locale);
8297
$this->logEvent('Setting [ %s ] as defaultLocale for the Request', $locale);
8398
$request->setLocale($locale);
8499
$this->identifiedLocale = $locale;
@@ -124,6 +139,8 @@ public function addCookieResponseListener()
124139
* Called at the kernel.response event to attach the cookie to the request
125140
*
126141
* @param Event $event
142+
*
143+
* @return \Symfony\Component\HttpFoundation\Response;
127144
*/
128145
public function onResponse(Event $event)
129146
{

LocaleGuesser/BrowserLocaleGuesser.php

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Lunetics\LocaleBundle\LocaleGuesser;
1111

1212
use Symfony\Component\HttpFoundation\Request;
13+
use Lunetics\LocaleBundle\Validator\MetaValidator;
1314

1415
/**
1516
* Locale Guesser for detecing the locale from the browser Accept-language string
@@ -19,85 +20,73 @@
1920
*/
2021
class BrowserLocaleGuesser implements LocaleGuesserInterface
2122
{
22-
private $allowedLocales;
23-
23+
/**
24+
* @var string
25+
*/
2426
private $identifiedLocale;
2527

28+
/**
29+
* @var bool
30+
*/
2631
private $intlExtension;
2732

33+
/**
34+
* @var MetaValidator
35+
*/
36+
private $metaValidator;
37+
2838
/**
2939
* Constructor
3040
*
31-
* @param array $allowedLocales Array of allowed locales
32-
* @param bool $intlExtensionInstalled Wether the intl extension is installed
41+
* @param MetaValidator $metaValidator MetaValidator
42+
* @param bool $intlExtensionInstalled Wether the intl extension is installed
3343
*/
34-
public function __construct(array $allowedLocales, $intlExtensionInstalled = false)
44+
public function __construct(MetaValidator $metaValidator, $intlExtensionInstalled = false)
3545
{
36-
$this->allowedLocales = $allowedLocales;
46+
$this->metaValidator = $metaValidator;
3747
$this->intlExtension = $intlExtensionInstalled;
3848
}
3949

4050
/**
41-
* Guess the locale based on browser settings
51+
* Guess the locale based on the browser settings
4252
*
4353
* @param Request $request
4454
*
4555
* @return boolean
4656
*/
4757
public function guessLocale(Request $request)
4858
{
59+
$validator = $this->metaValidator;
4960
// Get the preferred locale from the Browser.
5061
$preferredLocale = $request->getPreferredLanguage();
5162
$availableLocales = $request->getLanguages();
52-
$allowedLocales = $this->allowedLocales;
53-
5463

5564
if (!$preferredLocale OR count($availableLocales) === 0) {
5665
return false;
5766
}
5867

5968
// If the preferred primary locale is allowed, return the locale.
60-
if (in_array($preferredLocale, $allowedLocales)) {
69+
if ($validator->isAllowed($preferredLocale)) {
6170
$this->identifiedLocale = $preferredLocale;
6271

6372
return true;
6473
}
6574

66-
if ($this->intlExtension) {
67-
$primaryLanguage = \Locale::getPrimaryLanguage($preferredLocale);
68-
} else {
69-
$primaryLanguage = $this->getPrimaryLanguage($preferredLocale);
70-
}
71-
72-
if (!in_array($primaryLanguage, $allowedLocales)) {
73-
74-
// Try to find a full locale (Language + country)
75-
$matchLocale = function ($v) use ($allowedLocales) {
76-
if (in_array($v, $allowedLocales)) {
77-
return true;
78-
}
79-
80-
return false;
81-
};
75+
/**$primaryLanguage = $this->getPrimaryLanguage($preferredLocale);
76+
if ($validator->isAllowed($primaryLanguage)) {
77+
$this->identifiedLocale = $preferredLocale;
8278
83-
$result = array_values(array_filter($availableLocales, $matchLocale));
79+
return true;
80+
}**/
8481

85-
if (!empty($result)) {
86-
$this->identifiedLocale = $result[0];
82+
// Get a list of available and allowed locales and return the first result
83+
$matchLocale = function ($v) use ($validator) {
84+
return $validator->isAllowed($v);
85+
};
8786

88-
return true;
89-
}
90-
91-
// Try to find a language
92-
$availableLanguages = $this->compileLanguageArray($availableLocales);
93-
$result = array_values(array_filter($availableLanguages, $matchLocale));
94-
if (!empty($result)) {
95-
$this->identifiedLocale = $result[0];
96-
97-
return true;
98-
}
99-
} else {
100-
$this->identifiedLocale = $preferredLocale;
87+
$result = array_values(array_filter($availableLocales, $matchLocale));
88+
if (!empty($result)) {
89+
$this->identifiedLocale = $result[0];
10190

10291
return true;
10392
}
@@ -114,36 +103,12 @@ public function guessLocale(Request $request)
114103
*/
115104
private function getPrimaryLanguage($locale)
116105
{
117-
$primaryLanguage = substr($locale, 0, 2);
118-
if (preg_match('/[a-z]{2}/', $primaryLanguage)) {
119-
return $primaryLanguage;
120-
}
121-
122-
return null;
123-
}
124-
125-
/**
126-
* Compiles a list of all available languages from a locale list
127-
*
128-
* @param array $availableLocales
129-
*
130-
* @return array
131-
*/
132-
private function compileLanguageArray(array $availableLocales = array())
133-
{
134-
$providedLanguages = array();
135-
foreach ($availableLocales as $locale) {
136-
if ($this->intlExtension) {
137-
$language = \Locale::getPrimaryLanguage($locale);
138-
} else {
139-
$language = $this->getPrimaryLanguage($locale);
140-
}
141-
if ($language) {
142-
$providedLanguages[] = $language;
143-
}
106+
if ($this->intlExtension) {
107+
return \Locale::getPrimaryLanguage($locale);
144108
}
109+
$splittedLocale = explode('_', $locale);
145110

146-
return array_unique($providedLanguages);
111+
return count($splittedLocale) > 1 ? $splittedLocale[0] : $locale;
147112
}
148113

149114
/**

LocaleGuesser/CookieLocaleGuesser.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,53 @@
1010
namespace Lunetics\LocaleBundle\LocaleGuesser;
1111

1212
use Symfony\Component\HttpFoundation\Request;
13+
use Lunetics\LocaleBundle\Validator\MetaValidator;
1314

1415
/**
15-
* @author Christophe Willemsen <[email protected]/>
16+
* Cookie Guesser for retrieving a previously detected locale from a cookie
17+
*
18+
* @author Matthias Breddin <[email protected]>
19+
* @author Christophe Willemsen <[email protected]>
1620
*/
1721
class CookieLocaleGuesser implements LocaleGuesserInterface
1822
{
23+
/**
24+
* @var string
25+
*/
1926
private $identifiedLocale;
2027

28+
/**
29+
* @var string
30+
*/
2131
private $localeCookieName;
2232

23-
public function __construct($localeCookieName)
33+
/**
34+
* @var MetaValidator
35+
*/
36+
private $metaValidator;
37+
38+
/**
39+
* Constructor
40+
*
41+
* @param MetaValidator $metaValidator MetaValidator
42+
* @param string $localeCookieName Name of the cookie var
43+
*/
44+
public function __construct(MetaValidator $metaValidator, $localeCookieName)
2445
{
46+
$this->metaValidator = $metaValidator;
2547
$this->localeCookieName = $localeCookieName;
2648
}
2749

50+
/**
51+
* Retrieve from cookie
52+
*
53+
* @param Request $request Request
54+
*
55+
* @return bool
56+
*/
2857
public function guessLocale(Request $request)
2958
{
30-
if ($request->cookies->has($this->localeCookieName)) {
59+
if ($request->cookies->has($this->localeCookieName) && $this->metaValidator->isAllowed($request->cookies->get($this->localeCookieName))) {
3160
$this->identifiedLocale = $request->cookies->get($this->localeCookieName);
3261

3362
return true;
@@ -36,6 +65,9 @@ public function guessLocale(Request $request)
3665
return false;
3766
}
3867

68+
/**
69+
* {@inheritDoc}
70+
*/
3971
public function getIdentifiedLocale()
4072
{
4173
if (null === $this->identifiedLocale) {

LocaleGuesser/LocaleGuesserInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface LocaleGuesserInterface
2727
public function guessLocale(Request $request);
2828

2929
/**
30-
* @return mixed
30+
* @return bool|mixed|string
3131
*/
3232
public function getIdentifiedLocale();
3333
}

LocaleGuesser/LocaleGuesserManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@
2525
*/
2626
class LocaleGuesserManager
2727
{
28+
/**
29+
* @var array
30+
*/
2831
private $guessingOrder;
2932

33+
/**
34+
* @var array
35+
*/
3036
private $guessers;
3137

38+
/**
39+
* @var LoggerInterface
40+
*/
3241
private $logger;
3342

3443
/**

0 commit comments

Comments
 (0)