Multiple fixes
* Implemented ScriptRunner with environment expansion and cleaner code. * Added ApiClient plugin (com.noccy.apiclient) * Renamed CHANGELOG.md to VERSIONS.md * Shuffled buildtools * Added first unittests
This commit is contained in:
@@ -8,6 +8,7 @@ use Spark\SparkApplication;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
class Environment
|
||||
{
|
||||
@@ -36,12 +37,23 @@ class Environment
|
||||
return array_keys($this->config['scripts']??[]);
|
||||
}
|
||||
|
||||
public function getScriptRunner(): ScriptRunner
|
||||
{
|
||||
$runner = new ScriptRunner();
|
||||
$runner->setDirectory($this->getProjectDirectory());
|
||||
foreach ((array)$this->config['scripts'] as $name => $script) {
|
||||
$runner->defineScript($name, $script);
|
||||
}
|
||||
return $runner;
|
||||
}
|
||||
|
||||
/*
|
||||
public function runScript(string $name, array $args, InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$script = $this->config['scripts'][$name]??$name;
|
||||
|
||||
if (is_string($script)) {
|
||||
$this->execScript($script, $args);
|
||||
$this->execScript($script, $args, $output);
|
||||
} elseif (is_array($script)) {
|
||||
foreach ($script as $row) {
|
||||
$a = str_getcsv($row, ' ', "'");
|
||||
@@ -50,13 +62,13 @@ class Environment
|
||||
$c = ($this->config['scripts'][substr($c,1)])??$c;
|
||||
$this->runScript($c, $a, $input, $output);
|
||||
} else {
|
||||
$this->execScript($c, $a);
|
||||
$this->execScript($c, $a, $output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function execScript(string $script, array $args)
|
||||
private function execScript(string $script, array $args, OutputInterface $output)
|
||||
{
|
||||
// call script directly
|
||||
if (str_ends_with($script, '.php')) {
|
||||
@@ -74,6 +86,7 @@ class Environment
|
||||
passthru($script);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public function loadEnvironment()
|
||||
{
|
||||
@@ -84,9 +97,24 @@ class Environment
|
||||
if (!array_key_exists('project_dir', $this->config)) {
|
||||
return;
|
||||
}
|
||||
|
||||
chdir($this->config['project_dir']);
|
||||
|
||||
$envfile = $this->config['project_dir']."/.env";
|
||||
if (file_exists($envfile)) {
|
||||
$dotenv = new Dotenv();
|
||||
$dotenv->load($envfile);
|
||||
}
|
||||
|
||||
$blacklistFile = $this->config['config_dir']."/blacklist";
|
||||
if (file_exists($blacklistFile)) {
|
||||
$blacklist = json_decode(file_get_contents($blacklistFile), true);
|
||||
}
|
||||
|
||||
if (empty($this->config['preload'])) {
|
||||
fprintf(STDERR, "Error: Missing or malformed spark.json file.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// $this->logger->info("Loading environment...");
|
||||
$preloads = [];
|
||||
$root = $this->config['project_dir'];
|
||||
@@ -102,7 +130,7 @@ class Environment
|
||||
if (!str_starts_with($item, "/")) {
|
||||
$item = $this->getProjectDirectory() . "/" . $item;
|
||||
}
|
||||
if (is_file($item)) {
|
||||
if (is_file($item) && fnmatch("*.php", $item)) {
|
||||
// $this->logger->debug("Preloading file {$item}");
|
||||
try {
|
||||
include_once($item);
|
||||
@@ -119,8 +147,8 @@ class Environment
|
||||
//$this->logger->error("Error preloading plugin {$item}: {$t->getMessage()} in {$t->getFile()} on line {$t->getLine()}");
|
||||
}
|
||||
} else {
|
||||
fprintf(STDERR, "warning: Could not preload %s\n", $item);
|
||||
//$this->logger->warning("Could not preload {$item}");
|
||||
// fprintf(STDERR, "warning: Could not preload %s\n", $item);
|
||||
// $this->logger->warning("Could not preload {$item}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +191,11 @@ class Environment
|
||||
return $env;
|
||||
}
|
||||
|
||||
|
||||
public function expandString(string $input): string
|
||||
{
|
||||
return preg_replace_callback('/([\%\$]\{(.+?)\}/', function ($v) {
|
||||
print_r($v);
|
||||
}, $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user