定制 danc0/dcli 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danc0/dcli

Composer 安装命令:

composer create-project danc0/dcli

包简介

A lightweight PHP CLI application framework.

README 文档

README

A Basic PHP CLI App Framework. This project is dependency free and meant to be a lightweight starting point for PHP CLI applications.

These docs are a work in progress as this project is still in development.

Full Documentation (For Beta Version)

GitHub tag (latest by date) GitHub

Basic Setup

Install with: composer create-project danc0/dcli or download the latest package.

After creating your main file (ex: src/dcli) ensure you make that file executable and optionally add it to your path so you can call your application from anywhere. You will also want to create a Composer autoload file.

The most basic setup uses anonymous functions to process commands.

#!/usr/bin/php
<?php

if (php_sapi_name() !== 'cli') {
    exit;
}

require_once __DIR__ . '/vendor/autoload.php';

use App\Core\Application; // Main Application
use App\Core\Config; // Process the config
use App\Core\Command_Request; // Process the Request
use App\Core\Command_Container; // Stores the config and request environment
use App\IO\Output;

// Get any user set config values
$config = Config::load(__DIR__ . '/App/config.ini')->get();

// Load the request into the Command_Container
$Command_Request   = new Command_Request($argv);
$Command_Container = new Command_Container($config, $Command_Request->process());

// Load the Application
$app = Application::load($Command_Container);

// Set the commands
$app->set('hello-world', function () {
    Output::message('Hello World');
});

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

This is the most basic possible usage and does not take advantage of sub commands.

Passing Class@method to Call

This method also does not make use of sub commands but allows you to move your logic into a class.

#!/usr/bin/php
<?php

if (php_sapi_name() !== 'cli') {
    exit;
}

require_once __DIR__ . '/vendor/autoload.php';

use App\Core\Application; // Main Application
use App\Core\Config; // Process the config
use App\Core\Command_Request; // Process the Request
use App\Core\Command_Container; // Stores the config and request environment
use App\IO\Output;
use App\Commands\Hello\Test;

// Get any user set config values
$config = Config::load(__DIR__ . '/App/config.ini')->get();

// Load the request into the Command_Container
$Command_Request   = new Command_Request($argv);
$Command_Container = new Command_Container($config, $Command_Request->process());

// Load the Application
$app = Application::load($Command_Container);

// Set the commands
$app->set('hello-world', Test::class . '@test');

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

This will call the test method in the Test class.

File Structure Controlled Commands

This method makes use of sub commands. To use the advanced structure a specific project layout is required. Your classes that will handle the commands must be placed in the src/App/Commands directory. The command should be a sub folder. For example, the command hello should be placed in src/App/Commands/Hello. Within that directory a file named Default_Handler.php should be created. This, and all classes in the src/App/Commands directory should implement Command_Handler_Interface. This is the file that will be called on the command hello.

To use a sub command create an additional file with that sub-commands name. For example, hello test would require you to make a file named Test.php with a class of Test in src/App/Commands/Hello. This file will be called to process the sub command.

This requires a minimal setup in the main file.

#!/usr/bin/php
<?php

if (php_sapi_name() !== 'cli') {
    exit;
}

require_once __DIR__ . '/vendor/autoload.php';

use App\Core\Application; // Main Application
use App\Core\Config; // Process the config
use App\Core\Command_Request; // Process the Request
use App\Core\Command_Container; // Stores the config and request environment

// Get any user set config values
$config = Config::load(__DIR__ . '/App/config.ini')->get();

// Load the request into the Command_Container
$Command_Request   = new Command_Request($argv);
$Command_Container = new Command_Container($config, $Command_Request->process());

// Load the Application
$app = Application::load($Command_Container);

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

The Application class handles the logic and routing of the calls.

Passing Arguments

Arguments are treated as key value pairs passed in the the key starting with the -- annotation. Arguments can be passed into the command in the following way:

dcli hello --name "John Smith"
# OR

dcli hello --name John Smith

The command parser will return John Smith for the name argument for either of these approaches.

The command runner will automatically set these variables within your class when it resolves the handler. For example, if you have a Default_Handler with the argument name available the command runner will add the value of name to $this->name within your class.

Passing Flags

Flags are considered booleans as can be passed with either - or -- annotation.

The command runner will automatically set these variables within your class when it resolves the handler. For example, if you have a Default_Handler with the flag v available the command runner will set $this->v to true or false depending on if it was passed or not.

Command_Container

The Command_Container holds information about your Config and your Request Environment. To tell the Command Runner to load this into your handler class you can do one of two things. First you can declare a variable in your class public Command_Container $Command_Container this will be set AFTER the class is instantiated so you will not have access in the constructor, but will in the other methods of your class. If you need this available in your constructor function add Command_Container $Command_Container as a variable that needs to be passed into the class.

Event_Handler Global

The main Application Event_Handler can be injected into your handler classes using the same two techniques outlined above for the Command_Container

danc0/dcli 适用场景与选型建议

danc0/dcli 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 danc0/dcli 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 danc0/dcli 我们能提供哪些服务?
定制开发 / 二次开发

基于 danc0/dcli 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2022-02-26