Seedling β
Seedling is a variation of Leaf MVC built for console applications. It takes the ideas behind Leaf MVC and adapts them to the terminal, so you can build command-line tools with the same structure you already know.
It is built on Leaf Sprout, which allows you to create commands using a simple, familiar structure:
<?php
namespace App\Console;
use Leaf\Sprout\Command;
class GreetCommand extends Command
{
protected $signature = 'greet
{name=everyone : The name of the person}
{--greeting=Hello : The greeting to use}';
protected $description = 'An example command that greets a person with a specified greeting';
protected $help = 'This command allows you to greet a person with a custom greeting.';
protected function handle(): int
{
$name = $this->argument('name');
$greeting = $this->option('greeting');
$this->info("$greeting, $name!");
return 0;
}
}You can run this command from the terminal like so:
leaf greet John --greeting Hi # Hi, John!Creating a Seedling App β
Just like with the rest of Leaf, you can get started with a simple leaf create command:
leaf create <app-name> --consolecomposer create-project leafs/seedling <app-name>This will set up a new Seedling application in the specified <app-name> directory, complete with the necessary structure and dependencies for building console applications.
Quick Start β
Your generated Seedling application will have a similar structure to a Leaf MVC app, but focused on console commands.
βββ app
β βββ console
β βββ anything-else-you-need
βββ bin
βββ vendorThis file structure gives you just the basics you need to start building your console applications, but you can always add more directories, like models, jobs or services, as your application grows.
Running Commands β
In development, you can run your commands using the Leaf CLI, for example, you can run the greet command like so:
leaf greet John --greeting Hiphp leaf greet John --greeting HiInstalled modules bring their commands along too: add leafs/schema and the db: commands appear, add leafs/queue and queue:work shows up, all with no wiring on your part.
This makes it easy to build your application, but if you are distributing it, check this out
Writing commands β
All commands are stored in the app/console directory, and we've provided a simple example command to get you started. You can generate new commands from the console:
php leaf g:command deploy:siteThis creates app/console/DeploySiteCommand.php with the signature ready to edit. Made a mess? php leaf d:command deploy:site deletes it again.
Since Seedling is just like Leaf MVC, commands themselves work the same way they do in a Leaf MVC application: signatures, arguments, options, and interactive prompts via sprout()->prompt(). The writing commands documentation covers all of it.
Distribution β
If your Seedling application is intended to be distributed as a package or tool, you can set it up to be installed via composer, either in an app or globally. Your bin directory already contains a file named after your app (Leaf CLI names it for you during leaf create). Rename it to whatever you want the command to be called, for example mytool. This way if your command is installed globally, users can run:
mytool greet John --greeting HiAnd if it's installed in an app, they can run:
php vendor/bin/mytool greet John --greeting HiThe next thing to do is check the bin key in your composer.json file and make sure it points to the same name you chose for your file in the bin directory. For example:
"bin": [
"bin/mytool"
],And that's it! You can now distribute your Seedling application as a package or tool on Packagist or any other composer repository.
Phar Distribution
We don't yet have a built-in way to create Phar distributions of Seedling applications. If you need to distribute your application as a Phar, you'll need to set that up manually using tools like Box or similar.
