File tree Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Original file line number Diff line number Diff line change @@ -31,9 +31,13 @@ public function __construct(string $baseUrl)
31
31
$ this ->baseUrl = $ baseUrl ;
32
32
}
33
33
34
- public function resource (string $ type, $ adapter , Closure $ buildSchema = null ): void
34
+ public function resource (string $ type ): ResourceType
35
35
{
36
- $ this ->resources [$ type ] = new ResourceType ($ type , $ adapter , $ buildSchema );
36
+ if (! isset ($ this ->resources [$ type ])) {
37
+ $ this ->resources [$ type ] = new ResourceType ($ type );
38
+ }
39
+
40
+ return $ this ->resources [$ type ];
37
41
}
38
42
39
43
public function getResources (): array
Original file line number Diff line number Diff line change 2
2
3
3
namespace Tobyz \JsonApiServer ;
4
4
5
- use Closure ;
6
5
use Tobyz \JsonApiServer \Adapter \AdapterInterface ;
7
6
use Tobyz \JsonApiServer \Schema \Type ;
8
7
9
8
final class ResourceType
10
9
{
11
10
private $ type ;
12
11
private $ adapter ;
13
- private $ buildSchema ;
12
+ private $ schemaCallbacks = [] ;
14
13
private $ schema ;
15
14
16
- public function __construct (string $ type, AdapterInterface $ adapter , Closure $ buildSchema = null )
15
+ public function __construct (string $ type )
17
16
{
18
17
$ this ->type = $ type ;
19
- $ this ->adapter = $ adapter ;
20
- $ this ->buildSchema = $ buildSchema ;
21
18
}
22
19
23
20
public function getType (): string
24
21
{
25
22
return $ this ->type ;
26
23
}
27
24
25
+ public function adapter (AdapterInterface $ adapter )
26
+ {
27
+ $ this ->adapter = $ adapter ;
28
+
29
+ return $ this ;
30
+ }
31
+
28
32
public function getAdapter (): AdapterInterface
29
33
{
30
34
return $ this ->adapter ;
31
35
}
32
36
37
+ public function schema (callable $ callback )
38
+ {
39
+ $ this ->schemaCallbacks [] = $ callback ;
40
+
41
+ return $ this ;
42
+ }
43
+
33
44
public function getSchema (): Type
34
45
{
35
46
if (! $ this ->schema ) {
36
47
$ this ->schema = new Type ;
37
48
38
- if ($ this ->buildSchema ) {
39
- ($ this ->buildSchema )($ this ->schema );
40
- }
49
+ run_callbacks ($ this ->schemaCallbacks , [$ this ->schema ]);
41
50
}
42
51
43
52
return $ this ->schema ;
You can’t perform that action at this time.
0 commit comments