8000 [Intl] Improved bundle reader implementations · alexpott/symfony@c3cce5c · GitHub
[go: up one dir, main page]

Skip to content

Commit c3cce5c

Browse files
committed
[Intl] Improved bundle reader implementations
1 parent d35fd52 commit c3cce5c

25 files changed

+573
-253
lines changed
Lines 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 p 8000 art 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+
}
Lines 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

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

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 18 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,39 @@ 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.
4853
return new ArrayAccessibleResourceBundle($bundle);
4954
}
5055

5156
/**
5257
* {@inheritdoc}
5358
*/
54-
protected function getFileExtension()
59+
public function getLocales($path)
5560
{
56-
return 'res';
61+
$locales = glob($path.'/*.res');
62+
63+
// Remove file extension and sort
64+
array_walk($locales, function (&$locale) { $locale = basename($locale, '.res'); });
65+
sort($locales);
66+
67+
return $locales;
5768
}
5869
}

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

Lines changed: 12 additions & 10 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-
35-
$fileName = $path . '/' . $locale . '.php';
31+
$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