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

Skip to content

Commit 221fd86

Browse files
committed
Merge pull request lunetics-org#179 from Sententiaregum/sf3-support
[WIP] Symfony 3 support
2 parents 9c10e9b + 422761c commit 221fd86

12 files changed

Lines changed: 80 additions & 145 deletions

File tree

.travis.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
64
- 5.5
75
- 5.6
86
- 7
7+
- hhvm
8+
9+
matrix:
10+
allow_failures:
11+
- php: hhvm
912

1013
env:
11-
- SYMFONY_VERSION=2.3.*
12-
- SYMFONY_VERSION=2.5.*
13-
- SYMFONY_VERSION=2.6.*
14-
- SYMFONY_VERSION='2.7.* symfony/phpunit-bridge:2.7.*'
14+
- SYMFONY_VERSION='2.8.*'
15+
- SYMFONY_VERSION='3.0.*'
1516

1617
before_script:
1718
- php -i | grep "ICU version"
@@ -25,3 +26,9 @@ notifications:
2526
email:
2627
2728
29+
30+
sudo: false
31+
32+
cache:
33+
directories:
34+
- $HOME/.composer/cache

Form/Extension/ChoiceList/LocaleChoiceList.php

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
namespace Lunetics\LocaleBundle\Form\Extension\ChoiceList;
1313

14-
use Symfony\Component\Locale\Locale;
1514
use Lunetics\LocaleBundle\LocaleInformation\LocaleInformation;
16-
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
17-
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
15+
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
16+
use Symfony\Component\Intl\Intl;
1817

1918
/**
2019
* Locale Choicelist Class
2120
*/
22-
class LocaleChoiceList extends SimpleChoiceList
21+
class LocaleChoiceList extends ArrayChoiceList
2322
{
2423
private $localeChoices;
2524
private $preferredChoices;
@@ -34,57 +33,28 @@ class LocaleChoiceList extends SimpleChoiceList
3433
public function __construct(LocaleInformation $information, $languagesOnly = true, $strictMode = false)
3534
{
3635
$this->localeChoices = array();
37-
38-
if ($strictMode) {
39-
$allowedLocales = $information->getAllowedLocalesFromConfiguration();
40-
} else {
41-
$allowedLocales = $information->getAllAllowedLanguages();
42-
}
36+
$allowedLocales = $strictMode
37+
? $information->getAllowedLocalesFromConfiguration()
38+
: $information->getAllAllowedLanguages();
4339

4440
foreach ($allowedLocales as $locale) {
4541
if ($languagesOnly && strlen($locale) == 2 || !$languagesOnly) {
46-
$this->localeChoices[$locale] = Locale::getDisplayName($locale, $locale);
42+
$this->localeChoices[$locale] = Intl::getLanguageBundle()->getLanguageName($locale, $locale);
4743
}
4844
}
4945

5046
$this->preferredChoices = $information->getPreferredLocales();
5147

52-
parent::__construct($this->localeChoices, $this->preferredChoices);
48+
parent::__construct($this->localeChoices);
5349
}
5450

5551
/**
56-
* Returns the preferred views, sorted by the ->preferredChoices list
52+
* Returns the preferred choices
5753
*
5854
* @return array|void
5955
*/
60-
public function getPreferredViews()
56+
public function getPreferredChoices()
6157
{
62-
$preferredViews = parent::getPreferredViews();
63-
$result = array();
64-
foreach ($this->preferredChoices as $pchoice) {
65-
foreach ($preferredViews as $view) {
66-
if ($pchoice == $view->data) {
67-
$result[] = $view;
68-
}
69-
}
70-
}
71-
72-
return $result;
58+
return $this->preferredChoices;
7359
}
74-
75-
/**
76-
* Returns the remaining locales sorted by language name
77-
*
78-
* @return array
79-
*/
80-
public function getRemainingViews()
81-
{
82-
$remainingViews = parent::getRemainingViews();
83-
usort($remainingViews, function (ChoiceView $choiceView1, ChoiceView $choiceView2) {
84-
return \Collator::create(null)->compare($choiceView1->label, $choiceView2->label);
85-
});
86-
87-
return $remainingViews;
88-
}
89-
90-
}
60+
}

Form/Extension/Type/LocaleType.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
namespace Lunetics\LocaleBundle\Form\Extension\Type;
1313

