Christopher Vagnetoft
30dfd4889b
* Added request logging to com.noccy.apiclient * Added plugin com.noccy.watcher * Added pipe command and filter support * Fixes and stubs
53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Spark\Resource;
|
|
|
|
use ReflectionClass;
|
|
|
|
class ResourceManager
|
|
{
|
|
private $resourceTypes = [];
|
|
|
|
private $namedResources = [];
|
|
|
|
public function registerResourceType(string $name, string $type)
|
|
{
|
|
$refl = new ReflectionClass($type);
|
|
$this->resourceTypes[$name] = $type;
|
|
}
|
|
|
|
public function createResource(string $type, array $options)
|
|
{
|
|
if (!array_key_exists($type, $this->resourceTypes)) {
|
|
return null;
|
|
}
|
|
$resource = new $this->resourceTypes[$type]($options);
|
|
return $resource;
|
|
}
|
|
|
|
public function createNamedResource(string $name, string $type, array $options)
|
|
{
|
|
if (array_key_exists($name, $this->namedResources)) {
|
|
fprintf(STDERR, "warning: Redefining named resource %s\n", $name);
|
|
}
|
|
$resource = $this->createResource($type, $options);
|
|
$this->namedResources[$name] = $resource;
|
|
return $resource;
|
|
}
|
|
|
|
public function getNamedResource(string $name)
|
|
{
|
|
return $this->namedResources[$name] ?? null;
|
|
}
|
|
|
|
public function getAllNamedResources(): array
|
|
{
|
|
return $this->namedResources;
|
|
}
|
|
|
|
public function getAllResourceTypes(): array
|
|
{
|
|
return $this->resourceTypes;
|
|
}
|
|
}
|