FileLoader.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
  21. {
  22. if (!stream_is_local($resource)) {
  23. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  24. }
  25. if (!file_exists($resource)) {
  26. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  27. }
  28. $messages = $this->loadResource($resource);
  29. // empty resource
  30. $messages ??= [];
  31. // not an array
  32. if (!\is_array($messages)) {
  33. throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
  34. }
  35. $catalogue = parent::load($messages, $locale, $domain);
  36. if (class_exists(FileResource::class)) {
  37. $catalogue->addResource(new FileResource($resource));
  38. }
  39. return $catalogue;
  40. }
  41. /**
  42. * @throws InvalidResourceException if stream content has an invalid format
  43. */
  44. abstract protected function loadResource(string $resource): array;
  45. }