TranslatorImmutable.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.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 Carbon;
  11. use Carbon\Exceptions\ImmutableException;
  12. use Symfony\Component\Config\ConfigCacheFactoryInterface;
  13. use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
  14. class TranslatorImmutable extends Translator
  15. {
  16. /** @var bool */
  17. private $constructed = false;
  18. public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
  19. {
  20. parent::__construct($locale, $formatter, $cacheDir, $debug);
  21. $this->constructed = true;
  22. }
  23. /**
  24. * @codeCoverageIgnore
  25. */
  26. public function setDirectories(array $directories)
  27. {
  28. $this->disallowMutation(__METHOD__);
  29. return parent::setDirectories($directories);
  30. }
  31. public function setLocale($locale)
  32. {
  33. $this->disallowMutation(__METHOD__);
  34. return parent::setLocale($locale);
  35. }
  36. /**
  37. * @codeCoverageIgnore
  38. */
  39. public function setMessages($locale, $messages)
  40. {
  41. $this->disallowMutation(__METHOD__);
  42. return parent::setMessages($locale, $messages);
  43. }
  44. /**
  45. * @codeCoverageIgnore
  46. */
  47. public function setTranslations($messages)
  48. {
  49. $this->disallowMutation(__METHOD__);
  50. return parent::setTranslations($messages);
  51. }
  52. /**
  53. * @codeCoverageIgnore
  54. */
  55. public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory): void
  56. {
  57. $this->disallowMutation(__METHOD__);
  58. parent::setConfigCacheFactory($configCacheFactory);
  59. }
  60. public function resetMessages($locale = null)
  61. {
  62. $this->disallowMutation(__METHOD__);
  63. return parent::resetMessages($locale);
  64. }
  65. /**
  66. * @codeCoverageIgnore
  67. */
  68. public function setFallbackLocales(array $locales)
  69. {
  70. $this->disallowMutation(__METHOD__);
  71. parent::setFallbackLocales($locales);
  72. }
  73. private function disallowMutation($method)
  74. {
  75. if ($this->constructed) {
  76. throw new ImmutableException($method.' not allowed on '.static::class);
  77. }
  78. }
  79. }