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

Commit c43fc14

Browse filesBrowse files
committed
[Intl] Improved bundle reader implementations
1 parent a45e3da commit c43fc14
Copy full SHA for c43fc14

25 files changed

+564
-249
lines changed
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Exception;
13+
14+
/**
15+
* Thrown when an invalid entry of a resource bundle was requested.
16+
*
17+
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*/
19+
class MissingResourceException extends RuntimeException
20+
{
21+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Exception;
13+
14+
/**
15+
* @author Bernhard Schussek <bschussek@gmail.com>
16+
*/
17+
class ResourceBundleNotFoundException extends RuntimeException
18+
{
19+
}

‎src/Symfony/Component/Intl/Locale.php

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\LocaleDataProvider;
15+
use Symfony\Component\Intl\Exception\InvalidArgumentException;
16+
17+
/**
18+
* Provides access to locale-related data.
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
21+
*/
22+
final class Locale extends \Locale
23+
{
24+
/**
25+
* Returns the fallback locale for a given locale, if any
26+
*
27+
* @param string $locale The ICU locale code to find the fallback for.
28+
*
29+
* @return string|null The ICU locale code of the fallback locale, or null
30+
* if no fallback exists
31+
*/
32+
public static function getFallback($locale)
33+
{
34+
if (false === $pos = strrpos($locale, '_')) {
35+
if ('root' === $locale) {
36+
return;
37+
}
38+
39+
return 'root';
40+
}
41+
42+
return substr($locale, 0, $pos);
43+
}
44+
45+
/**
46+
* This class must not be instantiated.
47+
*/
48+
private function __construct() {}
49+
}

‎src/Symfony/Component/Intl/ResourceBundle/Reader/AbstractBundleReader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/ResourceBundle/Reader/AbstractBundleReader.php
-44Lines changed: 0 additions & 44 deletions
This file was deleted.

‎src/Symfony/Component/Intl/ResourceBundle/Reader/BinaryBundleReader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/ResourceBundle/Reader/BinaryBundleReader.php
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Intl\ResourceBundle\Reader;
1313

14-
use Symfony\Component\Intl\Exception\RuntimeException;
14+
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
1515
use Symfony\Component\Intl\ResourceBundle\Util\ArrayAccessibleResourceBundle;
1616

1717
/**
@@ -21,7 +21,7 @@
2121
*
2222
* @internal
2323
*/
24-
class BinaryBundleReader extends AbstractBundleReader implements BundleReaderInterface
24+
class BinaryBundleReader implements BundleReaderInterface
2525
{
2626
/**
2727
* {@inheritdoc}
@@ -31,28 +31,40 @@ public function read($path, $locale)
3131
// Point for future extension: Modify this class so that it works also
3232
// if the \ResourceBundle class is not available.
3333
try {
34-
$bundle = new \ResourceBundle($locale, $path);
34+
// Never enable fallback. We want to know if a bundle cannot be found
35+
$bundle = new \ResourceBundle($locale, $path, false);
3536
} catch (\Exception $e) {
3637
// HHVM compatibility: constructor throws on invalid resource
3738
$bundle = null;
3839
}
3940

41+
// The bundle is NULL if the path does not look like a resource bundle
42+
// (i.e. contain a bunch of *.res files)
4043
if (null === $bundle) {
41-
throw new RuntimeException(sprintf(
42-
'Could not load the resource bundle "%s/%s.res".',
44+
throw new ResourceBundleNotFoundException(sprintf(
45+
'The resource bundle "%s/%s.res" could not be found.',
4346
$path,
4447
$locale
4548
));
4649
}
4750

51+
// Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,
52+
// which are OK for us.
53+
4854
return new ArrayAccessibleResourceBundle($bundle);
4955
}
5056

5157
/**
5258
* {@inheritdoc}
5359
*/
54-
protected function getFileExtension()
60+
public function getLocales($path)
5561
{
56-
return 'res';
62+
$locales = glob($path . '/*.res');
63+
64+
// Remove file extension and sort
65+
array_walk($locales, function (&$locale) { $locale = basename($locale, '.res'); });
66+
sort($locales);
67+
68+
return $locales;
5769
}
5870
}

‎src/Symfony/Component/Intl/ResourceBundle/Reader/PhpBundleReader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/ResourceBundle/Reader/PhpBundleReader.php
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Intl\ResourceBundle\Reader;
1313

14-
use Symfony\Component\Intl\Exception\InvalidArgumentException;
14+
use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
1515
use Symfony\Component\Intl\Exception\RuntimeException;
1616

1717
/**
@@ -21,21 +21,17 @@
2121
*
2222
* @internal
2323
*/
24-
class PhpBundleReader extends AbstractBundleReader implements BundleReaderInterface
24+
class PhpBundleReader implements BundleReaderInterface
2525
{
2626
/**
2727
* {@inheritdoc}
2828
*/
2929
public function read($path, $locale)
3030
{
31-
if ('en' !== $locale) {
32-
throw new InvalidArgumentException('Only the locale "en" is supported.');
33-
}
34-
3531
$fileName = $path . '/' . $locale . '.php';
3632

3733
if (!file_exists($fileName)) {
38-
throw new RuntimeException(sprintf(
34+
throw new ResourceBundleNotFoundException(sprintf(
3935
'The resource bundle "%s/%s.php" does not exist.',
4036
$path,
4137
$locale
@@ -56,8 +52,14 @@ public function read($path, $locale)
5652
/**
5753
* {@inheritdoc}
5854
*/
59-
protected function getFileExtension()
55+
public function getLocales($path)
6056
{
61-
return 'php';
57+
$locales = glob($path . '/*.php');
58+
59+
// Remove file extension and sort
60+
array_walk($locales, function (&$locale) { $locale = basename($locale, '.php'); });
61+
sort($locales);
62+
63+
return $locales;
6264
}
6365
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.