PhpFileLoader.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. * PhpFileLoader loads translations from PHP files returning an array of translations.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @api
  20. */
  21. class PhpFileLoader extends ArrayLoader implements LoaderInterface
  22. {
  23. /**
  24. * {@inheritdoc}
  25. *
  26. * @api
  27. */
  28. public function load($resource, $locale, $domain = 'messages')
  29. {
  30. if (!stream_is_local($resource)) {
  31. throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
  32. }
  33. if (!file_exists($resource)) {
  34. throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
  35. }
  36. $messages = require($resource);
  37. $catalogue = parent::load($messages, $locale, $domain);
  38. $catalogue->addResource(new FileResource($resource));
  39. return $catalogue;
  40. }
  41. }