14-
1514
use Symfony\Component\Form\AbstractType;
1615
use Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList;
17-
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1817

18+
/**
19+
* Custom choice type for locales.
20+
*/
1921
class LocaleType extends AbstractType
2022
{
21-
2223
/**
2324
* @var LocaleChoiceList
2425
*/
@@ -31,15 +32,17 @@ public function __construct(LocaleChoiceList $choiceList)
3132
{
3233
$this->choiceList = $choiceList;
3334
}
35+
3436
/**
3537
* {@inheritdoc}
3638
*/
37-
public function setDefaultOptions(OptionsResolverInterface $resolver)
39+
public function configureOptions(OptionsResolver $resolver)
3840
{
39-
parent::setDefaultOptions($resolver);
41+
parent::configureOptions($resolver);
4042

4143
$resolver->setDefaults(array(
42-
'choice_list' => $this->choiceList,
44+
'choices' => $this->choiceList->getOriginalKeys(),
45+
'preferred_choices' => $this->choiceList->getPreferredChoices(),
4346
));
4447
}
4548

@@ -48,13 +51,13 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
4851
*/
4952
public function getParent()
5053
{
51-
return 'choice';
54+
return 'Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType';
5255
}
5356

5457
/**
5558
* {@inheritdoc}
5659
*/
57-
public function getName()
60+
public function getBlockPrefix()
5861
{
5962
return 'lunetics_locale';
6063
}

LocaleInformation/LocaleInformation.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@
1818
*/
1919
class LocaleInformation
2020
{
21+
/**
22+
* @var MetaValidator
23+
*/
2124
private $metaValidator;
25+
26+
/**
27+
* @var LocaleGuesserManager
28+
*/
2229
private $manager;
30+
31+
/**
32+
* @var AllowedLocalesProvider
33+
*/
2334
private $allowedLocalesProvider;
2435

2536
/**
@@ -79,7 +90,6 @@ public function getPreferredLocales()
7990

8091
// Make sure we return an array even if no preferred locale can be found
8192
return $preferredLocales ?: array();
82-
8393
}
8494

8595
/**
@@ -102,4 +112,4 @@ private function filterAllowed(array $localeList)
102112

103113
return false;
104114
}
105-
}
115+
}

Matcher/DefaultBestLocaleMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function match($locale)
4646
if (strpos($locale, $allowedLocale)===0) {
4747
return $allowedLocale;
4848
}
49-
}
50-
return false;
49+
}
50+
return false;
5151
}
5252
}

Resources/config/form.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<service id="form.type.lunetics_locale" class="%form.type.lunetics_locale.class%">
1919
<argument type="service" id="lunetics_locale.form.locale.choicelist" />
20-
<tag name="form.type" alias="lunetics_locale" />
20+
<tag name="form.type" alias="lunetics_locale" />
2121
</service>
2222

2323
</services>

Resources/config/switcher.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<tag name="twig.extension"/>
2323
</service>
2424

25-
<service id="lunetics_locale.switcher_controller" class="%lunetics_locale.switcher_controller.class%" scope="request">
25+
<service id="lunetics_locale.switcher_controller" class="%lunetics_locale.switcher_controller.class%">
2626
<argument type="service" id="router"/>
2727
<argument type="service" id="lunetics_locale.validator.meta" />
2828
<argument>%lunetics_locale.switcher.use_referrer%</argument>

Tests/Form/Extension/ChoiceList/LegacyLocaleChoiceListTest.php renamed to Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
*/
1010
namespace Lunetics\LocaleBundle\Tests\Form\Extension\ChoiceList;
1111

12-
use Lunetics\LocaleBundle\Tests\Validator\BaseMetaValidator;
1312
use Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList;
1413

15-
1614
/**
1715
* Test for the LocaleInformation
1816
*
1917
* @author Matthias Breddin <[email protected]>
2018
*/
21-
class LegacyLocaleChoiceListTest extends \PHPUnit_Framework_TestCase
19+
class LocaleChoiceListTest extends \PHPUnit_Framework_TestCase
2220
{
2321

2422
public function testDefaultChoiceList()
@@ -34,7 +32,7 @@ public function testDefaultChoiceList()
3432

3533
$list = new LocaleChoiceList($information);
3634
$result = array('nl', 'de');
37-
$this->assertEquals($result, $list->getChoices());
35+
$this->assertEquals($result, array_values($list->getOriginalKeys()));
3836
}
3937

4038
public function testStrictMode()
@@ -50,7 +48,7 @@ public function testStrictMode()
5048

5149
$list = new LocaleChoiceList($information, true, true);
5250
$result = array('en', 'de', 'nl');
53-
$this->assertEquals($result, $list->getChoices());
51+
$this->assertEquals($result, array_values($list->getOriginalKeys()));
5452
}
5553

5654
public function testNotLanguagesOnly()
@@ -66,7 +64,7 @@ public function testNotLanguagesOnly()
6664

6765
$list = new LocaleChoiceList($information, false);
6866
$result = array('nl', 'nl_BE', 'de', 'de_AT', 'de_CH');
69-
$this->assertEquals($result, $list->getChoices());
67+
$this->assertEquals($result, array_values($list->getOriginalKeys()));
7068
}
7169

