FileLoader.php 1.8 KB

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