MakeBootstrapCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Webman\Console\Commands;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Question\ConfirmationQuestion;
  8. use Webman\Console\Util;
  9. class MakeBootstrapCommand extends Command
  10. {
  11. protected static $defaultName = 'make:bootstrap';
  12. protected static $defaultDescription = 'Make bootstrap';
  13. /**
  14. * @return void
  15. */
  16. protected function configure()
  17. {
  18. $this->addArgument('name', InputArgument::REQUIRED, 'Bootstrap name');
  19. $this->addArgument('enable', InputArgument::OPTIONAL, 'Enable or not');
  20. }
  21. /**
  22. * @param InputInterface $input
  23. * @param OutputInterface $output
  24. * @return int
  25. */
  26. protected function execute(InputInterface $input, OutputInterface $output): int
  27. {
  28. $name = $input->getArgument('name');
  29. $enable = in_array($input->getArgument('enable'), ['no', '0', 'false', 'n']) ? false : true;
  30. $output->writeln("Make bootstrap $name");
  31. $name = str_replace('\\', '/', $name);
  32. if (!$bootstrap_str = Util::guessPath(app_path(), 'bootstrap')) {
  33. $bootstrap_str = Util::guessPath(app_path(), 'controller') === 'Controller' ? 'Bootstrap' : 'bootstrap';
  34. }
  35. $upper = $bootstrap_str === 'Bootstrap';
  36. if (!($pos = strrpos($name, '/'))) {
  37. $name = ucfirst($name);
  38. $file = app_path() . DIRECTORY_SEPARATOR . $bootstrap_str . DIRECTORY_SEPARATOR . "$name.php";
  39. $namespace = $upper ? 'App\Bootstrap' : 'app\bootstrap';
  40. } else {
  41. if($real_name = Util::guessPath(app_path(), $name)) {
  42. $name = $real_name;
  43. }
  44. if ($upper && !$real_name) {
  45. $name = preg_replace_callback('/\/([a-z])/', function ($matches) {
  46. return '/' . strtoupper($matches[1]);
  47. }, ucfirst($name));
  48. }
  49. $path = "$bootstrap_str/" . substr($upper ? ucfirst($name) : $name, 0, $pos);
  50. $name = ucfirst(substr($name, $pos + 1));
  51. $file = app_path() . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$name.php";
  52. $namespace = str_replace('/', '\\', ($upper ? 'App/' : 'app/') . $path);
  53. }
  54. if (is_file($file)) {
  55. $helper = $this->getHelper('question');
  56. $question = new ConfirmationQuestion("$file already exists. Do you want to override it? (yes/no)", false);
  57. if (!$helper->ask($input, $output, $question)) {
  58. return Command::SUCCESS;
  59. }
  60. }
  61. $this->createBootstrap($name, $namespace, $file);
  62. if ($enable) {
  63. $this->addConfig("$namespace\\$name", config_path() . '/bootstrap.php');
  64. }
  65. return self::SUCCESS;
  66. }
  67. /**
  68. * @param $name
  69. * @param $namespace
  70. * @param $file
  71. * @return void
  72. */
  73. protected function createBootstrap($name, $namespace, $file)
  74. {
  75. $path = pathinfo($file, PATHINFO_DIRNAME);
  76. if (!is_dir($path)) {
  77. mkdir($path, 0777, true);
  78. }
  79. $bootstrap_content = <<<EOF
  80. <?php
  81. namespace $namespace;
  82. use Webman\Bootstrap;
  83. class $name implements Bootstrap
  84. {
  85. public static function start(\$worker)
  86. {
  87. // Is it console environment ?
  88. \$is_console = !\$worker;
  89. if (\$is_console) {
  90. // If you do not want to execute this in console, just return.
  91. return;
  92. }
  93. }
  94. }
  95. EOF;
  96. file_put_contents($file, $bootstrap_content);
  97. }
  98. public function addConfig($class, $config_file)
  99. {
  100. $config = include $config_file;
  101. if(!in_array($class, $config ?? [])) {
  102. $config_file_content = file_get_contents($config_file);
  103. $config_file_content = preg_replace('/\];/', " $class::class,\n];", $config_file_content);
  104. file_put_contents($config_file, $config_file_content);
  105. }
  106. }
  107. }