Revert "Update resource definition API to allow extending schema"

This reverts commit 57ffa867c8.
This commit is contained in:
Toby Zerner 2019-11-16 10:15:03 +10:30
parent 1ccbe6568f
commit d889701ec6
2 changed files with 10 additions and 23 deletions

View File

@ -31,13 +31,9 @@ final class JsonApi implements RequestHandlerInterface
$this->baseUrl = $baseUrl;
}
public function resource(string $type): ResourceType
public function resource(string $type, $adapter, Closure $buildSchema = null): void
{
if (! isset($this->resources[$type])) {
$this->resources[$type] = new ResourceType($type);
}
return $this->resources[$type];
$this->resources[$type] = new ResourceType($type, $adapter, $buildSchema);
}
public function getResources(): array

View File

@ -2,6 +2,7 @@
namespace Tobyz\JsonApiServer;
use Closure;
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
use Tobyz\JsonApiServer\Schema\Type;
@ -9,12 +10,14 @@ final class ResourceType
{
private $type;
private $adapter;
private $schemaCallbacks = [];
private $buildSchema;
private $schema;
public function __construct(string $type)
public function __construct(string $type, AdapterInterface $adapter, Closure $buildSchema = null)
{
$this->type = $type;
$this->adapter = $adapter;
$this->buildSchema = $buildSchema;
}
public function getType(): string
@ -22,31 +25,19 @@ final class ResourceType
return $this->type;
}
public function adapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
return $this;
}
public function getAdapter(): AdapterInterface
{
return $this->adapter;
}
public function schema(callable $callback)
{
$this->schemaCallbacks[] = $callback;
return $this;
}
public function getSchema(): Type
{
if (! $this->schema) {
$this->schema = new Type;
run_callbacks($this->schemaCallbacks, [$this->schema]);
if ($this->buildSchema) {
($this->buildSchema)($this->schema);
}
}
return $this->schema;