App.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\Laravel;
  12. use ArrayAccess;
  13. use Symfony\Component\Translation\Translator;
  14. class App implements ArrayAccess
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $locale = 'en';
  20. /**
  21. * @var string
  22. */
  23. protected static $version;
  24. /**
  25. * @var Translator
  26. */
  27. public $translator;
  28. /**
  29. * @var \Illuminate\Events\EventDispatcher
  30. */
  31. public $events;
  32. public function register()
  33. {
  34. include_once __DIR__.'/EventDispatcher.php';
  35. $this->locale = 'de';
  36. $this->translator = new Translator($this->locale);
  37. }
  38. public function setEventDispatcher($dispatcher)
  39. {
  40. $this->events = $dispatcher;
  41. }
  42. public static function version($version = null)
  43. {
  44. if ($version !== null) {
  45. static::$version = $version;
  46. }
  47. return static::$version;
  48. }
  49. public static function getLocaleChangeEventName()
  50. {
  51. return version_compare((string) static::version(), '5.5') >= 0
  52. ? 'Illuminate\Foundation\Events\LocaleUpdated'
  53. : 'locale.changed';
  54. }
  55. public function setLocaleWithoutEvent($locale)
  56. {
  57. $this->locale = $locale;
  58. $this->translator->setLocale($locale);
  59. }
  60. public function setLocale($locale)
  61. {
  62. $this->setLocaleWithoutEvent($locale);
  63. $this->events->dispatch(static::getLocaleChangeEventName());
  64. }
  65. public function getLocale()
  66. {
  67. return $this->locale;
  68. }
  69. public function bound($service)
  70. {
  71. return isset($this->{$service});
  72. }
  73. #[\ReturnTypeWillChange]
  74. public function offsetExists($offset)
  75. {
  76. return isset($this->$offset);
  77. }
  78. #[\ReturnTypeWillChange]
  79. public function offsetGet($offset)
  80. {
  81. return $this->$offset;
  82. }
  83. #[\ReturnTypeWillChange]
  84. public function offsetSet($offset, $value)
  85. {
  86. // noop
  87. }
  88. #[\ReturnTypeWillChange]
  89. public function offsetUnset($offset)
  90. {
  91. // noop
  92. }
  93. public function removeService($offset)
  94. {
  95. $this->$offset = null;
  96. }
  97. }