Skip to content

Instantly share code, notes, and snippets.

@atakde
Created April 22, 2023 09:15
Show Gist options
  • Save atakde/7d6b01fc32d35e6aeb757066886f3ea2 to your computer and use it in GitHub Desktop.
Save atakde/7d6b01fc32d35e6aeb757066886f3ea2 to your computer and use it in GitHub Desktop.
Example CLI with PHP
<?php
namespace PhpCliAppExample;
use PhpCliAppExample\Commands\ExampleCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
class ExampleCLI
{
private Application $application;
public function __construct(Application $application)
{
$this->application = $application;
$this->addDefaultCommands();
}
public function run(): void
{
$this->application->run();
}
public function getApplication(): Application
{
return $this->application;
}
public function setApplication(Application $application): void
{
$this->application = $application;
}
public function addCommand(Command $command): void
{
$this->application->add($command);
}
public function getCommand(string $name): Command
{
return $this->application->get($name);
}
public function addDefaultCommands(): void
{
$this->application->add(new ExampleCommand());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment