Initial commit

This commit is contained in:
2021-12-07 17:26:34 +01:00
commit 0c9fd2e892
15 changed files with 674 additions and 0 deletions

38
src/Commands/Command.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Spark\Commands;
use Flare\SparkApplication;
use Spark\Environment\Environment;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as CommandCommand;
abstract class Command extends CommandCommand
{
public function getEnvironment(): Environment
{
/** @var SparkApplication */
$app = $this->getApplication();
if (!$app) return null;
return $app->getEnvironment();
}
public function loadEnvironment(string $path)
{
/** @var SparkApplication */
$app = $this->getApplication();
$app->loadEnvironment($path);
}
public function createEnvironment(string|null $path=null)
{
if (empty($path)) {
$path = getcwd();
}
/** @var SparkApplication */
$app = $this->getApplication();
$app->createEnvironment($path);
$app->loadEnvironment($path);
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Spark\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Spark\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name:'repl', description:'Interactive REPL for PHP')]
class ReplCommand extends Command
{
protected function configure()
{
$this->addOption("list", null, InputOption::VALUE_NONE, "List the available scripts");
$this->addArgument("script", InputArgument::OPTIONAL, "The script too run (see --list)");
$this->addArgument("args", InputArgument::OPTIONAL|InputArgument::IS_ARRAY, "Arguments to the script");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$env = $this->getEnvironment();
while (true) {
$cmd = readline("repl> ");
if ($cmd) readline_add_history($cmd);
try {
$ret = @eval("return {$cmd};");
//$output->writeln(json_encode($ret,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
//$output->writeln("<info>".var_export($ret,true)."</>");
dump($ret);
} catch (\Throwable $t) {
$output->writeln("<error>".$t->getMessage()."</>");
}
}
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Spark\Commands;
use Spark\SparkApplication;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name:'resources', description:'List defined resources')]
class ResourcesCommand extends Command
{
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$env = $this->getEnvironment();
$app = $this->getApplication();
/** @var SparkApplication $app */
$resources = $app->getResourceManager();
$types = $resources->getAllResourceTypes();
$named = $resources->getAllNamedResources();
$output->writeln("<options=bold>Resource Types:</>");
$table = new Table($output);
$table->setStyle("compact");
$table->setHeaders([ "Type", "Class" ]);
$table->setColumnWidth(0, 10);
$map = [];
foreach ($types as $type=>$class) {
$map[$class] = $type;
$table->addRow([ $type, $class ]);
}
$table->render();
$output->writeln("");
$output->writeln("<options=bold>Named Resources:</>");
$table = new Table($output);
$table->setStyle("compact");
$table->setHeaders([ "Name", "Type", "Info" ]);
$table->setColumnWidth(0, 10);
$table->setColumnWidth(1, 6);
foreach ($named as $name=>$class) {
$table->addRow([ $name, $map[get_class($class)], $class->info() ]);
}
$table->render();
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Spark\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Spark\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name:'run', description:'Run a script')]
class RunCommand extends Command
{
protected function configure()
{
$this->addOption("list", null, InputOption::VALUE_NONE, "List the available scripts");
$this->addArgument("script", InputArgument::OPTIONAL, "The script too run (see --list)");
$this->addArgument("args", InputArgument::OPTIONAL|InputArgument::IS_ARRAY, "Arguments to the script");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$env = $this->getEnvironment();
if ($input->getOption("list")) {
$output->writeln($env->getDefinedScripts());
return Command::SUCCESS;
} elseif ($script = $input->getArgument('script')) {
$env->runScript($script, $input->getArgument('args'), $input, $output);
}
return Command::SUCCESS;
}
}