Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Intl] Improved bundle reader implementations #11907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/Intl/Exception/MissingResourceException.php
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
@@ -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 48 src/Symfony/Component/Intl/Locale.php
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really internal while being at the root of the component ? this looks weird to me. Internal classes used for the building of ICU data only should probably be placed in a subnamespace, keeping the top-level of the component for the poublic API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to remove this flag in 2.6, but I don't want anybody to rely on this code until I'm sure this class will stay as it is.

*/
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.

Original file line number Diff line number Diff line change
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;
}
}
Original file line number Diff line number Diff line change
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;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.