AbstractCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\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\ZimbraEmail\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. return 0;
  58. }
  59. /**
  60. * Add some custom actions here based on documentation https://symfony.com/doc/current/console.html
  61. */
  62. protected function setup()
  63. {
  64. }
  65. /**
  66. * @param InputInterface $input
  67. * @param OutputInterface $output
  68. * @param SymfonyStyle $io
  69. */
  70. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  71. {
  72. }
  73. /**
  74. * Function will be called before executing "process" function
  75. * @throws \Exception
  76. */
  77. protected function beforeProcess(InputInterface $input, OutputInterface $output)
  78. {
  79. }
  80. /**
  81. * Function will be called after executing "process" function
  82. * @param InputInterface $input
  83. * @param OutputInterface $output
  84. */
  85. protected function afterProcess(InputInterface $input, OutputInterface $output)
  86. {
  87. }
  88. }