AbstractCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\CommandLine;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Style\SymfonyStyle;
  7. /**
  8. * Class Command
  9. * @package ModulesGarden\Servers\ProxmoxCloudVps\Core\CommandLine
  10. */
  11. class AbstractCommand extends \Symfony\Component\Console\Command\Command
  12. {
  13. /**
  14. * Command name
  15. * @var string
  16. */
  17. protected $name = null;
  18. /**
  19. * Command description
  20. * @var string
  21. */
  22. protected $description = '';
  23. /**
  24. * Command help text. Use --help to show
  25. * @var string
  26. */
  27. protected $help = '';
  28. /**
  29. * minimal command configuration
  30. */
  31. protected function configure()
  32. {
  33. $this
  34. ->setName($this->name)
  35. ->setDescription($this->description)
  36. ->setHelp($this->help)
  37. ->addOption('force', 'f', InputOption::VALUE_OPTIONAL, 'Force script to run, without checking if another instance is running', false);
  38. $this->setup();
  39. }
  40. /**
  41. * Execute command
  42. * @param InputInterface $input
  43. * @param OutputInterface $output
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. try
  48. {
  49. $this->beforeProcess($input, $output);
  50. $this->process($input, $output, new SymfonyStyle($input, $output));
  51. $this->afterProcess($input, $output);
  52. }
  53. catch(\Exception $ex)
  54. {
  55. (new SymfonyStyle($input, $output))->error($ex->getMessage());
  56. }
  57. }
  58. /**
  59. * Add some custom actions here based on documentation https://symfony.com/doc/current/console.html
  60. */
  61. protected function setup()
  62. {
  63. }
  64. /**
  65. * @param InputInterface $input
  66. * @param OutputInterface $output
  67. * @param SymfonyStyle $io
  68. */
  69. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  70. {
  71. }
  72. /**
  73. * Function will be called before executing "process" function
  74. * @throws \Exception
  75. */
  76. protected function beforeProcess(InputInterface $input, OutputInterface $output)
  77. {
  78. }
  79. /**
  80. * Function will be called after executing "process" function
  81. * @param InputInterface $input
  82. * @param OutputInterface $output
  83. */
  84. protected function afterProcess(InputInterface $input, OutputInterface $output)
  85. {
  86. }
  87. }