/src/Symfony/Component/Intl/Intl.php

https://github.com/gimler/symfony · PHP · 279 lines · 138 code · 35 blank · 106 comment · 9 complexity · 8c6af8c1b5baf26ebefe8d96c6b51510 MD5 · raw file

  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\Intl\Data\Bundle\Reader\BufferedBundleReader;
  12. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
  13. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  14. use Symfony\Component\Intl\Data\Bundle\Reader\JsonBundleReader;
  15. use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
  16. use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
  17. use Symfony\Component\Intl\ResourceBundle\CurrencyBundle;
  18. use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;
  19. use Symfony\Component\Intl\ResourceBundle\LanguageBundle;
  20. use Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface;
  21. use Symfony\Component\Intl\ResourceBundle\LocaleBundle;
  22. use Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface;
  23. use Symfony\Component\Intl\ResourceBundle\RegionBundle;
  24. use Symfony\Component\Intl\ResourceBundle\RegionBundleInterface;
  25. /**
  26. * Gives access to internationalization data.
  27. *
  28. * @author Bernhard Schussek <bschussek@gmail.com>
  29. */
  30. final class Intl
  31. {
  32. /**
  33. * The number of resource bundles to buffer. Loading the same resource
  34. * bundle for n locales takes up n spots in the buffer.
  35. */
  36. const BUFFER_SIZE = 10;
  37. /**
  38. * The directory name of the currency data.
  39. */
  40. const CURRENCY_DIR = 'currencies';
  41. /**
  42. * The directory name of the language data.
  43. */
  44. const LANGUAGE_DIR = 'languages';
  45. /**
  46. * The directory name of the script data.
  47. */
  48. const SCRIPT_DIR = 'scripts';
  49. /**
  50. * The directory name of the locale data.
  51. */
  52. const LOCALE_DIR = 'locales';
  53. /**
  54. * The directory name of the region data.
  55. */
  56. const REGION_DIR = 'regions';
  57. /**
  58. * @var ResourceBundle\CurrencyBundleInterface
  59. */
  60. private static $currencyBundle;
  61. /**
  62. * @var ResourceBundle\LanguageBundleInterface
  63. */
  64. private static $languageBundle;
  65. /**
  66. * @var ResourceBundle\LocaleBundleInterface
  67. */
  68. private static $localeBundle;
  69. /**
  70. * @var ResourceBundle\RegionBundleInterface
  71. */
  72. private static $regionBundle;
  73. /**
  74. * @var string|bool|null
  75. */
  76. private static $icuVersion = false;
  77. /**
  78. * @var string
  79. */
  80. private static $icuDataVersion = false;
  81. /**
  82. * @var BundleEntryReaderInterface
  83. */
  84. private static $entryReader;
  85. /**
  86. * Returns whether the intl extension is installed.
  87. *
  88. * @return bool Returns true if the intl extension is installed, false otherwise
  89. */
  90. public static function isExtensionLoaded()
  91. {
  92. return class_exists('\ResourceBundle');
  93. }
  94. /**
  95. * Returns the bundle containing currency information.
  96. *
  97. * @return CurrencyBundleInterface The currency resource bundle
  98. */
  99. public static function getCurrencyBundle()
  100. {
  101. if (null === self::$currencyBundle) {
  102. self::$currencyBundle = new CurrencyBundle(
  103. self::getDataDirectory().'/'.self::CURRENCY_DIR,
  104. self::getEntryReader(),
  105. self::getLocaleBundle()
  106. );
  107. }
  108. return self::$currencyBundle;
  109. }
  110. /**
  111. * Returns the bundle containing language information.
  112. *
  113. * @return LanguageBundleInterface The language resource bundle
  114. */
  115. public static function getLanguageBundle()
  116. {
  117. if (null === self::$languageBundle) {
  118. self::$languageBundle = new LanguageBundle(
  119. self::getDataDirectory().'/'.self::LANGUAGE_DIR,
  120. self::getEntryReader(),
  121. self::getLocaleBundle(),
  122. new ScriptDataProvider(
  123. self::getDataDirectory().'/'.self::SCRIPT_DIR,
  124. self::getEntryReader()
  125. )
  126. );
  127. }
  128. return self::$languageBundle;
  129. }
  130. /**
  131. * Returns the bundle containing locale information.
  132. *
  133. * @return LocaleBundleInterface The locale resource bundle
  134. */
  135. public static function getLocaleBundle()
  136. {
  137. if (null === self::$localeBundle) {
  138. self::$localeBundle = new LocaleBundle(
  139. self::getDataDirectory().'/'.self::LOCALE_DIR,
  140. self::getEntryReader()
  141. );
  142. }
  143. return self::$localeBundle;
  144. }
  145. /**
  146. * Returns the bundle containing region information.
  147. *
  148. * @return RegionBundleInterface The region resource bundle
  149. */
  150. public static function getRegionBundle()
  151. {
  152. if (null === self::$regionBundle) {
  153. self::$regionBundle = new RegionBundle(
  154. self::getDataDirectory().'/'.self::REGION_DIR,
  155. self::getEntryReader(),
  156. self::getLocaleBundle()
  157. );
  158. }
  159. return self::$regionBundle;
  160. }
  161. /**
  162. * Returns the version of the installed ICU library.
  163. *
  164. * @return null|string The ICU version or NULL if it could not be determined
  165. */
  166. public static function getIcuVersion()
  167. {
  168. if (false === self::$icuVersion) {
  169. if (!self::isExtensionLoaded()) {
  170. self::$icuVersion = self::getIcuStubVersion();
  171. } elseif (\defined('INTL_ICU_VERSION')) {
  172. self::$icuVersion = INTL_ICU_VERSION;
  173. } else {
  174. try {
  175. $reflector = new \ReflectionExtension('intl');
  176. ob_start();
  177. $reflector->info();
  178. $output = strip_tags(ob_get_clean());
  179. preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
  180. self::$icuVersion = trim($matches[1]);
  181. } catch (\ReflectionException $e) {
  182. self::$icuVersion = null;
  183. }
  184. }
  185. }
  186. return self::$icuVersion;
  187. }
  188. /**
  189. * Returns the version of the installed ICU data.
  190. *
  191. * @return string The version of the installed ICU data
  192. */
  193. public static function getIcuDataVersion()
  194. {
  195. if (false === self::$icuDataVersion) {
  196. self::$icuDataVersion = trim(file_get_contents(self::getDataDirectory().'/version.txt'));
  197. }
  198. return self::$icuDataVersion;
  199. }
  200. /**
  201. * Returns the ICU version that the stub classes mimic.
  202. *
  203. * @return string The ICU version of the stub classes
  204. */
  205. public static function getIcuStubVersion()
  206. {
  207. return '62.1';
  208. }
  209. /**
  210. * Returns the absolute path to the data directory.
  211. *
  212. * @return string The absolute path to the data directory
  213. */
  214. public static function getDataDirectory()
  215. {
  216. return __DIR__.'/Resources/data';
  217. }
  218. /**
  219. * Returns the cached bundle entry reader.
  220. *
  221. * @return BundleEntryReaderInterface The bundle entry reader
  222. */
  223. private static function getEntryReader()
  224. {
  225. if (null === self::$entryReader) {
  226. self::$entryReader = new BundleEntryReader(new BufferedBundleReader(
  227. new JsonBundleReader(),
  228. self::BUFFER_SIZE
  229. ));
  230. $localeDataProvider = new LocaleDataProvider(
  231. self::getDataDirectory().'/'.self::LOCALE_DIR,
  232. self::$entryReader
  233. );
  234. self::$entryReader->setLocaleAliases($localeDataProvider->getAliases());
  235. }
  236. return self::$entryReader;
  237. }
  238. /**
  239. * This class must not be instantiated.
  240. */
  241. private function __construct()
  242. {
  243. }
  244. }