定制 utopia-php/console 二次开发

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

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

utopia-php/console

Composer 安装命令:

composer require utopia-php/console

包简介

Console helpers for logging, prompting, and executing commands

README 文档

README

Important

This repository is a read-only mirror of the utopia-php monorepo. Development happens in packages/console — please open issues and pull requests there.

Small collection of helpers for working with PHP command line applications. The Console class focuses on everyday needs such as logging, prompting users for input, executing external commands, and building long-running daemons.

Installation

Install using Composer:

composer require utopia-php/console

Usage

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

use Utopia\Console;
use Utopia\Command;

Console::success('Ready to work!');

$answer = Console::confirm('Continue? [y/N]');

if ($answer !== 'y') {
    Console::warning('Aborting...');
    Console::exit(1);
}

$output = '';
$stderr = '';
$command = new Command(PHP_BINARY)
    ->option('-r', 'echo "Hello";');

$exitCode = Console::execute($command, '', $output, $stderr, 3);

Console::log("Command returned {$exitCode} with: {$output}");

Log Messages

Console::log('Plain log');        // stdout
Console::success('Green log');    // stdout
Console::info('Blue log');        // stdout
Console::warning('Yellow log');   // stderr
Console::error('Red log');        // stderr

Execute Commands

Console::execute() returns the exit code and writes stdout and stderr into the referenced output variables. Pass a timeout (in seconds) to stop long-running processes and an optional progress callback to stream intermediate output. Prefer Utopia\Command or argv arrays when you want structured command building.

$command = new Command(PHP_BINARY)
    ->option('-r', 'fwrite(STDOUT, "success\\n");');

$output = '';
$input = '';
$stderr = '';
$exitCode = Console::execute($command, $input, $output, $stderr, 3);

echo $exitCode;  // 0
echo $output;    // "success\n"

Build Commands

Use flag() for switches without a value, option() for keys that take a value, and argument() for positional arguments.

$command = new Command('tar')
    ->flag('-cz')
    ->option('-f', 'archive.tar.gz')
    ->option('-C', '/tmp/project')
    ->argument('.');

Compose Commands

Use the static helpers when you need shell operators such as pipes, &&, ||, grouping, or redirects.

$pipeline = Command::pipe(
    new Command('ps')->flag('-ef'),
    new Command('grep')->argument('php-fpm'),
    new Command('wc')->flag('-l'),
);

$deploy = Command::and(
    Command::group(
        Command::or(
            new Command('build'),
            new Command('build:fallback'),
        )
    ),
    new Command('publish'),
);

$logs = Command::appendStdout(
    Command::pipe(
        new Command('cat')->argument('app.log'),
        new Command('grep')->argument('ERROR'),
    ),
    'errors.log',
);

Plain commands execute in argv mode. Composed, grouped, and redirected commands execute through shell syntax.

Create a Daemon

Use Console::loop() to build daemons without tight loops. The helper sleeps between iterations and periodically triggers garbage collection.

<?php

use Utopia\Console;

Console::loop(function () {
    echo "Hello World\n";
}, 1); // 1 second

System Requirements

Utopia Console requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

License

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php

utopia-php/console 适用场景与选型建议

utopia-php/console 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「cli」 「console」 「terminal」 「utopia」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 utopia-php/console 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 147.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 17
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-20