PatchReader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Patch;
  7. use Magento\Framework\Component\ComponentRegistrar;
  8. use Magento\Framework\Config\ReaderInterface;
  9. use Magento\Framework\Filesystem\Glob;
  10. use Magento\Framework\Module\Dir;
  11. /**
  12. * Allows to read all patches through the whole system
  13. */
  14. class PatchReader
  15. {
  16. /**
  17. * Folder name, where patches are
  18. */
  19. const SETUP_PATCH_FOLDER = 'Patch';
  20. /**
  21. * @var ComponentRegistrar
  22. */
  23. private $componentRegistrar;
  24. /**
  25. * @var string
  26. */
  27. private $type;
  28. /**
  29. * @param ComponentRegistrar $componentRegistrar
  30. * @param string $type
  31. */
  32. public function __construct(
  33. ComponentRegistrar $componentRegistrar,
  34. $type
  35. ) {
  36. $this->componentRegistrar = $componentRegistrar;
  37. $this->type = $type;
  38. }
  39. /**
  40. * Retrieve absolute path to modules patch folder
  41. *
  42. * @param string $modulePath
  43. * @return string
  44. */
  45. private function getPatchFolder($modulePath)
  46. {
  47. return $modulePath . DIRECTORY_SEPARATOR . Dir::MODULE_SETUP_DIR .
  48. DIRECTORY_SEPARATOR . self::SETUP_PATCH_FOLDER;
  49. }
  50. /**
  51. * Retrieve module name prepared to usage in namespaces
  52. *
  53. * @param string $moduleName
  54. * @return string
  55. */
  56. private function getModuleNameForNamespace($moduleName)
  57. {
  58. return str_replace('_', '\\', $moduleName);
  59. }
  60. /**
  61. * Depends on type we want to handle schema and data patches in different folders
  62. *
  63. * @return string
  64. */
  65. private function getTypeFolder()
  66. {
  67. return ucfirst($this->type);
  68. }
  69. /**
  70. * Create array of class patch names from module name
  71. *
  72. * @param string $moduleName
  73. * @param string $modulePath
  74. * @return array
  75. */
  76. private function getPatchClassesPerModule($moduleName, $modulePath)
  77. {
  78. $patchClasses = [];
  79. $patchesPath = $this->getPatchFolder($modulePath);
  80. $specificPatchPath = $patchesPath . DIRECTORY_SEPARATOR . $this->getTypeFolder();
  81. $patchesMask = $specificPatchPath . DIRECTORY_SEPARATOR . '*.php';
  82. foreach (Glob::glob($patchesMask) as $patchPath) {
  83. $moduleName = $this->getModuleNameForNamespace($moduleName);
  84. $patchClasses[] = $moduleName . '\\Setup\\' .
  85. self::SETUP_PATCH_FOLDER . '\\' .
  86. $this->getTypeFolder() . '\\' .
  87. basename($patchPath, '.php');
  88. }
  89. return $patchClasses;
  90. }
  91. /**
  92. * @param string $moduleName
  93. * @return array
  94. */
  95. public function read($moduleName)
  96. {
  97. $modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
  98. $patches = $this->getPatchClassesPerModule($moduleName, $modulePath);
  99. return $patches;
  100. }
  101. }