Update resource definition API to allow extending schema
This commit is contained in:
parent
ae46a35cbd
commit
57ffa867c8
|
|
@ -31,9 +31,13 @@ final class JsonApi implements RequestHandlerInterface
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resource(string $type, $adapter, Closure $buildSchema = null): void
|
public function resource(string $type): ResourceType
|
||||||
{
|
{
|
||||||
$this->resources[$type] = new ResourceType($type, $adapter, $buildSchema);
|
if (! isset($this->resources[$type])) {
|
||||||
|
$this->resources[$type] = new ResourceType($type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->resources[$type];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getResources(): array
|
public function getResources(): array
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Tobyz\JsonApiServer;
|
namespace Tobyz\JsonApiServer;
|
||||||
|
|
||||||
use Closure;
|
|
||||||
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
|
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
|
||||||
use Tobyz\JsonApiServer\Schema\Type;
|
use Tobyz\JsonApiServer\Schema\Type;
|
||||||
|
|
||||||
|
|
@ -10,14 +9,12 @@ final class ResourceType
|
||||||
{
|
{
|
||||||
private $type;
|
private $type;
|
||||||
private $adapter;
|
private $adapter;
|
||||||
private $buildSchema;
|
private $schemaCallbacks = [];
|
||||||
private $schema;
|
private $schema;
|
||||||
|
|
||||||
public function __construct(string $type, AdapterInterface $adapter, Closure $buildSchema = null)
|
public function __construct(string $type)
|
||||||
{
|
{
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
$this->adapter = $adapter;
|
|
||||||
$this->buildSchema = $buildSchema;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType(): string
|
public function getType(): string
|
||||||
|
|
@ -25,19 +22,31 @@ final class ResourceType
|
||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function adapter(AdapterInterface $adapter)
|
||||||
|
{
|
||||||
|
$this->adapter = $adapter;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getAdapter(): AdapterInterface
|
public function getAdapter(): AdapterInterface
|
||||||
{
|
{
|
||||||
return $this->adapter;
|
return $this->adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function schema(callable $callback)
|
||||||
|
{
|
||||||
|
$this->schemaCallbacks[] = $callback;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getSchema(): Type
|
public function getSchema(): Type
|
||||||
{
|
{
|
||||||
if (! $this->schema) {
|
if (! $this->schema) {
|
||||||
$this->schema = new Type;
|
$this->schema = new Type;
|
||||||
|
|
||||||
if ($this->buildSchema) {
|
run_callbacks($this->schemaCallbacks, [$this->schema]);
|
||||||
($this->buildSchema)($this->schema);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->schema;
|
return $this->schema;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue