GeneratePatchCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Developer\Console\Command;
  8. use Magento\Developer\Model\Di\Information;
  9. use Magento\Framework\Component\ComponentRegistrar;
  10. use Magento\Framework\Console\Cli;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Helper\Table;
  18. /**
  19. * Allows to generate setup patches
  20. */
  21. class GeneratePatchCommand extends Command
  22. {
  23. /**
  24. * Command arguments and options
  25. */
  26. const COMMAND_NAME = 'setup:db-declaration:generate-patch';
  27. const MODULE_NAME = 'module';
  28. const INPUT_KEY_IS_REVERTABLE = 'revertable';
  29. const INPUT_KEY_PATCH_TYPE = 'type';
  30. const INPUT_KEY_PATCH_NAME = 'patch';
  31. /**
  32. * @var ComponentRegistrar
  33. */
  34. private $componentRegistrar;
  35. /**
  36. * GeneratePatchCommand constructor.
  37. * @param ComponentRegistrar $componentRegistrar
  38. */
  39. public function __construct(ComponentRegistrar $componentRegistrar)
  40. {
  41. $this->componentRegistrar = $componentRegistrar;
  42. parent::__construct();
  43. }
  44. /**
  45. * Configure command
  46. *
  47. * @inheritdoc
  48. * @throws InvalidArgumentException
  49. */
  50. protected function configure()
  51. {
  52. $this->setName(self::COMMAND_NAME)
  53. ->setDescription('Generate patch and put it in specific folder.')
  54. ->setDefinition([
  55. new InputArgument(
  56. self::MODULE_NAME,
  57. InputArgument::REQUIRED,
  58. 'Module name'
  59. ),
  60. new InputArgument(
  61. self::INPUT_KEY_PATCH_NAME,
  62. InputArgument::REQUIRED,
  63. 'Patch name'
  64. ),
  65. new InputOption(
  66. self::INPUT_KEY_IS_REVERTABLE,
  67. null,
  68. InputOption::VALUE_OPTIONAL,
  69. 'Check whether patch is revertable or not.',
  70. false
  71. ),
  72. new InputOption(
  73. self::INPUT_KEY_PATCH_TYPE,
  74. null,
  75. InputOption::VALUE_OPTIONAL,
  76. 'Find out what type of patch should be generated.',
  77. 'data'
  78. ),
  79. ]);
  80. parent::configure();
  81. }
  82. /**
  83. * Patch template
  84. *
  85. * @return string
  86. */
  87. private function getPatchTemplate() : string
  88. {
  89. return file_get_contents(__DIR__ . '/patch_template.php.dist');
  90. }
  91. /**
  92. * @inheritdoc
  93. * @throws \InvalidArgumentException
  94. */
  95. protected function execute(InputInterface $input, OutputInterface $output) : int
  96. {
  97. $moduleName = $input->getArgument(self::MODULE_NAME);
  98. $patchName = $input->getArgument(self::INPUT_KEY_PATCH_NAME);
  99. $type = $input->getOption(self::INPUT_KEY_PATCH_TYPE);
  100. $modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
  101. $preparedModuleName = str_replace('_', '\\', $moduleName);
  102. $preparedType = ucfirst($type);
  103. $patchInterface = sprintf('%sPatchInterface', $preparedType);
  104. $patchTemplateData = $this->getPatchTemplate();
  105. $patchTemplateData = str_replace('%moduleName%', $preparedModuleName, $patchTemplateData);
  106. $patchTemplateData = str_replace('%patchType%', $preparedType, $patchTemplateData);
  107. $patchTemplateData = str_replace('%patchInterface%', $patchInterface, $patchTemplateData);
  108. $patchTemplateData = str_replace('%class%', $patchName, $patchTemplateData);
  109. $patchDir = $patchToFile = $modulePath . '/Setup/Patch/' . $preparedType;
  110. if (!is_dir($patchDir)) {
  111. mkdir($patchDir, 0777, true);
  112. }
  113. $patchToFile = $patchDir . '/' . $patchName . '.php';
  114. file_put_contents($patchToFile, $patchTemplateData);
  115. return Cli::RETURN_SUCCESS;
  116. }
  117. }