RepositoryMap.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Asset;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\View\DesignInterface;
  10. /**
  11. * Class RepositoryMap
  12. */
  13. class RepositoryMap
  14. {
  15. /**
  16. * Name of package map file
  17. *
  18. * Map file contains list of files which are missed in current package,
  19. * so inherited from one of the ancestor packages
  20. *
  21. * @var string
  22. */
  23. const MAP_NAME = 'map.json';
  24. /**
  25. * Name of package result map file
  26. *
  27. * Result map contains list of all package files (own and inherited from ancestors)
  28. *
  29. * @var string
  30. */
  31. const RESULT_MAP_NAME = 'result_map.json';
  32. /**
  33. * Name of package result map file
  34. *
  35. * Result map contains list of all package files (own and inherited from ancestors)
  36. *
  37. * @var string
  38. */
  39. const REQUIRE_JS_MAP_NAME = 'requirejs-map.js';
  40. /**
  41. * @var DesignInterface
  42. */
  43. private $design;
  44. /**
  45. * @var Filesystem
  46. */
  47. private $filesystem;
  48. /**
  49. * @var array
  50. */
  51. private $maps = [];
  52. /**
  53. * RepositoryMap constructor.
  54. * @param DesignInterface $design
  55. * @param Filesystem $filesystem
  56. */
  57. public function __construct(DesignInterface $design, Filesystem $filesystem)
  58. {
  59. $this->design = $design;
  60. $this->filesystem = $filesystem;
  61. }
  62. /**
  63. * @param string $fileId
  64. * @param array $params
  65. * @return array
  66. */
  67. public function getMap($fileId, array $params)
  68. {
  69. $area = $params['area'];
  70. $locale = $params['locale'];
  71. $themePath = isset($params['theme']) ? $params['theme'] : $this->design->getThemePath($params['themeModel']);
  72. $path = "$area/$themePath/$locale/" . self::MAP_NAME;
  73. if (!isset($this->maps[$path])) {
  74. $this->maps[$path] = [];
  75. /** @var Filesystem $filesystem */
  76. $staticDir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
  77. if ($staticDir->isFile($path)) {
  78. $map = $staticDir->readFile($path);
  79. if ($map) {
  80. $this->maps[$path] = json_decode($map, true);
  81. }
  82. }
  83. }
  84. if (isset($this->maps[$path][$fileId])) {
  85. return $this->maps[$path][$fileId];
  86. }
  87. return [];
  88. }
  89. }