BufferedBundleReader.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /**
  20. * @var BundleReaderInterface
  21. */
  22. private $reader;
  23. private $buffer;
  24. /**
  25. * Buffers a given reader.
  26. *
  27. * @param BundleReaderInterface $reader The reader to buffer
  28. * @param int $bufferSize The number of entries to store
  29. * in the buffer.
  30. */
  31. public function __construct(BundleReaderInterface $reader, $bufferSize)
  32. {
  33. $this->reader = $reader;
  34. $this->buffer = new RingBuffer($bufferSize);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function read($path, $locale)
  40. {
  41. $hash = $path.'//'.$locale;
  42. if (!isset($this->buffer[$hash])) {
  43. $this->buffer[$hash] = $this->reader->read($path, $locale);
  44. }
  45. return $this->buffer[$hash];
  46. }
  47. }