FileLoader.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Config\Resource\FileResource;
  12. use Symfony\Component\Translation\Exception\InvalidResourceException;
  13. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  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 = [];
  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. }