BufferedBundleReader.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Intl\Data\Bundle\Reader;
  11. use Symfony\Component\Intl\Data\Util\RingBuffer;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. *
  15. * @internal
  16. */
  17. class BufferedBundleReader implements BundleReaderInterface
  18. {
  19. private $reader;
  20. private $buffer;
  21. /**
  22. * Buffers a given reader.
  23. *
  24. * @param int $bufferSize The number of entries to store in the buffer
  25. */
  26. public function __construct(BundleReaderInterface $reader, int $bufferSize)
  27. {
  28. $this->reader = $reader;
  29. $this->buffer = new RingBuffer($bufferSize);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function read(string $path, string $locale)
  35. {
  36. $hash = $path.'//'.$locale;
  37. if (!isset($this->buffer[$hash])) {
  38. $this->buffer[$hash] = $this->reader->read($path, $locale);
  39. }
  40. return $this->buffer[$hash];
  41. }
  42. }