Initial commit
This commit is contained in:
		
							
								
								
									
										38
									
								
								src/Commands/Command.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/Commands/Command.php
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								src/Commands/ReplCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/Commands/ReplCommand.php
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								src/Commands/ResourcesCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/Commands/ResourcesCommand.php
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								src/Commands/RunCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/Commands/RunCommand.php
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user