BufferedBundleReader.php 1.4 KB

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