tobento/app-console

应用程序控制台支持。

1.0.2 2023-12-19 15:50 UTC

This package is auto-updated.

Last update: 2024-09-19 17:27:40 UTC


README

使用控制台服务的控制台为应用程序提供支持。

目录

入门

使用此命令添加正在运行的最新版本的程序控制台项目。

composer require tobento/app-console

需求

  • PHP 8.0 或更高版本

文档

应用程序

如果您使用的是骨架,请查看 应用程序骨架

您还可以查看 应用程序 以了解更多有关应用程序的信息。

控制台启动

控制台启动执行以下操作

  • 如果不存在,则在根目录中创建应用程序文件
  • 实现控制台接口
use Tobento\App\AppFactory;
use Tobento\Service\Console\ConsoleInterface;
use Tobento\Service\Console\ConsoleFactoryInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Console\Boot\Console::class);
$app->booting();

// Implemented interfaces:
$consoleFactory = $app->get(ConsoleFactoryInterface::class);
$console = $app->get(ConsoleInterface::class);

// Run the app
$app->run();

如果您不使用 应用程序骨架,您可以调整根目录中的 ap 文件,以包含您的应用程序路径

// Get and run the application.
// (require __DIR__.'/app/app.php')->run();
(require __DIR__.'/path/to/app.php')->run();

创建命令

查看 控制台服务 - 创建命令 部分以了解有关创建命令的更多信息。

添加命令

您可以通过多种方式添加命令

使用应用程序

您可以使用应用程序 on 方法注册命令,仅当请求控制台时。

use Tobento\App\AppFactory;
use Tobento\Service\Console\ConsoleInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Console\Boot\Console::class);

// Adding commands:
$app->on(ConsoleInterface::class, function(ConsoleInterface $console) {
    $console->addCommand($command);
});

// Run the app
$app->run();

使用控制台启动

use Tobento\App\Boot;
use Tobento\App\Console\Boot\Console;

class AnyServiceBoot extends Boot
{
    public const BOOT = [
        // you may ensure the console boot.
        Console::class,
    ];
    
    public function boot(Console $console)
    {
        // you may add commands only if running in console:
        if ($console->runningInConsole()) {
            $console->addCommand($command);
        }
    }
}

调用命令

要调用应用程序命令,只需运行

php ap command:name

要获取可用命令列表

php ap list

致谢