Added init command, improved plugin api
This commit is contained in:
		@@ -1,6 +1,33 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
use Spark\SparkApplication;
 | 
			
		||||
 | 
			
		||||
abstract class SparkPlug
 | 
			
		||||
{
 | 
			
		||||
    abstract public function load();
 | 
			
		||||
 | 
			
		||||
    public function getPlugin(string $name)
 | 
			
		||||
    {
 | 
			
		||||
        return SparkApplication::$instance->getPluginManager()->getPlugin($name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function get_resource(string $name) {
 | 
			
		||||
        return SparkApplication::$instance->getResourceManager()->getNamedResource($name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function read_config($file=null) {
 | 
			
		||||
        if (!$file) return;
 | 
			
		||||
        $abs = get_environment()->getConfigDirectory() . "/" . $file;
 | 
			
		||||
        if (!file_exists($abs)) {
 | 
			
		||||
            //fprintf(STDERR, "warning: Can't read config file %s\n", $abs);
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
        return (array)json_decode(file_get_contents($abs), true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function getProjectDirectory()
 | 
			
		||||
    {
 | 
			
		||||
        return SparkApplication::$instance->getEnvironment()->getProjectDirectory();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										42
									
								
								src/Commands/InitCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/Commands/InitCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
<?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:'init', description:'Initialize a new spark in the current directory')]
 | 
			
		||||
class InitCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    protected function configure()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $configDir = getcwd()."/.spark";
 | 
			
		||||
        if (is_dir($configDir)) {
 | 
			
		||||
            $output->writeln("<notice>Spark already initialized?</>");
 | 
			
		||||
            return Command::FAILURE;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        mkdir($configDir);
 | 
			
		||||
        mkdir($configDir."/plugins");
 | 
			
		||||
        file_put_contents($configDir."/spark.json", json_encode([
 | 
			
		||||
            'preload' => [
 | 
			
		||||
                './.spark/plugins/*'
 | 
			
		||||
            ],
 | 
			
		||||
            'scripts' => [
 | 
			
		||||
                'hello' => "echo 'Hello World\n'"
 | 
			
		||||
            ]
 | 
			
		||||
        ], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
 | 
			
		||||
 | 
			
		||||
        $output->writeln("<fg=green>Initialized new spark in ".getcwd()."/.spark</>");
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -37,6 +37,7 @@ class SparkApplication extends Application
 | 
			
		||||
        $this->add(new Commands\RunCommand());
 | 
			
		||||
        $this->add(new Commands\ResourcesCommand());
 | 
			
		||||
        $this->add(new Commands\ReplCommand());
 | 
			
		||||
        $this->add(new Commands\InitCommand());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user