CLI & Artisan
Yabasi framework provides a powerful Command Line Interface (CLI) tool, similar to Laravel's Artisan, to help you manage and interact with your application. This tool simplifies many common tasks and allows you to create custom commands for your specific needs.
1. Yabasi CLI Overview
The Yabasi CLI is a versatile tool that automates many development tasks, from generating boilerplate code to managing database migrations. To use the Yabasi CLI, you typically run commands in your terminal from your project's root directory.
php
yabasi
command:name
2. Basic Commands
Yabasi comes with several built-in commands to help you generate common components of your application:
make:controller
Creates a new controller class.
php
yabasi
make:controller
UserController
make:model
Generates a new model class.
php
yabasi
make:model
User
make:middleware
Creates a new middleware class.
php
yabasi
make:middleware
AuthMiddleware
make:migration
Generates a new database migration file.
php
yabasi
make:migration
create_users_table
3. Database Commands
Yabasi provides several commands to manage your database migrations:
migrate
: Run all pending migrationsmigrate:rollback
: Rollback the last database migrationmigrate:reset
: Rollback all database migrationsmigrate:refresh
: Reset and re-run all migrationsmigrate:status
: Show the status of each migrationdb:dump
: Create a database dumpdb:restore
: Restore a database from a dump
4. Creating Custom Commands
You can create your own custom commands in Yabasi. Here's an example of how to structure a custom command:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CustomCommand extends Command
{
protected static $defaultName = 'app:custom-command';
protected function configure()
{
$this->setDescription('A custom command description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Custom command executed!');
return Command::SUCCESS;
}
}
After creating your custom command, you need to register it in the Console class:
protected function registerCommands(): void
{
$this->console->add(new CustomCommand());
}
5. Console Class
The Console class in Yabasi is responsible for registering and running CLI commands. It uses Symfony's Console component to manage commands.
class Console
{
protected Container $container;
protected SymfonyConsole $console;
public function __construct(Container $container)
{
$this->container = $container;
$this->console = new SymfonyConsole('Yabasi', '1.0.0');
$this->registerCommands();
}
public function run(array $argv): int
{
return $this->console->run();
}
}
6. Arguments and Options
You can add arguments and options to your custom commands to make them more flexible and powerful. Here's an example of how to define and use arguments and options:
protected function configure()
{
$this
->setName('app:greet')
->setDescription('Greet someone')
->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?')
->addOption('yell', 'y', InputOption::VALUE_NONE, 'Yell in uppercase letters');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$greeting = "Hello, $name!";
if ($input->getOption('yell')) {
$greeting = strtoupper($greeting);
}
$output->writeln($greeting);
return Command::SUCCESS;
}
7. Output Formatting
Yabasi CLI commands support colorized and formatted output to enhance readability and user experience. Here's how you can use various output styles:
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>This is a success message</info>');
$output->writeln('<comment>This is a comment</comment>');
$output->writeln('<question>This is a question</question>');
$output->writeln('<error>This is an error</error>');
return Command::SUCCESS;
}
8. Interactive Commands
You can create interactive commands that ask for user input or confirmation. Here's an example:
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$question = new Question('Please enter your name: ', 'User');
$name = $helper->ask($input, $output, $question);
$question = new ConfirmationQuestion('Do you want to continue? (y/n) ', false);
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}
$output->writeln("Hello, $name! You chose to continue.");
return Command::SUCCESS;
}
9. Help and Documentation
Yabasi automatically generates help documentation for your commands based on the descriptions and configurations you provide. You can view help for a command by running:
php
yabasi
command:name
--help
To provide more detailed help for your custom commands, you can override the configure()
method:
protected function configure()
{
$this
->setName('app:custom-command')
->setDescription('A custom command description')
->setHelp('This command allows you to...')
->addArgument('name', InputArgument::REQUIRED, 'Name description')
->addOption('option', null, InputOption::VALUE_NONE, 'Option description')
;
}
10. Best Practices
When writing CLI commands for Yabasi, consider the following best practices:
- Keep commands focused on a single task
- Use meaningful names for your commands, arguments, and options
- Provide clear and concise descriptions for your commands and their parameters
- Use input validation to prevent errors
- Implement proper error handling and provide informative error messages
- Use output formatting to improve readability
- For long-running commands, consider adding progress indicators
- Write unit tests for your commands to ensure they work as expected
Example: Progress Bar
For long-running tasks, you can use a progress bar to keep the user informed:
use Symfony\Component\Console\Helper\ProgressBar;
protected function execute(InputInterface $input, OutputInterface $output)
{
$progressBar = new ProgressBar($output, 100);
$progressBar->start();
for ($i = 0; $i < 100; $i++) {
// ... do some work here
$progressBar->advance();
}
$progressBar->finish();
return Command::SUCCESS;
}
By following these best practices and utilizing the features provided by Yabasi's CLI tools, you can create powerful, user-friendly command-line interfaces for your application. These tools can significantly improve your development workflow and provide valuable utilities for managing your Yabasi projects.