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
12namespace Symfony\Component\Intl\Data\Bundle\Reader;
13
14use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
15use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
16
17/**
18 * Reads binary .res resource bundles.
19 *
20 * @author Bernhard Schussek <bschussek@gmail.com>
21 *
22 * @internal
23 */
24class IntlBundleReader implements BundleReaderInterface
25{
26    /**
27     * {@inheritdoc}
28     */
29    public function read($path, $locale)
30    {
31        // Point for future extension: Modify this class so that it works also
32        // if the \ResourceBundle class is not available.
33        try {
34            // Never enable fallback. We want to know if a bundle cannot be found
35            $bundle = new \ResourceBundle($locale, $path, false);
36        } catch (\Exception $e) {
37            // HHVM compatibility: constructor throws on invalid resource
38            $bundle = null;
39        }
40
41        // The bundle is NULL if the path does not look like a resource bundle
42        // (i.e. contain a bunch of *.res files)
43        if (null === $bundle) {
44            throw new ResourceBundleNotFoundException(sprintf(
45                'The resource bundle "%s/%s.res" could not be found.',
46                $path,
47                $locale
48            ));
49        }
50
51        // Other possible errors are U_USING_FALLBACK_WARNING and U_ZERO_ERROR,
52        // which are OK for us.
53        return new ArrayAccessibleResourceBundle($bundle);
54    }
55}
56