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