FileLoader.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Translation\Loader;
  11. use Symfony\Component\Translation\Exception\InvalidResourceException;
  12. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. /**
  15. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  16. */
  17. abstract class FileLoader extends ArrayLoader
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function load($resource, $locale, $domain = 'messages')
  23. {
  24. if (!stream_is_local($resource)) {
  25. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  26. }
  27. if (!file_exists($resource)) {
  28. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  29. }
  30. $messages = $this->loadResource($resource);
  31. // empty resource
  32. if (null === $messages) {
  33. $messages = array();
  34. }
  35. // not an array
  36. if (!is_array($messages)) {
  37. throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
  38. }
  39. $catalogue = parent::load($messages, $locale, $domain);
  40. if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
  41. $catalogue->addResource(new FileResource($resource));
  42. }
  43. return $catalogue;
  44. }
  45. /*
  46. * @param string $resource
  47. *
  48. * @return array
  49. *
  50. * @throws InvalidResourceException If stream content has an invalid format.
  51. */
  52. abstract protected function loadResource($resource);
  53. }