Intl.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl;
  11. use Symfony\Component\Icu\IcuCurrencyBundle;
  12. use Symfony\Component\Icu\IcuData;
  13. use Symfony\Component\Icu\IcuLanguageBundle;
  14. use Symfony\Component\Icu\IcuLocaleBundle;
  15. use Symfony\Component\Icu\IcuRegionBundle;
  16. use Symfony\Component\Intl\ResourceBundle\Reader\BufferedBundleReader;
  17. use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReader;
  18. /**
  19. * Gives access to internationalization data.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. */
  23. class Intl
  24. {
  25. /**
  26. * The number of resource bundles to buffer. Loading the same resource
  27. * bundle for n locales takes up n spots in the buffer.
  28. */
  29. const BUFFER_SIZE = 10;
  30. /**
  31. * @var ResourceBundle\CurrencyBundleInterface
  32. */
  33. private static $currencyBundle;
  34. /**
  35. * @var ResourceBundle\LanguageBundleInterface
  36. */
  37. private static $languageBundle;
  38. /**
  39. * @var ResourceBundle\LocaleBundleInterface
  40. */
  41. private static $localeBundle;
  42. /**
  43. * @var ResourceBundle\RegionBundleInterface
  44. */
  45. private static $regionBundle;
  46. /**
  47. * @var string|Boolean|null
  48. */
  49. private static $icuVersion = false;
  50. /**
  51. * @var string
  52. */
  53. private static $icuDataVersion = false;
  54. /**
  55. * @var ResourceBundle\Reader\StructuredBundleReaderInterface
  56. */
  57. private static $bundleReader;
  58. /**
  59. * Returns whether the intl extension is installed.
  60. *
  61. * @return Boolean Returns true if the intl extension is installed, false otherwise.
  62. */
  63. public static function isExtensionLoaded()
  64. {
  65. return class_exists('\ResourceBundle');
  66. }
  67. /**
  68. * Returns the bundle containing currency information.
  69. *
  70. * @return ResourceBundle\CurrencyBundleInterface The currency resource bundle.
  71. */
  72. public static function getCurrencyBundle()
  73. {
  74. if (null === self::$currencyBundle) {
  75. self::$currencyBundle = new IcuCurrencyBundle(self::getBundleReader());
  76. }
  77. return self::$currencyBundle;
  78. }
  79. /**
  80. * Returns the bundle containing language information.
  81. *
  82. * @return ResourceBundle\LanguageBundleInterface The language resource bundle.
  83. */
  84. public static function getLanguageBundle()
  85. {
  86. if (null === self::$languageBundle) {
  87. self::$languageBundle = new IcuLanguageBundle(self::getBundleReader());
  88. }
  89. return self::$languageBundle;
  90. }
  91. /**
  92. * Returns the bundle containing locale information.
  93. *
  94. * @return ResourceBundle\LocaleBundleInterface The locale resource bundle.
  95. */
  96. public static function getLocaleBundle()
  97. {
  98. if (null === self::$localeBundle) {
  99. self::$localeBundle = new IcuLocaleBundle(self::getBundleReader());
  100. }
  101. return self::$localeBundle;
  102. }
  103. /**
  104. * Returns the bundle containing region information.
  105. *
  106. * @return ResourceBundle\RegionBundleInterface The region resource bundle.
  107. */
  108. public static function getRegionBundle()
  109. {
  110. if (null === self::$regionBundle) {
  111. self::$regionBundle = new IcuRegionBundle(self::getBundleReader());
  112. }
  113. return self::$regionBundle;
  114. }
  115. /**
  116. * Returns the version of the installed ICU library.
  117. *
  118. * @return null|string The ICU version or NULL if it could not be determined.
  119. */
  120. public static function getIcuVersion()
  121. {
  122. if (false === self::$icuVersion) {
  123. if (!self::isExtensionLoaded()) {
  124. self::$icuVersion = self::getIcuStubVersion();
  125. } elseif (defined('INTL_ICU_VERSION')) {
  126. self::$icuVersion = INTL_ICU_VERSION;
  127. } else {
  128. try {
  129. $reflector = new \ReflectionExtension('intl');
  130. ob_start();
  131. $reflector->info();
  132. $output = strip_tags(ob_get_clean());
  133. preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
  134. self::$icuVersion = trim($matches[1]);
  135. } catch (\ReflectionException $e) {
  136. self::$icuVersion = null;
  137. }
  138. }
  139. }
  140. return self::$icuVersion;
  141. }
  142. /**
  143. * Returns the version of the installed ICU data.
  144. *
  145. * @return string The version of the installed ICU data.
  146. */
  147. public static function getIcuDataVersion()
  148. {
  149. if (false === self::$icuDataVersion) {
  150. self::$icuDataVersion = IcuData::getVersion();
  151. }
  152. return self::$icuDataVersion;
  153. }
  154. /**
  155. * Returns the ICU version that the stub classes mimic.
  156. *
  157. * @return string The ICU version of the stub classes.
  158. */
  159. public static function getIcuStubVersion()
  160. {
  161. return '51.2';
  162. }
  163. /**
  164. * Returns a resource bundle reader for .php resource bundle files.
  165. *
  166. * @return ResourceBundle\Reader\StructuredBundleReaderInterface The resource reader.
  167. */
  168. private static function getBundleReader()
  169. {
  170. if (null === self::$bundleReader) {
  171. self::$bundleReader = new StructuredBundleReader(new BufferedBundleReader(
  172. IcuData::getBundleReader(),
  173. self::BUFFER_SIZE
  174. ));
  175. }
  176. return self::$bundleReader;
  177. }
  178. /**
  179. * This class must not be instantiated.
  180. */
  181. private function __construct() {}
  182. }