CachedCollection.php 896 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * @package Grav\Common\GPM
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\GPM\Common;
  9. use Grav\Common\Iterator;
  10. /**
  11. * Class CachedCollection
  12. * @package Grav\Common\GPM\Common
  13. */
  14. class CachedCollection extends Iterator
  15. {
  16. /** @var array */
  17. protected static $cache = [];
  18. /**
  19. * CachedCollection constructor.
  20. *
  21. * @param array $items
  22. */
  23. public function __construct($items)
  24. {
  25. parent::__construct();
  26. $method = static::class . __METHOD__;
  27. // local cache to speed things up
  28. if (!isset(self::$cache[$method])) {
  29. self::$cache[$method] = $items;
  30. }
  31. foreach (self::$cache[$method] as $name => $item) {
  32. $this->append([$name => $item]);
  33. }
  34. }
  35. }