Created
April 22, 2023 09:15
-
-
Save atakde/7d6b01fc32d35e6aeb757066886f3ea2 to your computer and use it in GitHub Desktop.
Example CLI with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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