Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GeneratePatchCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 use Symfony\Component\Console\Command\Command;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Helper\Table;
20 
24 class GeneratePatchCommand extends Command
25 {
29  const COMMAND_NAME = 'setup:db-declaration:generate-patch';
30  const MODULE_NAME = 'module';
31  const INPUT_KEY_IS_REVERTABLE = 'revertable';
32  const INPUT_KEY_PATCH_TYPE = 'type';
33  const INPUT_KEY_PATCH_NAME = 'patch';
34 
38  private $componentRegistrar;
39 
44  public function __construct(ComponentRegistrar $componentRegistrar)
45  {
46  $this->componentRegistrar = $componentRegistrar;
47  parent::__construct();
48  }
49 
56  protected function configure()
57  {
58  $this->setName(self::COMMAND_NAME)
59  ->setDescription('Generate patch and put it in specific folder.')
60  ->setDefinition([
61  new InputArgument(
62  self::MODULE_NAME,
63  InputArgument::REQUIRED,
64  'Module name'
65  ),
66  new InputArgument(
67  self::INPUT_KEY_PATCH_NAME,
68  InputArgument::REQUIRED,
69  'Patch name'
70  ),
71  new InputOption(
72  self::INPUT_KEY_IS_REVERTABLE,
73  null,
74  InputOption::VALUE_OPTIONAL,
75  'Check whether patch is revertable or not.',
76  false
77  ),
78  new InputOption(
79  self::INPUT_KEY_PATCH_TYPE,
80  null,
81  InputOption::VALUE_OPTIONAL,
82  'Find out what type of patch should be generated.',
83  'data'
84  ),
85  ]);
86 
87  parent::configure();
88  }
89 
95  private function getPatchTemplate() : string
96  {
97  return file_get_contents(__DIR__ . '/patch_template.php.dist');
98  }
99 
104  protected function execute(InputInterface $input, OutputInterface $output) : int
105  {
106  $moduleName = $input->getArgument(self::MODULE_NAME);
107  $patchName = $input->getArgument(self::INPUT_KEY_PATCH_NAME);
108  $type = $input->getOption(self::INPUT_KEY_PATCH_TYPE);
109  $modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
110  $preparedModuleName = str_replace('_', '\\', $moduleName);
111  $preparedType = ucfirst($type);
112  $patchInterface = sprintf('%sPatchInterface', $preparedType);
113  $patchTemplateData = $this->getPatchTemplate();
114  $patchTemplateData = str_replace('%moduleName%', $preparedModuleName, $patchTemplateData);
115  $patchTemplateData = str_replace('%patchType%', $preparedType, $patchTemplateData);
116  $patchTemplateData = str_replace('%patchInterface%', $patchInterface, $patchTemplateData);
117  $patchTemplateData = str_replace('%class%', $patchName, $patchTemplateData);
118  $patchDir = $patchToFile = $modulePath . '/Setup/Patch/' . $preparedType;
119 
120  if (!is_dir($patchDir)) {
121  mkdir($patchDir, 0777, true);
122  }
123  $patchToFile = $patchDir . '/' . $patchName . '.php';
124  file_put_contents($patchToFile, $patchTemplateData);
125  return Cli::RETURN_SUCCESS;
126  }
127 }
$componentRegistrar
Definition: bootstrap.php:23
__construct(ComponentRegistrar $componentRegistrar)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$type
Definition: item.phtml:13
execute(InputInterface $input, OutputInterface $output)
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25