Skip to content

Commit

Permalink
[Intl] Moved stub data to Icu component 1.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Apr 5, 2013
1 parent dbca3b7 commit 0160fd5
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 3,254 deletions.
140 changes: 12 additions & 128 deletions src/Symfony/Component/Intl/Intl.php
Expand Up @@ -33,37 +33,12 @@
*/
class Intl
{
/**
* Load data from the Icu component.
*/
const ICU = 0;

/**
* Load data from the stub files of the Intl component.
*/
const STUB = 1;

/**
* The number of resource bundles to buffer. Loading the same resource
* bundle for n locales takes up n spots in the buffer.
*/
const BUFFER_SIZE = 10;

/**
* The accepted values for the {@link $dataSource} property.
*
* @var array
*/
private static $allowedDataSources = array(
self::ICU => 'Intl::ICU',
self::STUB => 'Intl::STUB',
);

/**
* @var integer
*/
private static $dataSource;

/**
* @var ResourceBundle\CurrencyBundleInterface
*/
Expand Down Expand Up @@ -97,12 +72,7 @@ class Intl
/**
* @var ResourceBundle\Reader\StructuredBundleReaderInterface
*/
private static $phpReader;

/**
* @var ResourceBundle\Reader\StructuredBundleReaderInterface
*/
private static $binaryReader;
private static $bundleReader;

/**
* Returns whether the intl extension is installed.
Expand All @@ -111,66 +81,7 @@ class Intl
*/
public static function isExtensionLoaded()
{
return IcuData::isLoadable();
}

/**
* Sets the data source from which to load the resource bundles.
*
* @param integer $dataSource One of the constants {@link Intl::ICU} or
* {@link Intl::STUB}.
*
* @throws InvalidArgumentException If the data source is invalid.
*
* @see getData>Source
*/
public static function setDataSource($dataSource)
{
if (!isset(self::$allowedDataSources[$dataSource])) {
throw new InvalidArgumentException(sprintf(
'The data sources should be one of %s',
implode(', ', self::$allowedDataSources)
));
}

if (self::ICU === $dataSource && !IcuData::isLoadable()) {
throw new InvalidArgumentException(
'The data source cannot be set to Intl::ICU if the intl ' .
'extension is not installed.'
);
}

if ($dataSource !== self::$dataSource) {
self::$currencyBundle = null;
self::$languageBundle = null;
self::$localeBundle = null;
self::$regionBundle = null;
}

self::$dataSource = $dataSource;
}

/**
* Returns the data source from which to load the resource bundles.
*
* If {@link setDataSource()} has not been called, the data source will be
* chosen depending on whether the intl extension is installed or not:
*
* * If the extension is present, the bundles will be loaded from the Icu
* component;
* * Otherwise, the bundles will be loaded from the stub files in the
* Intl component.
*
* @return integer One of the constants {@link Intl::ICU} or
* {@link Intl::STUB}.
*/
public static function getDataSource()
{
if (null === self::$dataSource) {
self::$dataSource = IcuData::isLoadable() ? self::ICU : self::STUB;
}

return self::$dataSource;
return class_exists('\ResourceBundle');
}

/**
Expand All @@ -181,9 +92,7 @@ public static function getDataSource()
public static function getCurrencyBundle()
{
if (null === self::$currencyBundle) {
self::$currencyBundle = self::ICU === self::getDataSource()
? new IcuCurrencyBundle(self::getBinaryReader())
: new StubCurrencyBundle(self::getPhpReader());
self::$currencyBundle = new IcuCurrencyBundle(self::getBundleReader());
}

return self::$currencyBundle;
Expand All @@ -197,9 +106,7 @@ public static function getCurrencyBundle()
public static function getLanguageBundle()
{
if (null === self::$languageBundle) {
self::$languageBundle = self::ICU === self::getDataSource()
? new IcuLanguageBundle(self::getBinaryReader())
: new StubLanguageBundle(self::getPhpReader());
self::$languageBundle = new IcuLanguageBundle(self::getBundleReader());
}

return self::$languageBundle;
Expand All @@ -213,9 +120,7 @@ public static function getLanguageBundle()
public static function getLocaleBundle()
{
if (null === self::$localeBundle) {
self::$localeBundle = self::ICU === self::getDataSource()
? new IcuLocaleBundle(self::getBinaryReader())
: new StubLocaleBundle(self::getPhpReader());
self::$localeBundle = new IcuLocaleBundle(self::getBundleReader());
}

return self::$localeBundle;
Expand All @@ -229,9 +134,7 @@ public static function getLocaleBundle()
public static function getRegionBundle()
{
if (null === self::$regionBundle) {
self::$regionBundle = self::ICU === self::getDataSource()
? new IcuRegionBundle(self::getBinaryReader())
: new StubRegionBundle(self::getPhpReader());
self::$regionBundle = new IcuRegionBundle(self::getBundleReader());
}

return self::$regionBundle;
Expand Down Expand Up @@ -275,9 +178,7 @@ public static function getIcuVersion()
public static function getIcuDataVersion()
{
if (false === self::$icuDataVersion) {
self::$icuDataVersion = self::ICU === self::getDataSource()
? IcuData::getVersion()
: file_get_contents(__DIR__ . '/Resources/version.txt');
self::$icuDataVersion = IcuData::getVersion();
}

return self::$icuDataVersion;
Expand All @@ -298,33 +199,16 @@ public static function getIcuStubVersion()
*
* @return ResourceBundle\Reader\StructuredBundleReaderInterface The resource reader.
*/
private static function getPhpReader()
{
if (null === self::$phpReader) {
self::$phpReader = new StructuredBundleReader(new BufferedBundleReader(
new PhpBundleReader(),
self::BUFFER_SIZE
));
}

return self::$phpReader;
}

/**
* Returns a resource bundle reader for binary .res resource bundle files.
*
* @return ResourceBundle\Reader\StructuredBundleReaderInterface The resource reader.
*/
private static function getBinaryReader()
private static function getBundleReader()
{
if (null === self::$binaryReader) {
self::$binaryReader = new StructuredBundleReader(new BufferedBundleReader(
new BinaryBundleReader(),
if (null === self::$bundleReader) {
self::$bundleReader = new StructuredBundleReader(new BufferedBundleReader(
IcuData::isLoadable() ? new BinaryBundleReader() : new PhpBundleReader(),
self::BUFFER_SIZE
));
}

return self::$binaryReader;
return self::$bundleReader;
}

/**
Expand Down

0 comments on commit 0160fd5

Please sign in to comment.