TablesWhitelistGenerateCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Setup\Declaration\Schema\WhitelistGenerator;
  9. use Magento\Framework\Config\FileResolverByModule;
  10. use Magento\Framework\Exception\ConfigurationMismatchException;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Command that allows to generate whitelist, that will be used, when declaration data is installed.
  17. *
  18. * If whitelist already exists, new values will be added to existing whitelist.
  19. */
  20. class TablesWhitelistGenerateCommand extends Command
  21. {
  22. /**
  23. * Module name key, that will be used in whitelist generate command.
  24. */
  25. const MODULE_NAME_KEY = 'module-name';
  26. /**
  27. * @var WhitelistGenerator
  28. */
  29. private $whitelistGenerator;
  30. /**
  31. * @param WhitelistGenerator $whitelistGenerator
  32. * @param string|null $name
  33. */
  34. public function __construct(
  35. WhitelistGenerator $whitelistGenerator,
  36. $name = null
  37. ) {
  38. $this->whitelistGenerator = $whitelistGenerator;
  39. parent::__construct($name);
  40. }
  41. /**
  42. * Initialization of the command.
  43. *
  44. * @return void
  45. */
  46. protected function configure()
  47. {
  48. $this->setName('setup:db-declaration:generate-whitelist')
  49. ->setDescription(
  50. 'Generate whitelist of tables and columns that are allowed to be edited by declaration installer'
  51. )
  52. ->setDefinition(
  53. [
  54. new InputOption(
  55. self::MODULE_NAME_KEY,
  56. null,
  57. InputOption::VALUE_OPTIONAL,
  58. 'Name of the module where whitelist will be generated',
  59. FileResolverByModule::ALL_MODULES
  60. )
  61. ]
  62. );
  63. parent::configure();
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. protected function execute(InputInterface $input, OutputInterface $output) : int
  69. {
  70. $moduleName = $input->getOption(self::MODULE_NAME_KEY);
  71. try {
  72. $this->whitelistGenerator->generate($moduleName);
  73. } catch (ConfigurationMismatchException $e) {
  74. $output->writeln($e->getMessage());
  75. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  76. } catch (\Exception $e) {
  77. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  78. }
  79. return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
  80. }
  81. }