自定义命令使用 symfony/console 包:

安装 symfony/console

  1. composer require symfony/console

编写自定义命令脚本

下图所示的 Commands 目录,即为自定义的命令脚本,仿照的laravel的目录结构:

  1. <?php
  2. namespace 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. class OpenServeCityLbs extends Command
  8. {
  9. protected function configure()
  10. {
  11. $this
  12. ->setName('Lbs:OpenServeCityLbs')
  13. ->setDescription('已开通服务城市lbs数据源');
  14. }
  15. protected function execute(InputInterface $input, OutputInterface $output)
  16. {
  17. return 1;
  18. }
  19. }

命令列表

.自定义命令 - 图1

上图所示的 Kernel.php 文件:

  1. <?php
  2. namespace console;
  3. class Kernel
  4. {
  5. /**
  6. * The Artisan commands provided by your application.
  7. *
  8. * @var array
  9. */
  10. public static $commands = [
  11. //
  12. \console\commands\Test::class,
  13. \console\commands\Script\CreateAreaTable::class,
  14. \console\commands\Script\OrderRefund::class,
  15. \console\commands\Lbs\OpenServeCityLbs::class,
  16. ];
  17. }

自定义命令入口文件

根目录下创建 artisan 文件作为命令入口文件(仿laravel框架的入口文件位置):

  1. #!/usr/bin/env php
  2. <?php
  3. require __DIR__ . '/vendor/autoload.php';
  4. use Dotenv\Dotenv;
  5. use Illuminate\Database\Capsule\Manager as Capsule;
  6. use Illuminate\Events\Dispatcher;
  7. use support\constants\Project;
  8. use Symfony\Component\Console\Application;
  9. use console\Kernel;
  10. use Webman\Config;
  11. # 加载配置文件
  12. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  13. Dotenv::createUnsafeImmutable(base_path())->load();
  14. } else {
  15. Dotenv::createMutable(base_path())->load();
  16. }
  17. Config::reload(config_path(), ['route']);
  18. foreach (config('autoload.files', []) as $file) {
  19. include_once $file;
  20. }
  21. foreach (config('bootstrap', []) as $class_name) {
  22. /** @var \Webman\Bootstrap $class_name */
  23. $class_name::start('');
  24. }
  25. # 数据库初始化
  26. if (!class_exists('\Illuminate\Database\Capsule\Manager')) {
  27. exit('数据库操作缺少库文件:\Illuminate\Database\Capsule\Manager' . PHP_EOL);
  28. }
  29. $capsule = new Capsule;
  30. $configs = config('database');
  31. $default_config = $configs['connections'][$configs['default']];
  32. $capsule->addConnection($default_config);
  33. // 创建链接(多库配置)
  34. foreach ($configs['connections'] as $name => $config) {
  35. $capsule->addConnection($config, $name);
  36. }
  37. // 数据库查询事件
  38. if (class_exists('\Illuminate\Events\Dispatcher')) {
  39. $capsule->setEventDispatcher(new Dispatcher(new \Illuminate\Container\Container));
  40. }
  41. // 设置全局静态可访问
  42. $capsule->setAsGlobal();
  43. # sql语句监听
  44. if (env('APP_DEBUG')) {
  45. Capsule::connection()->listen(function ($query) {
  46. //这里是执行sql后的监听回调方法
  47. $sql = vsprintf(str_replace("?", "'%s'", $query->sql), $query->bindings) . " \t[" . $query->time . ' ms] ';
  48. // 把SQL写入到日志文件中
  49. dp($sql);
  50. });
  51. }
  52. // 启动Eloquent
  53. $capsule->bootEloquent();
  54. # 命令操作
  55. $application = new Application();
  56. # 初始化自定义命令列表
  57. foreach (Kernel::$commands as $command) {
  58. $application->add(new $command);
  59. }
  60. Project::$REQUEST_ID = generate_uuid();
  61. $application->run();

执行自定义命令

php artisan xxx:xxx

具体使用可参考 [symfony/console](http://www.symfonychina.com/doc/current/components/console.html) 使用文档。