Map.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Package\Processor\PostProcessor;
  7. use Magento\Deploy\Package\PackageFile;
  8. use Magento\Deploy\Service\DeployStaticFile;
  9. use Magento\Deploy\Package\Package;
  10. use Magento\Deploy\Package\PackageFileFactory;
  11. use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
  12. use Magento\Deploy\Package\Processor\ProcessorInterface;
  13. use Magento\Framework\View\Asset\Repository;
  14. use Magento\Framework\View\Asset\Minification;
  15. use Magento\Framework\View\Asset\RepositoryMap;
  16. /**
  17. * Map post-processor is for generating map files for JavaScript and server-side URL resolvers
  18. *
  19. * Map files needed only for compact deployment, and yet can be used for tracking changes of deployed static files
  20. * in development mode, when using symlinks is not possible
  21. */
  22. class Map implements ProcessorInterface
  23. {
  24. /**
  25. * Service class is used for deploying files to public directory
  26. *
  27. * The service does simple write, copy or go through publication process (apply fallback rules, pre-processing etc)
  28. *
  29. * @var DeployStaticFile
  30. */
  31. private $deployStaticFile;
  32. /**
  33. * PHP code formatter
  34. *
  35. * Formatter generates code for PHP file that returns data array
  36. *
  37. * @var PhpFormatter
  38. */
  39. private $formatter;
  40. /**
  41. * Factory class for deployment package object
  42. *
  43. * @see PackageFile
  44. * @var PackageFileFactory
  45. */
  46. private $packageFileFactory;
  47. /**
  48. * Helper class for static files minification related processes
  49. *
  50. * @var Minification
  51. */
  52. private $minification;
  53. /**
  54. * Deployment procedure options
  55. *
  56. * @var array
  57. */
  58. private $options = [];
  59. /**
  60. * Map constructor
  61. *
  62. * @param DeployStaticFile $deployStaticFile
  63. * @param PhpFormatter $formatter
  64. * @param PackageFileFactory $packageFileFactory
  65. * @param Minification $minification
  66. */
  67. public function __construct(
  68. DeployStaticFile $deployStaticFile,
  69. PhpFormatter $formatter,
  70. PackageFileFactory $packageFileFactory,
  71. Minification $minification
  72. ) {
  73. $this->deployStaticFile = $deployStaticFile;
  74. $this->packageFileFactory = $packageFileFactory;
  75. $this->formatter = $formatter;
  76. $this->minification = $minification;
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function process(Package $package, array $options)
  82. {
  83. if ($package->isVirtual()) {
  84. return true;
  85. }
  86. // delete existing map files
  87. $this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::MAP_NAME);
  88. $this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::REQUIRE_JS_MAP_NAME);
  89. $this->deployStaticFile->deleteFile($package->getPath() . '/' . RepositoryMap::RESULT_MAP_NAME);
  90. if (!$package->getParam('build_map')) {
  91. return true;
  92. }
  93. $this->options = $options;
  94. $packageMap = $package->getParentMap();
  95. if ($packageMap) {
  96. $this->deployStaticFile->writeFile(
  97. RepositoryMap::MAP_NAME,
  98. $package->getPath(),
  99. json_encode($packageMap)
  100. );
  101. $this->deployStaticFile->writeFile(
  102. $this->minification->addMinifiedSign(RepositoryMap::REQUIRE_JS_MAP_NAME),
  103. $package->getPath(),
  104. $this->getRequireJsMap($packageMap)
  105. );
  106. }
  107. $resultMap = $package->getResultMap();
  108. if ($resultMap) {
  109. $this->deployStaticFile->writeFile(
  110. RepositoryMap::RESULT_MAP_NAME,
  111. $package->getPath(),
  112. json_encode($resultMap)
  113. );
  114. }
  115. return true;
  116. }
  117. /**
  118. * Retrieve require js map
  119. *
  120. * @param array $map
  121. * @return string
  122. */
  123. private function getRequireJsMap(array $map)
  124. {
  125. $jsonMap = [];
  126. foreach ($map as $fileId => $fileInfo) {
  127. if (!in_array(pathinfo($fileId, PATHINFO_EXTENSION), ['js', 'html'])) {
  128. continue;
  129. }
  130. $fileId = '/' . $fileId; // add leading slash to match exclude patterns
  131. $filePath = $this->minification->addMinifiedSign(str_replace(Repository::FILE_ID_SEPARATOR, '/', $fileId));
  132. $filePath = substr($filePath, 1); // and remove
  133. $jsonMap[$filePath] = '../../../../'
  134. . $fileInfo['area'] . '/' . $fileInfo['theme'] . '/' . $fileInfo['locale'] . '/';
  135. }
  136. $jsonMap = json_encode($jsonMap);
  137. return "require.config({\"config\": {\"baseUrlInterceptor\":{$jsonMap}}});";
  138. }
  139. }