IntlBundleReader.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Data\Bundle\Reader;
  11. use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
  12. use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
  13. /**
  14. * Reads binary .res resource bundles.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class IntlBundleReader implements BundleReaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function read($path, $locale)
  26. {
  27. // Point for future extension: Modify this class so that it works also
  28. // if the \ResourceBundle class is not available.
  29. try {
  30. // Never enable fallback. We want to know if a bundle cannot be found
  31. $bundle = new \ResourceBundle($locale, $path, false);
  32. } catch (\Exception $e) {
  33. // HHVM compatibility: constructor throws on invalid resource
  34. $bundle = null;
  35. }
  36. // The bundle is NULL if the path does not look like a resource bundle
  37. // (i.e. contain a bunch of *.res files)
  38. if (null === $bundle) {
  39. throw new ResourceBundleNotFoundException(sprintf(
  40. 'The resource bundle "%s/%s.res" could not be found.',
  41. $path,
  42. $locale
  43. ));
  44. }
  45. // Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,
  46. // which are OK for us.
  47. return new ArrayAccessibleResourceBundle($bundle);
  48. }
  49. }