Theme.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\File\Collector;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Component\ComponentRegistrarInterface;
  9. use Magento\Framework\Filesystem\Directory\ReadFactory;
  10. use Magento\Framework\View\Design\ThemeInterface;
  11. use Magento\Framework\View\File\CollectorInterface;
  12. use Magento\Framework\View\File\Factory as FileFactory;
  13. /**
  14. * Source of view files introduced by a theme
  15. */
  16. class Theme implements CollectorInterface
  17. {
  18. /**
  19. * @var FileFactory
  20. */
  21. private $fileFactory;
  22. /**
  23. * @var ReadFactory
  24. */
  25. private $readDirFactory;
  26. /**
  27. * @var ComponentRegistrarInterface
  28. */
  29. private $componentRegistrar;
  30. /**
  31. * @var string
  32. */
  33. private $subDir;
  34. /**
  35. * Constructor
  36. *
  37. * @param FileFactory $fileFactory
  38. * @param ReadFactory $readDirFactory
  39. * @param ComponentRegistrarInterface $componentRegistrar
  40. * @param string $subDir
  41. */
  42. public function __construct(
  43. FileFactory $fileFactory,
  44. ReadFactory $readDirFactory,
  45. ComponentRegistrarInterface $componentRegistrar,
  46. $subDir = ''
  47. ) {
  48. $this->fileFactory = $fileFactory;
  49. $this->readDirFactory = $readDirFactory;
  50. $this->componentRegistrar = $componentRegistrar;
  51. $this->subDir = $subDir ? $subDir . '/' : '';
  52. }
  53. /**
  54. * Retrieve files
  55. *
  56. * @param ThemeInterface $theme
  57. * @param string $filePath
  58. * @return \Magento\Framework\View\File[]
  59. * @throws \UnexpectedValueException
  60. */
  61. public function getFiles(ThemeInterface $theme, $filePath)
  62. {
  63. $themePath = $theme->getFullPath();
  64. if (empty($themePath)) {
  65. return [];
  66. }
  67. $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath);
  68. if (!$themeAbsolutePath) {
  69. return [];
  70. }
  71. $themeDir = $this->readDirFactory->create($themeAbsolutePath);
  72. $files = $themeDir->search($this->subDir . $filePath);
  73. $result = [];
  74. foreach ($files as $file) {
  75. $filename = $themeDir->getAbsolutePath($file);
  76. $result[] = $this->fileFactory->create($filename, null, $theme);
  77. }
  78. return $result;
  79. }
  80. }