webman 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env php
  2. <?php
  3. use Webman\Config;
  4. use Webman\Console\Command;
  5. use Webman\Console\Util;
  6. use support\Container;
  7. if (!Phar::running()) {
  8. chdir(__DIR__);
  9. }
  10. require_once __DIR__ . '/vendor/autoload.php';
  11. if (!$appConfigFile = config_path('app.php')) {
  12. throw new RuntimeException('Config file not found: app.php');
  13. }
  14. $appConfig = require $appConfigFile;
  15. if ($timezone = $appConfig['default_timezone'] ?? '') {
  16. date_default_timezone_set($timezone);
  17. }
  18. if ($errorReporting = $appConfig['error_reporting'] ?? '') {
  19. error_reporting($errorReporting);
  20. }
  21. if (!in_array($argv[1] ?? '', ['start', 'restart', 'stop', 'status', 'reload', 'connections'])) {
  22. require_once __DIR__ . '/support/bootstrap.php';
  23. } else {
  24. if (class_exists('Support\App')) {
  25. Support\App::loadAllConfig(['route']);
  26. } else {
  27. Config::reload(config_path(), ['route', 'container']);
  28. }
  29. }
  30. $cli = new Command();
  31. $cli->setName('webman cli');
  32. $cli->installInternalCommands();
  33. if (is_dir($command_path = Util::guessPath(app_path(), '/command', true))) {
  34. $cli->installCommands($command_path);
  35. }
  36. foreach (config('plugin', []) as $firm => $projects) {
  37. if (isset($projects['app'])) {
  38. foreach (['', '/app'] as $app) {
  39. if ($command_str = Util::guessPath(base_path() . "/plugin/$firm{$app}", 'command')) {
  40. $command_path = base_path() . "/plugin/$firm{$app}/$command_str";
  41. $cli->installCommands($command_path, "plugin\\$firm" . str_replace('/', '\\', $app) . "\\$command_str");
  42. }
  43. }
  44. }
  45. foreach ($projects as $name => $project) {
  46. if (!is_array($project)) {
  47. continue;
  48. }
  49. foreach ($project['command'] ?? [] as $class_name) {
  50. $reflection = new \ReflectionClass($class_name);
  51. if ($reflection->isAbstract()) {
  52. continue;
  53. }
  54. $properties = $reflection->getStaticProperties();
  55. $name = $properties['defaultName'];
  56. if (!$name) {
  57. throw new RuntimeException("Command {$class_name} has no defaultName");
  58. }
  59. $description = $properties['defaultDescription'] ?? '';
  60. $command = Container::get($class_name);
  61. $command->setName($name)->setDescription($description);
  62. $cli->add($command);
  63. }
  64. }
  65. }
  66. $cli->run();