/lib/internal/Magento/Framework/View/File/Collector/Override/Base.php

https://gitlab.com/AlexandrSy/magento.xxx · PHP · 113 lines · 57 code · 8 blank · 48 comment · 3 complexity · b7888280a49e1b1acce71670951c6a01 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\File\Collector\Override;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Component\ComponentRegistrarInterface;
  9. use Magento\Framework\Filesystem;
  10. use Magento\Framework\Filesystem\Directory\ReadFactory;
  11. use Magento\Framework\View\Design\ThemeInterface;
  12. use Magento\Framework\View\File\CollectorInterface;
  13. use Magento\Framework\View\File\Factory as FileFactory;
  14. use Magento\Framework\View\Helper\PathPattern;
  15. /**
  16. * Source of view files that explicitly override base files introduced by modules
  17. */
  18. class Base implements CollectorInterface
  19. {
  20. /**
  21. * Pattern helper
  22. *
  23. * @var PathPattern
  24. */
  25. private $pathPatternHelper;
  26. /**
  27. * File factory
  28. *
  29. * @var FileFactory
  30. */
  31. private $fileFactory;
  32. /**
  33. * Directory factory
  34. *
  35. * @var ReadFactory
  36. */
  37. private $readDirFactory;
  38. /**
  39. * Component registrar
  40. *
  41. * @var ComponentRegistrarInterface
  42. */
  43. private $componentRegistrar;
  44. /**
  45. * Sub-directory path
  46. *
  47. * @var string
  48. */
  49. private $subDir;
  50. /**
  51. * Constructor
  52. *
  53. * @param FileFactory $fileFactory
  54. * @param ReadFactory $readDirFactory
  55. * @param ComponentRegistrarInterface $componentRegistrar
  56. * @param PathPattern $pathPatternHelper
  57. * @param string $subDir
  58. */
  59. public function __construct(
  60. FileFactory $fileFactory,
  61. ReadFactory $readDirFactory,
  62. ComponentRegistrarInterface $componentRegistrar,
  63. PathPattern $pathPatternHelper,
  64. $subDir = ''
  65. ) {
  66. $this->pathPatternHelper = $pathPatternHelper;
  67. $this->fileFactory = $fileFactory;
  68. $this->readDirFactory = $readDirFactory;
  69. $this->componentRegistrar = $componentRegistrar;
  70. $this->subDir = $subDir ? $subDir . '/' : '';
  71. }
  72. /**
  73. * Retrieve files
  74. *
  75. * @param ThemeInterface $theme
  76. * @param string $filePath
  77. * @return \Magento\Framework\View\File[]
  78. */
  79. public function getFiles(ThemeInterface $theme, $filePath)
  80. {
  81. $namespace = $module = '*';
  82. $themePath = $theme->getFullPath();
  83. if (empty($themePath)) {
  84. return [];
  85. }
  86. $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath);
  87. if (!$themeAbsolutePath) {
  88. return [];
  89. }
  90. $themeDir = $this->readDirFactory->create($themeAbsolutePath);
  91. $searchPattern = "{$namespace}_{$module}/{$this->subDir}{$filePath}";
  92. $files = $themeDir->search($searchPattern);
  93. $result = [];
  94. $pattern = "#(?<moduleName>[^/]+)/{$this->subDir}"
  95. . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "$#i";
  96. foreach ($files as $file) {
  97. $filename = $themeDir->getAbsolutePath($file);
  98. if (!preg_match($pattern, $filename, $matches)) {
  99. continue;
  100. }
  101. $result[] = $this->fileFactory->create($filename, $matches['moduleName']);
  102. }
  103. return $result;
  104. }
  105. }