FileIterator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Framework\Config;
  8. use Magento\Framework\Filesystem\DriverPool;
  9. use Magento\Framework\Filesystem\File\ReadFactory;
  10. /**
  11. * Class FileIterator
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class FileIterator implements \Iterator, \Countable
  16. {
  17. /**
  18. * Paths
  19. *
  20. * @var array
  21. */
  22. protected $paths = [];
  23. /**
  24. * Position
  25. *
  26. * @var int
  27. */
  28. protected $position;
  29. /**
  30. * File read factory
  31. *
  32. * @var ReadFactory
  33. */
  34. protected $fileReadFactory;
  35. /**
  36. * Constructor
  37. *
  38. * @param ReadFactory $readFactory
  39. * @param array $paths
  40. */
  41. public function __construct(ReadFactory $readFactory, array $paths)
  42. {
  43. $this->fileReadFactory = $readFactory;
  44. $this->paths = $paths;
  45. $this->position = 0;
  46. }
  47. /**
  48. * Rewind
  49. *
  50. * @return void
  51. */
  52. public function rewind()
  53. {
  54. reset($this->paths);
  55. }
  56. /**
  57. * Current
  58. *
  59. * @return string
  60. */
  61. public function current()
  62. {
  63. $fileRead = $this->fileReadFactory->create($this->key(), DriverPool::FILE);
  64. return $fileRead->readAll();
  65. }
  66. /**
  67. * Key
  68. *
  69. * @return mixed
  70. */
  71. public function key()
  72. {
  73. return current($this->paths);
  74. }
  75. /**
  76. * Next
  77. *
  78. * @return void
  79. */
  80. public function next()
  81. {
  82. next($this->paths);
  83. }
  84. /**
  85. * Valid
  86. *
  87. * @return bool
  88. */
  89. public function valid()
  90. {
  91. return (bool) $this->key();
  92. }
  93. /**
  94. * Convert to an array
  95. *
  96. * @return array
  97. */
  98. public function toArray()
  99. {
  100. $result = [];
  101. foreach ($this as $item) {
  102. $result[$this->key()] = $item;
  103. }
  104. return $result;
  105. }
  106. /**
  107. * Count
  108. *
  109. * @return int
  110. */
  111. public function count()
  112. {
  113. return count($this->paths);
  114. }
  115. }