128 lines
3.3 KiB
PHP
128 lines
3.3 KiB
PHP
<?php
|
|
|
|
use Spark\Commands\Command;
|
|
use Spark\Environment\Environment;
|
|
use Spark\Pipe\Filters\FilterInterface;
|
|
use Spark\Pipe\Filters\PhpFilter;
|
|
use Spark\Resource\ResourceType;
|
|
use Spark\SparkApplication;
|
|
|
|
function object(...$data) {
|
|
return (object)$data;
|
|
}
|
|
|
|
// ------- Helpers -------
|
|
|
|
$HELPERS = [];
|
|
|
|
function register_helper(string $name, callable $helper) {
|
|
global $HELPERS;
|
|
$HELPERS[$name] = $helper;
|
|
}
|
|
|
|
function helper(string $name, ...$args) {
|
|
global $HELPERS;
|
|
if (!array_key_exists($name, $HELPERS)) {
|
|
fprintf(STDERR, "error: No helper %s registered", $name);
|
|
return false;
|
|
}
|
|
$helper = $HELPERS[$name];
|
|
return $helper(...$args);
|
|
}
|
|
|
|
// ------ Plugins ------
|
|
|
|
function register_plugin(string $name, SparkPlug $plugin) {
|
|
$refl = new ReflectionClass($plugin);
|
|
$psr4 = object(
|
|
namespace: $refl->getNamespaceName()."\\",
|
|
path: dirname($refl->getFileName())."/"
|
|
);
|
|
spl_autoload_register(function ($class) use ($psr4) {
|
|
if (str_starts_with($class, $psr4->namespace)) {
|
|
$part = substr($class, strlen($psr4->namespace));
|
|
$file = $psr4->path . strtr($part, "\\", DIRECTORY_SEPARATOR).".php";
|
|
if (file_exists($file)) {
|
|
require_once $file;
|
|
}
|
|
}
|
|
});
|
|
SparkApplication::$instance->getPluginManager()->registerPlugin($name, $plugin);
|
|
}
|
|
|
|
function get_plugin(string $name) {
|
|
return SparkApplication::$instance->getPluginManager()->getPlugin($name);
|
|
}
|
|
|
|
// ------ Commands ------
|
|
|
|
function register_command(Command $command) {
|
|
SparkApplication::$instance->add($command);
|
|
}
|
|
|
|
function get_environment(): Environment {
|
|
return SparkApplication::$instance->getEnvironment();
|
|
}
|
|
|
|
// ------ Resources ------
|
|
|
|
function register_resource_type(string $name, string $type) {
|
|
SparkApplication::$instance->getResourceManager()->registerResourceType($name, $type);
|
|
}
|
|
|
|
function create_resource(string $name, string $type, ...$options) {
|
|
return SparkApplication::$instance->getResourceManager()->createNamedResource($name, $type, $options);
|
|
}
|
|
|
|
function get_resource(string $name) {
|
|
return SparkApplication::$instance->getResourceManager()->getNamedResource($name);
|
|
}
|
|
|
|
function resource(string $name) {
|
|
return get_resource($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);
|
|
}
|
|
|
|
|
|
// ------ Filters ------
|
|
|
|
$FILTERS = [];
|
|
|
|
function register_filter(string $name, string|callable $filter) {
|
|
global $FILTERS;
|
|
$FILTERS[$name] = $filter;
|
|
}
|
|
|
|
function get_registered_filters(): array
|
|
{
|
|
global $FILTERS;
|
|
return array_keys($FILTERS);
|
|
}
|
|
|
|
function get_filter(string $name, array $args=[]): null|FilterInterface|callable
|
|
{
|
|
global $FILTERS;
|
|
$filter = $FILTERS[$name]??null;
|
|
if (is_string($filter) && class_exists($filter)) {
|
|
$filter = new $filter;
|
|
}
|
|
if (is_callable($filter)) {
|
|
$filter = new PhpFilter($filter);
|
|
}
|
|
if ($filter instanceof FilterInterface) {
|
|
$filter->setArguments($args);
|
|
}
|
|
return $filter;
|
|
}
|
|
|
|
require_once __DIR__."/http.php";
|