Kirby 命令行界面

1.5.0 2024-05-21 13:13 UTC

This package is auto-updated.

Last update: 2024-09-21 14:02:49 UTC


README

Kirby 命令行界面可以帮助您简化与 Kirby 安装相关的常见任务。

安装

通过 Composer

composer global require getkirby/cli

请确保将您的 composer bin 目录添加到您的 ~/.bash_profile (Mac OS 用户) 或 ~/.bashrc (Linux 用户)。

您的全局 composer 目录通常是 ~/.composer/vendor/bin~/.config/composer/vendor/bin。您可以通过运行 ... 来找到正确的路径。

composer -n config --global home

之后,将结果添加到您的 bash 配置文件中 ...

export PATH=~/.composer/vendor/bin:$PATH

它工作了吗?

通过在您的终端中运行以下命令来检查安装是否成功。

kirby

这将打印 Kirby CLI 版本和可用命令列表

可用核心命令

- kirby backup
- kirby clean:content
- kirby clear:cache
- kirby clear:lock
- kirby clear:logins
- kirby clear:media
- kirby clear:sessions
- kirby download
- kirby help
- kirby install
- kirby install:kit
- kirby install:repo
- kirby make:blueprint
- kirby make:collection
- kirby make:command
- kirby make:config
- kirby make:controller
- kirby make:language
- kirby make:model
- kirby make:plugin
- kirby make:snippet
- kirby make:template
- kirby make:user
- kirby plugin:install
- kirby plugin:remove
- kirby plugin:upgrade
- kirby register
- kirby remove:command
- kirby roots
- kirby unzip
- kirby upgrade
- kirby uuid:generate
- kirby uuid:populate
- kirby uuid:remove
- kirby version

编写命令

您可以通过 CLI 创建一个新的命令

kirby make:command hello

这将在您的安装中创建一个新的 site/commands 文件夹和一个新的 hello.php 文件

CLI 已经将基本框架放入了文件中

<?php

return [
    'description' => 'Nice command',
    'args' => [],
    'command' => static function ($cli): void {
        $cli->success('Nice command!');
    }
];

您可以在命令回调中定义您的命令逻辑。$cli 对象包含一系列方便的工具,用于创建输出、解析命令参数、创建提示等。

全局命令

您可能有一些需要在所有本地 Kirby 安装中使用的命令。这就是全局命令发挥作用的地方。您可以使用 --global 标志创建一个新的全局命令

kirby make:command hello --global

命令文件将放置在 ~/.kirby/commands/hello.php,并在任何地方自动可用。

命令环境

要为特定主机加载自定义环境配置,您可以设置一个环境变量

env KIRBY_HOST=production.com kirby mycommand

命令插件

您的 Kirby 插件可以定义自己的命令集:https://getkirby.com/docs/reference/plugins/extensions/commands

Kirby::plugin('your/plugin', [
  'commands' => [
    'your-plugin:test' => [
      'description' => 'Nice command',
      'args' => [],
      'command' => function ($cli) {
        $cli->success('My first plugin command');
      }
    ]
  ]
]);

检查已安装的命令

您始终可以通过再次运行 kirby 来检查您的命令是否已正确创建。

kirby

删除命令

一旦您不再需要命令,您可以使用 ... 来删除它

kirby remove:command hello

如果您有一个本地命令和一个全局命令,您可以选择删除哪一个。

调试

使用 -d--debug 参数以调试模式运行命令

kirby make:command hello --debug

输出格式化

向终端发送消息非常简单。

$cli->out()

$cli->out('This is some simple text');

$cli->success()

$cli->success('This is text in a nice green box');

$cli->error()

$cli->error('This is red text for errors');

$cli->bold()

$cli->bold('This is some bold text');

$cli->br()

// this will create a line break
$cli->br();

有关更多颜色和格式,请参阅 CLImate 文档:https://climate.thephpleague.com/styling/colors/

参数

您的命令可以定义一个必需和可选参数列表,这些参数需要由用户提供。

<?php

return [
    'description' => 'Hello world',
    'args' => [
        'name' => [
            'description' => 'The name for the greeting',
            'required'    => true
        ]
    ],
    'command' => static function ($cli): void {
        $cli->success('Hello ' . $cli->arg('name') . '!');
    }
];

现在可以通过提供名称来执行命令

kirby hello Joe

如果没有提供名称,将显示错误。

参数文档

参数可以是必需的,可以设置默认值等。有关附加选项,请参阅 CLImate 文档:https://climate.thephpleague.com/arguments/

提示

您可以从命令行获取参数,也可以在提示中请求它们

<?php

return [
    'description' => 'Hello world',
    'command' => static function ($cli): void {
        $name = $cli->prompt('Please enter a name:');
        $cli->success('Hello ' . $name . '!');
    }
];

作为第三种选择,您可以选择获取参数,如果未提供则请求它

<?php

return [
    'description' => 'Hello world',
    'args' => [
        'name' => [
            'description' => 'The name for the greeting',
        ]
    ],
    'command' => static function ($cli): void {
        $name = $cli->argOrPrompt('name', 'Please enter a name:');
        $cli->success('Hello ' . $name . '!');
    }
];

复选框、单选按钮等

CLI 还支持更复杂的方式从用户那里获取输入。查看 CLImate 文档了解如何处理用户输入:https://climate.thephpleague.com/terminal-objects/input/

组合命令

您可以在自定义命令中重用所有现有命令,以创建整个动作链。

<?php

return [
    'description' => 'Downloads the starterkit and the plainkit',
    'command' => static function ($cli): void {

        $cli->command('install:kit', 'starterkit');
        $cli->command('install:kit', 'plainkit');

        $cli->success('Starterkit and plainkit have been installed');
    }
];

什么是 Kirby?

  • getkirby.com – 了解 CMS。
  • 试用 – 使用我们的在线演示进行测试。或者下载我们的套件开始使用。
  • 文档 – 阅读官方指南、参考和食谱。
  • 问题 – 报告错误和其他问题。
  • 反馈 – 您对 Kirby 有什么想法?分享它。
  • 论坛 – 无论何时遇到困难,请不要犹豫,提出问题和寻求支持。
  • Discord – 加入社区,交流。
  • YouTube - 与 Bastian 视觉化地观看最新的视频教程。
  • Mastodon – 传播消息。
  • Instagram – 分享您的创作:#madewithkirby。

© 2009 Bastian Allgeier getkirby.com · 许可协议