7270
public function testNotLanguagesOnlyStrictMode()
@@ -82,7 +80,7 @@ public function testNotLanguagesOnlyStrictMode()
8280

8381
$list = new LocaleChoiceList($information, false, true);
8482
$result = array('en', 'de', 'nl', 'de_AT');
85-
$this->assertEquals($result, $list->getChoices());
83+
$this->assertEquals($result, array_values($list->getOriginalKeys()));
8684
}
8785

8886
public function testPreferredLocalesSorted()
@@ -97,19 +95,15 @@ public function testPreferredLocalesSorted()
9795
->will($this->returnValue(array('de', 'nl', 'en')));
9896

9997
$list = new LocaleChoiceList($information);
100-
$preferredResult = $list->getPreferredViews();
101-
$remainingResults = $list->getRemainingViews();
102-
103-
$this->assertEquals('de', $preferredResult[0]->value);
104-
$this->assertEquals('nl', $preferredResult[1]->value);
105-
$this->assertEquals('en', $preferredResult[2]->value);
98+
$preferredResult = $list->getPreferredChoices();
10699

107-
$this->assertEquals('fr', $remainingResults[0]->value);
108-
$this->assertEquals('tr', $remainingResults[1]->value);
100+
$this->assertEquals('de', $preferredResult[0]);
101+
$this->assertEquals('nl', $preferredResult[1]);
102+
$this->assertEquals('en', $preferredResult[2]);
109103
}
110104

111105
public function getLocaleInformation()
112106
{
113107
return $this->getMockBuilder('Lunetics\LocaleBundle\LocaleInformation\LocaleInformation')->disableOriginalConstructor()->getMock();
114108
}
115-
}
109+
}

Tests/Form/Extension/Type/LocaleTypeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616
*/
1717
class LocaleTypeTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testSetDefaultOptions()
19+
public function testConfigureOptions()
2020
{
2121
$choiceList = $this->getMockLocaleChoiceList();
2222

2323
$resolver = $this->getMockOptionsResolverInterface();
2424
$resolver
2525
->expects($this->once())
2626
->method('setDefaults')
27-
->with(array('choice_list' => $choiceList));
27+
->with(array('choices' => null, 'preferred_choices' => null));
2828

2929
$type = new LocaleType($choiceList);
30-
$type->setDefaultOptions($resolver);
30+
$type->configureOptions($resolver);
3131
}
3232

3333
public function testGetParent()
3434
{
3535
$type = new LocaleType($this->getMockLocaleChoiceList());
3636

37-
$this->assertEquals('choice', $type->getParent());
37+
$this->assertEquals('Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType', $type->getParent());
3838
}
3939

4040
public function testGetName()
4141
{
4242
$type = new LocaleType($this->getMockLocaleChoiceList());
4343

44-
$this->assertEquals('lunetics_locale', $type->getName());
44+
$this->assertEquals('lunetics_locale', $type->getBlockPrefix());
4545
}
4646

4747
protected function getMockLocaleChoiceList()
@@ -57,4 +57,4 @@ protected function getMockOptionsResolverInterface()
5757
{
5858
return $this->getMock('Symfony\Component\OptionsResolver\OptionsResolver');
5959
}
60-
}
60+
}

Tests/LocaleInformation/LegacyLocaleInformationTest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)