summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2020-06-07 02:59:59 +0100
committerFbenas <philbeansburton@gmail.com>2020-06-07 02:59:59 +0100
commitb82f2c74d1fdb0e0aaaa4c93a2a904e376b310a2 (patch)
treebc1f3000424f9a837606bf610f80af0356ff7c5f /src
parent1a9f3aa5596c2a9fa9096e56d29fea1e14bdaad7 (diff)
First pass at a php IRC helperfor I/O handling
Diffstat (limited to 'src')
-rw-r--r--src/Application.php95
-rw-r--r--src/Command.php21
2 files changed, 116 insertions, 0 deletions
diff --git a/src/Application.php b/src/Application.php
new file mode 100644
index 0000000..88843ac
--- /dev/null
+++ b/src/Application.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace FBeans\BlaIRC;
+
+use FBeans\BlaIRC\Command;
+use Symfony\Component\Console\Application as SymfonyApplication;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\ArgvInput;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\ConsoleOutput;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Process\Process;
+
+abstract class Application
+{
+ protected $input;
+ protected $output;
+ protected $application;
+
+ // ... put here the code to run in your command
+
+ // this method must return an integer number with the "exit status code"
+ // of the command. You can also use these constants to make code more readable
+
+ // return this if there was no problem running the command
+ // (it's equivalent to returning int(0))
+
+
+ // or return this if some error happened during the execution
+ // (it's equivalent to returning int(1))
+ // return Command::FAILURE;
+ abstract protected function command(): Command;
+
+ public function fromArgv()
+ {
+ return $this->setInput(new ArgvInput());
+ }
+
+ public function fromStdin()
+ {
+ return $this->setInput(new ArrayInput($this->readStdin()));
+ }
+
+ public function toConsole()
+ {
+ return $this->setOutput(new ConsoleOutput);
+ }
+
+ public function toIRC()
+ {
+ return null;
+ }
+
+ public function __construct(InputInterface $input = null, OutputInterface $output = null)
+ {
+ $this->setInput($input)->setOutput($output);
+ }
+
+ public function setInput(InputInterface $input = null)
+ {
+ $this->input = $input;
+
+ return $this;
+ }
+
+ public function setOutput(OutputInterface $output = null)
+ {
+ $this->output = $output;
+
+ return $this;
+ }
+
+ public function run()
+ {
+ $this->application = new SymfonyApplication();
+ $command = $this->command();
+
+ $this->application->add($command);
+ $this->application->setDefaultCommand($command->getName());
+ $this->application->run($this->input, $this->output);
+ }
+
+ protected function readStdin()
+ {
+ $stream = fopen("php://stdin", "r");
+ $input_string = fgets($stream, 128);
+ $input_string = rtrim($input_string);
+
+ $input_array = explode(' ', $input_string);
+
+ fclose($stream);
+
+ return $input_array;
+ }
+}
diff --git a/src/Command.php b/src/Command.php
new file mode 100644
index 0000000..6fb3770
--- /dev/null
+++ b/src/Command.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace FBeans\BlaIRC;
+
+use Closure;
+use Symfony\Component\Console\Command\Command as SymfonyCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Command extends SymfonyCommand
+{
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ return call_user_func($this->execute_callback, $input, $output);
+ }
+
+ public function setExecuteCallback(Closure $callable)
+ {
+ $this->execute_callback = $callable;
+ }
+}