4
4
5
5
use Blueprint \Contracts \Generator ;
6
6
use Blueprint \Models \Controller ;
7
+ use Blueprint \Models \Model ;
7
8
use Blueprint \Models \Statements \ResourceStatement ;
9
+ use Illuminate \Support \Str ;
8
10
9
11
class ResourceGenerator implements Generator
10
12
{
13
+ const INDENT = ' ' ;
11
14
/**
12
15
* @var \Illuminate\Contracts\Filesystem\Filesystem
13
16
*/
@@ -26,11 +29,13 @@ public function output(array $tree): array
26
29
27
30
$ stub = $ this ->files ->stub ('resource.stub ' );
28
31
32
+ $ this ->registerModels ($ tree );
33
+
29
34
/** @var \Blueprint\Models\Controller $controller */
30
35
foreach ($ tree ['controllers ' ] as $ controller ) {
31
36
foreach ($ controller ->methods () as $ method => $ statements ) {
32
37
foreach ($ statements as $ statement ) {
33
- if (!$ statement instanceof ResourceStatement) {
38
+ if (! $ statement instanceof ResourceStatement) {
34
39
continue ;
35
40
}
36
41
@@ -40,7 +45,7 @@ public function output(array $tree): array
40
45
continue ;
41
46
}
42
47
43
- if (!$ this ->files ->exists (dirname ($ path ))) {
48
+ if (! $ this ->files ->exists (dirname ($ path ))) {
44
49
$ this ->files ->makeDirectory (dirname ($ path ), 0755 , true );
45
50
}
46
51
@@ -56,12 +61,12 @@ public function output(array $tree): array
56
61
57
62
protected function getPath (string $ name )
58
63
{
59
- return config ('blueprint.app_path ' ) . '/Http/Resources/ ' . $ name . '.php ' ;
64
+ return config ('blueprint.app_path ' ). '/Http/Resources/ ' . $ name. '.php ' ;
60
65
}
61
66
62
67
protected function populateStub (string $ stub , ResourceStatement $ resource )
63
68
{
64
- $ stub = str_replace ('DummyNamespace ' , config ('blueprint.namespace ' ) . '\\Http \\Resources ' , $ stub );
69
+ $ stub = str_replace ('DummyNamespace ' , config ('blueprint.namespace ' ). '\\Http \\Resources ' , $ stub );
65
70
$ stub = str_replace ('DummyImport ' , $ resource ->collection () ? 'Illuminate \\Http \\Resources \\Json \\ResourceCollection ' : 'Illuminate \\Http \\Resources \\Json \\JsonResource ' , $ stub );
66
71
$ stub = str_replace ('DummyParent ' , $ resource ->collection () ? 'ResourceCollection ' : 'JsonResource ' , $ stub );
67
72
$ stub = str_replace ('DummyClass ' , $ resource ->name (), $ stub );
@@ -74,14 +79,54 @@ protected function populateStub(string $stub, ResourceStatement $resource)
74
79
75
80
private function buildData (ResourceStatement $ resource )
76
81
{
82
+ $ context = Str::singular ($ resource ->reference ());
83
+
84
+ /** @var \Blueprint\Models\Model $model */
85
+ $ model = $ this ->modelForContext ($ context );
86
+
87
+ $ data = [];
77
88
if ($ resource ->collection ()) {
78
- return 'return [
79
- \'data \' => $this->collection,
80
- ]; ' ;
89
+ $ data [] = 'return [ ' ;
90
+ $ data [] = self ::INDENT .'\'data \' => $this->collection, ' ;
91
+ $ data [] = ' ]; ' ;
92
+
93
+ return implode (PHP_EOL , $ data );
94
+ }
95
+
96
+ $ data [] = 'return [ ' ;
97
+ foreach ($ this ->visibleColumns ($ model ) as $ column ) {
98
+ $ data [] = self ::INDENT .'\'' .$ column .'\' => $this-> ' .$ column .', ' ;
81
99
}
100
+ $ data [] = ' ]; ' ;
101
+
102
+ return implode (PHP_EOL , $ data );
103
+ }
82
104
83
- return 'return [
84
- \'id \' => $this->id,
85
- ]; ' ;
105
+ private function visibleColumns (Model $ model )
106
+ {
107
+ return array_diff (array_keys ($ model ->columns ()), [
108
+ 'password ' ,
109
+ 'remember_token ' ,
110
+ ]);
111
+ }
112
+
113
+ private function modelForContext (string $ context )
114
+ {
115
+ if (isset ($ this ->models [Str::studly ($ context )])) {
116
+ return $ this ->models [Str::studly ($ context )];
117
+ }
118
+
119
+ $ matches = array_filter (array_keys ($ this ->models ), function ($ key ) use ($ context ) {
120
+ return Str::endsWith ($ key , '/ ' .Str::studly ($ context ));
121
+ });
122
+
123
+ if (count ($ matches ) === 1 ) {
124
+ return $ this ->models [$ matches [0 ]];
125
+ }
126
+ }
127
+
128
+ private function registerModels (array $ tree )
129
+ {
130
+ $ this ->models = array_merge ($ tree ['cache ' ] ?? [], $ tree ['models ' ] ?? []);
86
131
}
87
132
}
0 commit comments