FileList.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\File;
  7. use Magento\Framework\View\File;
  8. use Magento\Framework\View\File\FileList\CollateInterface;
  9. /**
  10. * Unordered list of view file instances with awareness of view file identity
  11. */
  12. class FileList
  13. {
  14. /**
  15. * Array of files
  16. *
  17. * @var File[]
  18. */
  19. protected $files = [];
  20. /**
  21. * Collator
  22. *
  23. * @var \Magento\Framework\View\File\FileList\CollateInterface
  24. */
  25. protected $collator;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\Framework\View\File\FileList\CollateInterface $collator
  30. */
  31. public function __construct(CollateInterface $collator)
  32. {
  33. $this->collator = $collator;
  34. }
  35. /**
  36. * Retrieve all view file instances
  37. *
  38. * @return File[]
  39. */
  40. public function getAll()
  41. {
  42. return array_values($this->files);
  43. }
  44. /**
  45. * Add view file instances to the list, preventing identity coincidence
  46. *
  47. * @param \Magento\Framework\View\File[] $files
  48. * @return void
  49. * @throws \LogicException
  50. */
  51. public function add(array $files)
  52. {
  53. foreach ($files as $file) {
  54. $identifier = $file->getFileIdentifier();
  55. if (array_key_exists($identifier, $this->files)) {
  56. $filename = $this->files[$identifier]->getFilename();
  57. throw new \LogicException(
  58. "View file '{$file->getFilename()}' is indistinguishable from the file '{$filename}'."
  59. );
  60. }
  61. $this->files[$identifier] = $file;
  62. }
  63. }
  64. /**
  65. * Replace already added view files with specified ones, checking for identity match
  66. *
  67. * @param File[] $files
  68. * @return void
  69. */
  70. public function replace(array $files)
  71. {
  72. $this->files = $this->collator->collate($files, $this->files);
  73. }
  74. }