PhpBundleReader.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Exception\RuntimeException;
  13. /**
  14. * Reads .php resource bundles.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @internal
  19. */
  20. class PhpBundleReader implements BundleReaderInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function read($path, $locale)
  26. {
  27. $fileName = $path.'/'.$locale.'.php';
  28. // prevent directory traversal attacks
  29. if (dirname($fileName) !== $path) {
  30. throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName));
  31. }
  32. if (!file_exists($fileName)) {
  33. throw new ResourceBundleNotFoundException(sprintf(
  34. 'The resource bundle "%s/%s.php" does not exist.',
  35. $path,
  36. $locale
  37. ));
  38. }
  39. if (!is_file($fileName)) {
  40. throw new RuntimeException(sprintf(
  41. 'The resource bundle "%s/%s.php" is not a file.',
  42. $path,
  43. $locale
  44. ));
  45. }
  46. return include $fileName;
  47. }
  48. }