Intl.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  12. * Gives access to internationalization data.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. final class Intl
  17. {
  18. /**
  19. * The number of resource bundles to buffer. Loading the same resource
  20. * bundle for n locales takes up n spots in the buffer.
  21. */
  22. public const BUFFER_SIZE = 10;
  23. /**
  24. * The directory name of the currency data.
  25. */
  26. public const CURRENCY_DIR = 'currencies';
  27. /**
  28. * The directory name of the language data.
  29. */
  30. public const LANGUAGE_DIR = 'languages';
  31. /**
  32. * The directory name of the script data.
  33. */
  34. public const SCRIPT_DIR = 'scripts';
  35. /**
  36. * The directory name of the locale data.
  37. */
  38. public const LOCALE_DIR = 'locales';
  39. /**
  40. * The directory name of the region data.
  41. */
  42. public const REGION_DIR = 'regions';
  43. /**
  44. * The directory name of the zone data.
  45. */
  46. public const TIMEZONE_DIR = 'timezones';
  47. /**
  48. * @var string|bool|null
  49. */
  50. private static $icuVersion = false;
  51. /**
  52. * @var string
  53. */
  54. private static $icuDataVersion = false;
  55. /**
  56. * Returns whether the intl extension is installed.
  57. *
  58. * @return bool Returns true if the intl extension is installed, false otherwise
  59. */
  60. public static function isExtensionLoaded(): bool
  61. {
  62. return class_exists(\ResourceBundle::class);
  63. }
  64. /**
  65. * Returns the version of the installed ICU library.
  66. *
  67. * @return string|null The ICU version or NULL if it could not be determined
  68. */
  69. public static function getIcuVersion(): ?string
  70. {
  71. if (false === self::$icuVersion) {
  72. if (!self::isExtensionLoaded()) {
  73. self::$icuVersion = self::getIcuStubVersion();
  74. } elseif (\defined('INTL_ICU_VERSION')) {
  75. self::$icuVersion = \INTL_ICU_VERSION;
  76. } else {
  77. try {
  78. $reflector = new \ReflectionExtension('intl');
  79. ob_start();
  80. $reflector->info();
  81. $output = strip_tags(ob_get_clean());
  82. preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
  83. self::$icuVersion = trim($matches[1]);
  84. } catch (\ReflectionException $e) {
  85. self::$icuVersion = null;
  86. }
  87. }
  88. }
  89. return self::$icuVersion;
  90. }
  91. /**
  92. * Returns the version of the installed ICU data.
  93. *
  94. * @return string The version of the installed ICU data
  95. */
  96. public static function getIcuDataVersion(): string
  97. {
  98. if (false === self::$icuDataVersion) {
  99. self::$icuDataVersion = trim(file_get_contents(self::getDataDirectory().'/version.txt'));
  100. }
  101. return self::$icuDataVersion;
  102. }
  103. /**
  104. * Returns the ICU version that the stub classes mimic.
  105. *
  106. * @return string The ICU version of the stub classes
  107. */
  108. public static function getIcuStubVersion(): string
  109. {
  110. return '68.2';
  111. }
  112. /**
  113. * Returns the absolute path to the data directory.
  114. *
  115. * @return string The absolute path to the data directory
  116. */
  117. public static function getDataDirectory(): string
  118. {
  119. return __DIR__.'/Resources/data';
  120. }
  121. /**
  122. * This class must not be instantiated.
  123. */
  124. private function __construct()
  125. {
  126. }
  127. }