leafs/sprout
Composer 安装命令:
composer require leafs/sprout
包简介
Fast, lightweight and minimal CLI framework for PHP
README 文档
README
🌱 Fast, lightweight and minimal CLI framework for PHP. It provides a type-safe, feature-rich environment to build CLI apps of all sizes, plus a ton of helpers for building amazing experiences for users interacting with your CLI app.
- Not based on any Laravel or Symfony console components
- First-class support for Leaf & Leaf modules
- Comes with built in styling, input handling, output handling, and more
- Support for PHP 7.4 and above
- Easy to use, easy to extend
Installation
You can install Leaf Sprout using the Leaf CLI or Composer:
leaf install sprout
# or with composer
composer require leafs/sprout
Usage
To get started, you need to create a file which will be the entry point for your CLI app. This file should be executable and should have a shebang at the top of the file. Here's an example of a simple CLI app:
#!/usr/bin/env php <?php use Leaf\Sprout\Command; require __DIR__ . "/vendor/autoload.php"; $app = sprout()->createApp([ 'name' => 'My CLI App', 'version' => '1.0.0' ]); $app->command('greet', function (Command $app) { $command->write('Hello, world!'); }); $app->register(\MyCliApp\Commands\GreetCommand::class); $app->run();
You can then run your CLI app using the PHP CLI:
php my-cli-app greet
Writing Commands
You can write your commands in a functional way using the command() method on the app instance, or you can create a class that extends the Leaf\Sprout\Command class. Here's an example of a command written as a class:
<?php namespace MyCliApp\Commands; use Leaf\Sprout\Command; class GreetCommand extends Command { protected $signature = 'greet {name}'; protected $description = 'Greet a user'; public function handle() { $this->write("Hello, {$this->argument('name')}!"); } }
You can then register this command with the app instance:
$app->register(\MyCliApp\Commands\GreetCommand::class);
Styling Output
Leaf Sprout comes with a built-in output styling system that allows you to style your output using a simple API adapted from Symfony Console. Here's an example of how you can style your output:
$this->write("<info>Hello</info>, <comment>world</comment>!"); $this->write("<error>Error occurred!</error>"); $this->write("<question>Are you sure?</question>");
Or you can use pre-configured styles like success(), error(), warning(), and info():
$this->success('Operation successful'); $this->error('Operation failed'); $this->warning('This is a warning'); $this->info('This is some information');
Later versions will include more advanced styling options modelled after TailwindCSS and TermWind.
Input Handling
Leaf Sprout comes with a built-in input handling system that allows you to easily get input from the user. The easiest way is to use the prompt() method:
$name = sprout()->prompt([ 'type' => 'text', // 'select', 'confirm', 'password', 'number', 'text' 'message' => 'What is your name?', ]); $command->write("Hello, $name!"); // Hello, John Doe!
You can also have multiple prompts in a single command for more complex interactions like setting up a project via your CLI:
$possiblySetValue = $something ?? null; $results = sprout()->prompt([ [ 'type' => 'text', 'name' => 'name', 'message' => 'What is your project name?', 'default' => 'my-project', ], [ 'type' => $possiblySetValue ? null : 'select', 'name' => 'type', 'message' => 'What type of project do you want?', 'default' => 0, 'choices' => [ ['title' => 'Web', 'value' => 'web'], ['title' => 'API', 'value' => 'api'], ['title' => 'CLI', 'value' => 'cli'], ], ], [ 'type' => 'confirm', 'name' => 'install', 'message' => 'Do you want to install dependencies?', 'default' => true, ], ]); $results; // ['name' => 'my-project', 'type' => 'web', 'install' => true]
When the user runs the command, they will be prompted for the project name, type, and whether they want to install dependencies. The results will be stored in the $results variable. The prompt type can be text, select, confirm, password, number, or text, when set to null, the prompt will be skipped.
Besides prompts, you can also use the arguments() and options() methods to get arguments and options passed to the command:
$app->command('greet', function (Command $command) { $name = $command->argument('name'); $uppercase = $command->option('uppercase'); $greeting = "Hello, $name!"; if ($uppercase) { $greeting = strtoupper($greeting); } $command->write($greeting); });
Output Handling
Process Handling
Sprout comes with a built-in process handling system that allows you to run processes on the system. You can use the process() method to run a process:
$process = sprout()->createProcess('ls -la'); $process->onError(function ($error) use ($command) { $command->write("An error occurred: $error"); }); $process->run(function ($output) use ($command) { $command->write($output); }); $process->isSuccessful(); // true $process->getExitCode(); // 0
You can also use the run() method to run a process and log the output to the console in real-time, and then return it's exit code:
$exitCode = sprout()->run('ls -la'); if ($exitCode !== 0) { $command->error("An error occurred while running the process"); }
Composer & Npm Scripts
A lot of console applications allow you to install composer or npm dependencies, and sprout makes it a whole lot easier to do so:
sprout()->composer()->hasDependency('leafs/fs'); // true sprout()->composer()->run('composer-script'); sprout()->composer()->install(); // install all dependencies sprout()->composer()->install('leafs/fs'); // install a package sprout()->composer()->remove('leafs/fs'); // remove a package sprout()->npm()->hasDependency('@leafphp/vite'); // true sprout()->npm()->run('npm-script'); sprout()->npm()->install(); // install all dependencies sprout()->npm()->install('vite'); // install a package sprout()->npm()->remove('vite'); // remove a package
For npm, you can also specify the package manager you want to use:
sprout()->npm('yarn')->install(); // install all dependencies using yarn sprout()->npm('pnpm')->install('vite'); // install a package using pnpm sprout()->npm('bun')->install('vite'); // install a package using bun
All the composer and npm commands log the output to the console in real-time. If you want to change the output, you can pass a callback as the second argument:
sprout()->composer()->install(function ($output) { $command->write($output); }); sprout()->npm()->install('vite @leafphp/vite', function ($output) { $command->write("NPM -> $output"); });
The also return a process instance which you can use to get the output, check if the process was successful, and get the exit code:
$process = sprout()->composer()->install(); $process->isSuccessful(); // true
leafs/sprout 适用场景与选型建议
leafs/sprout 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.93k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 02 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「microframework」 「php」 「cli」 「console」 「leaf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 leafs/sprout 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 leafs/sprout 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 leafs/sprout 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A lightweight, secure-by-default PHP microframework built on Slim – providing Laravel-like features (ORM, authentication, migrations, caching) without the bloat. Perfect for building REST APIs and small-to-medium PHP applications.
Alfabank REST API integration
Reducido núcleo de trabajo para PHP
A Slim Framework MVC skeleton application for rapid development
A TidyPHP is a micro Framework for build fast and tidy web applications
统计信息
- 总下载量: 4.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 35
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-01