webman/console plugin dòng lệnh

webman/console dựa trên symfony/console

Cài đặt

composer require webman/console

Các lệnh được hỗ trợ

Cách sử dụng
php webman lệnh
Ví dụ php webman version

Ghi chú
có thể đơn giản hóa thành ./webman lệnh trên hệ thống linux

Các lệnh được hỗ trợ

version

In ra số phiên bản của webman

route:list

In ra cấu hình định tuyến hiện tại

make:controller

Tạo một tập tin controller
Ví dụ php webman make:controller admin sẽ tạo ra một app/controller/AdminController.php
Ví dụ php webman make:controller api/user sẽ tạo ra một app/api/controller/UserController.php

make:model

Tạo một tập tin model
Ví dụ php webman make:model admin sẽ tạo ra một app/model/Admin.php
Ví dụ php webman make:model api/user sẽ tạo ra một app/api/model/User.php

make:middleware

Tạo một tập tin middleware
Ví dụ php webman make:middleware Auth sẽ tạo ra một app/middleware/Auth.php

make:command

Tạo tập tin lệnh tùy chỉnh
Ví dụ php webman make:command db:config sẽ tạo ra một app\command\DbConfigCommand.php

plugin:create

Tạo một plugin cơ bản
Ví dụ php webman plugin:create --name=foo/admin sẽ tạo ra hai thư mục config/plugin/foo/adminvendor/foo/admin
Xem thêm Tạo plugin cơ bản

plugin:export

Xuất plugin cơ bản
Ví dụ php webman plugin:export --name=foo/admin
Xem thêm Tạo plugin cơ bản

plugin:export

Xuất plugin ứng dụng
Ví dụ php webman plugin:export shop
Xem thêm Plugin ứng dụng

build:phar

Đóng gói dự án webman thành tập tin phar
Xem thêm Đóng gói phar

build:bin

Đóng gói dự án webman thành tập tin nhị phân có thể thực thi trực tiếp
Xem thêm Đóng gói phar

Lệnh tùy chỉnh

Người dùng có thể định nghĩa lệnh của riêng mình, ví dụ dưới đây là lệnh in ra cấu hình cơ sở dữ liệu

  • Thực thi php webman make:command config:mysql
  • Mở app/command/ConfigMySQLCommand.php và sửa đổi như sau
<?php

namespace app\command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigMySQLCommand extends Command
{
    protected static $defaultName = 'config:mysql';
    protected static $defaultDescription = 'Hiển thị cấu hình máy chủ MySQL hiện tại';

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Thông tin cấu hình MySQL như sau:');
        $config = config('database');
        $headers = ['name', 'default', 'driver', 'host', 'port', 'database', 'username', 'password', 'unix_socket', 'charset', 'collation', 'prefix', 'strict', 'engine', 'schema', 'sslmode'];
        $rows = [];
        foreach ($config['connections'] as $name => $db_config) {
            $row = [];
            foreach ($headers as $key) {
                switch ($key) {
                    case 'name':
                        $row[] = $name;
                        break;
                    case 'default':
                        $row[] = $config['default'] == $name ? 'true' : 'false';
                        break;
                    default:
                        $row[] = $db_config[$key] ?? '';
                }
            }
            if ($config['default'] == $name) {
                array_unshift($rows, $row);
            } else {
                $rows[] = $row;
            }
        }
        $table = new Table($output);
        $table->setHeaders($headers);
        $table->setRows($rows);
        $table->render();
        return self::SUCCESS;
    }
}

Kiểm tra

Chạy lệnh dòng lệnh php webman config:mysql

Kết quả giống như sau:

+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+
| name  | default | driver | host      | port | database | username | password | unix_socket | charset | collation       | prefix | strict | engine | schema | sslmode |
+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+
| mysql | true    | mysql  | 127.0.0.1 | 3306 | mysql    | root     | ******   |             | utf8    | utf8_unicode_ci |        | 1      |        |        |         |
+-------+---------+--------+-----------+------+----------+----------+----------+-------------+---------+-----------------+--------+--------+--------+--------+---------+

Tài liệu tham khảo thêm

http://www.symfonychina.com/doc/current/components/console.html