Skip to content

Commit

Permalink
[Intl] Improved bundle reader implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Sep 15, 2014
1 parent d35fd52 commit c3cce5c
Show file tree
Hide file tree
Showing 25 changed files with 573 additions and 253 deletions.
21 changes: 21 additions & 0 deletions src/Symfony/Component/Intl/Exception/MissingResourceException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl\Exception;

/**
* Thrown when an invalid entry of a resource bundle was requested.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MissingResourceException extends RuntimeException
{
}
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl\Exception;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ResourceBundleNotFoundException extends RuntimeException
{
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/Intl/Locale.php
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl;

/**
* Provides access to locale-related data.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
*/
final class Locale extends \Locale
{
/**
* Returns the fallback locale for a given locale, if any
*
* @param string $locale The ICU locale code to find the fallback for.
*
* @return string|null The ICU locale code of the fallback locale, or null
* if no fallback exists
*/
public static function getFallback($locale)
{
if (false === $pos = strrpos($locale, '_')) {
if ('root' === $locale) {
return;
}

return 'root';
}

return substr($locale, 0, $pos);
}

/**
* This class must not be instantiated.
*/
private function __construct() {}
}

This file was deleted.

Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Intl\ResourceBundle\Reader;

use Symfony\Component\Intl\Exception\RuntimeException;
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
use Symfony\Component\Intl\ResourceBundle\Util\ArrayAccessibleResourceBundle;

/**
Expand All @@ -21,7 +21,7 @@
*
* @internal
*/
class BinaryBundleReader extends AbstractBundleReader implements BundleReaderInterface
class BinaryBundleReader implements BundleReaderInterface
{
/**
* {@inheritdoc}
Expand All @@ -31,28 +31,39 @@ public function read($path, $locale)
// Point for future extension: Modify this class so that it works also
// if the \ResourceBundle class is not available.
try {
$bundle = new \ResourceBundle($locale, $path);
// Never enable fallback. We want to know if a bundle cannot be found
$bundle = new \ResourceBundle($locale, $path, false);
} catch (\Exception $e) {
// HHVM compatibility: constructor throws on invalid resource
$bundle = null;
}

// The bundle is NULL if the path does not look like a resource bundle
// (i.e. contain a bunch of *.res files)
if (null === $bundle) {
throw new RuntimeException(sprintf(
'Could not load the resource bundle "%s/%s.res".',
throw new ResourceBundleNotFoundException(sprintf(
'The resource bundle "%s/%s.res" could not be found.',
$path,
$locale
));
}

// Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,
// which are OK for us.
return new ArrayAccessibleResourceBundle($bundle);
}

/**
* {@inheritdoc}
*/
protected function getFileExtension()
public function getLocales($path)
{
return 'res';
$locales = glob($path.'/*.res');

// Remove file extension and sort
array_walk($locales, function (&$locale) { $locale = basename($locale, '.res'); });
sort($locales);

return $locales;
}
}
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Intl\ResourceBundle\Reader;

use Symfony\Component\Intl\Exception\InvalidArgumentException;
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
use Symfony\Component\Intl\Exception\RuntimeException;

/**
Expand All @@ -21,21 +21,17 @@
*
* @internal
*/
class PhpBundleReader extends AbstractBundleReader implements BundleReaderInterface
class PhpBundleReader implements BundleReaderInterface
{
/**
* {@inheritdoc}
*/
public function read($path, $locale)
{
if ('en' !== $locale) {
throw new InvalidArgumentException('Only the locale "en" is supported.');
}

$fileName = $path . '/' . $locale . '.php';
$fileName = $path.'/'.$locale.'.php';

if (!file_exists($fileName)) {
throw new RuntimeException(sprintf(
throw new ResourceBundleNotFoundException(sprintf(
'The resource bundle "%s/%s.php" does not exist.',
$path,
$locale
Expand All @@ -56,8 +52,14 @@ public function read($path, $locale)
/**
* {@inheritdoc}
*/
protected function getFileExtension()
public function getLocales($path)
{
return 'php';
$locales = glob($path.'/*.php');

// Remove file extension and sort
array_walk($locales, function (&$locale) { $locale = basename($locale, '.php'); });
sort($locales);

return $locales;
}
}

0 comments on commit c3cce5c

Please sign in to comment.