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

Skip to content

Commit 8298b1d

Browse files
committed
[Form] optimize ChoiceType children by caching choices
1 parent c5c63dc commit 8298b1d

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
class CountryType extends AbstractType
1919
{
20+
/**
21+
* Stores the available country choices.
22+
*
23+
* @var array
24+
*/
25+
private static $countries;
26+
2027
/**
2128
* {@inheritdoc}
2229
*/
2330
public function configureOptions(OptionsResolver $resolver)
2431
{
2532
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getRegionBundle()->getCountryNames()),
33+
'choices' => self::getCountries(),
2734
'choice_translation_domain' => false,
2835
));
2936
}
@@ -43,4 +50,25 @@ public function getBlockPrefix()
4350
{
4451
return 'country';
4552
}
53+
54+
/**
55+
* Returns the country choices.
56+
*
57+
* The choices are generated from the Intl component
58+
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
59+
*
60+
* They are cached during a single request, so multiple
61+
* country fields on the same page don't lead to
62+
* unnecessary overhead.
63+
*
64+
* @return array The country choices
65+
*/
66+
private static function getCountries()
67+
{
68+
if (null === self::$countries) {
69+
self::$countries = array_flip(Intl::getRegionBundle()->getCountryNames());
70+
}
71+
72+
return self::$countries;
73+
}
4674
}

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
class CurrencyType extends AbstractType
1919
{
20+
/**
21+
* Stores the available currency choices.
22+
*
23+
* @var array
24+
*/
25+
private static $currencies;
26+
2027
/**
2128
* {@inheritdoc}
2229
*/
2330
public function configureOptions(OptionsResolver $resolver)
2431
{
2532
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getCurrencyBundle()->getCurrencyNames()),
33+
'choices' => self::getCurrencies(),
2734
'choice_translation_domain' => false,
2835
));
2936
}
@@ -43,4 +50,25 @@ public function getBlockPrefix()
4350
{
4451
return 'currency';
4552
}
53+
54+
/**
55+
* Returns the currency choices.
56+
*
57+
* The choices are generated from the Intl component
58+
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
59+
*
60+
* They are cached during a single request, so multiple
61+
* currency fields on the same page don't lead to
62+
* unnecessary overhead.
63+
*
64+
* @return array The currency choices
65+
*/
66+
private static function getCurrencies()
67+
{
68+
if (null === self::$currencies) {
69+
self::$currencies = array_flip(Intl::getCurrencyBundle()->getCurrencyNames());
70+
}
71+
72+
return self::$currencies;
73+
}
4674
}

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
class LanguageType extends AbstractType
1919
{
20+
/**
21+
* Stores the available language choices.
22+
*
23+
* @var array
24+
*/
25+
private static $languages;
26+
2027
/**
2128
* {@inheritdoc}
2229
*/
2330
public function configureOptions(OptionsResolver $resolver)
2431
{
2532
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLanguageBundle()->getLanguageNames()),
33+
'choices' => self::getLanguages(),
2734
'choice_translation_domain' => false,
2835
));
2936
}
@@ -43,4 +50,25 @@ public function getBlockPrefix()
4350
{
4451
return 'language';
4552
}
53+
54+
/**
55+
* Returns the language choices.
56+
*
57+
* The choices are generated from the Intl component
58+
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
59+
*
60+
* They are cached during a single request, so multiple
61+
* language fields on the same page don't lead to
62+
* unnecessary overhead.
63+
*
64+
* @return array The language choices
65+
*/
66+
private static function getLanguages()
67+
{
68+
if (null === self::$languages) {
69+
self::$languages = array_flip(Intl::getLanguageBundle()->getLanguageNames());
70+
}
71+
72+
return self::$languages;
73+
}
4674
}

src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717

1818
class LocaleType extends AbstractType
1919
{
20+
/**
21+
* Stores the available locale choices.
22+
*
23+
* @var array
24+
*/
25+
private static $locales;
26+
2027
/**
2128
* {@inheritdoc}
2229
*/
2330
public function configureOptions(OptionsResolver $resolver)
2431
{
2532
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLocaleBundle()->getLocaleNames()),
33+
'choices' => self::getLocales(),
2734
'choice_translation_domain' => false,
2835
));
2936
}
@@ -43,4 +50,25 @@ public function getBlockPrefix()
4350
{
4451
return 'locale';
4552
}
53+
54+
/**
55+
* Returns the locale choices.
56+
*
57+
* The choices are generated from the Intl component
58+
* {@link \Symfony\Component\Intl\Intl::getLocaleBundle()}.
59+
*
60+
* They are cached during a single request, so multiple
61+
* locale fields on the same page don't lead to
62+
* unnecessary overhead.
63+
*
64+
* @return array The locale choices
65+
*/
66+
private static function getLocales()
67+
{
68+
if (null === self::$locales) {
69+
self::$locales = array_flip(Intl::getLocaleBundle()->getLocaleNames());
70+
}
71+
72+
return self::$locales;
73+
}
4674
}

0 commit comments

Comments
 (0)