|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
class Models extends AbstractGenerator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* {@inheritdoc} |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $signature = 'contentful:generate-models'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $description = 'Generate Eloquent models from Contentful content-types'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Execute the console command. |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
24
|
|
|
* @throws \Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public function handle() |
|
27
|
|
|
{ |
|
28
|
|
|
$contentTypes = $this->api->contentTypes(); |
|
29
|
|
|
|
|
30
|
|
|
if (! empty($contentTypes['items'])) { |
|
31
|
|
|
array_unshift($contentTypes['items'], $this->assetContentType()); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($contentTypes['items'] as $contentType) { |
|
34
|
|
|
if ($contentType['sys']['id'] !== 'asset') { |
|
35
|
|
|
$this->info('Content-Type: ' . Str::upper($contentType['name'])); |
|
36
|
|
|
$file = $this->createMapper($contentType); |
|
37
|
|
|
$this->line('Mapper "' . $file . '" created'); |
|
38
|
|
|
$file = $this->createModel($contentType); |
|
39
|
|
|
$this->line('Model "' . $file . '" created'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create migration file for given content-type. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $contentType |
|
49
|
|
|
* @return string |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function createModel(array $contentType): string |
|
53
|
|
|
{ |
|
54
|
|
|
$table = $this->tableName($contentType['sys']['id']); |
|
55
|
|
|
$model = Str::studly(Str::singular($table)); |
|
56
|
|
|
|
|
57
|
|
|
$stubPath = __DIR__ . '/stubs/model.stub'; |
|
58
|
|
|
$destPath = rtrim(config('contentful.generator.model'), '/') . '/' . $model . '.php'; |
|
59
|
|
|
|
|
60
|
|
|
return static::writeStub($stubPath, $destPath, [ |
|
61
|
|
|
'model' => $model, |
|
62
|
|
|
'table' => $table, |
|
63
|
|
|
'getters' => $this->modelGetters($table, $contentType['fields']), |
|
64
|
|
|
'properties' => $this->modelProperties($table, $contentType['fields']), |
|
65
|
|
|
]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return model mapper. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $contentType |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createMapper(array $contentType): string |
|
75
|
|
|
{ |
|
76
|
|
|
$table = $this->tableName($contentType['sys']['id']); |
|
77
|
|
|
$model = Str::studly(Str::singular($table)); |
|
78
|
|
|
|
|
79
|
|
|
$stubPath = __DIR__ . '/stubs/mapper.stub'; |
|
80
|
|
|
$destPath = rtrim(config('contentful.generator.mapper'), '/') . '/' . $model . 'Mapper.php'; |
|
81
|
|
|
|
|
82
|
|
|
return static::writeStub($stubPath, $destPath, [ |
|
83
|
|
|
'model' => $model |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return model getters. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $table |
|
91
|
|
|
* @param array $fields |
|
92
|
|
|
* @return string |
|
93
|
|
|
* @throws \Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function modelGetters($table, $fields): string |
|
96
|
|
|
{ |
|
97
|
|
|
$getters = []; |
|
98
|
|
|
foreach ($fields as $field) { |
|
99
|
|
|
if ($this->isFieldEnabled($field)) { |
|
100
|
|
|
$fieldDefinition = $this->fieldDefinition($table, $field); |
|
101
|
|
|
$getters[] = $fieldDefinition->modelGetter(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$getters = rtrim(implode("\n", array_map(function ($getter) { |
|
106
|
|
|
return $getter; |
|
107
|
|
|
}, $getters))); |
|
108
|
|
|
|
|
109
|
|
|
return ! empty($getters) ? "\n" . $getters : "\n\t\t//"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return model properties doc-block. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $table |
|
116
|
|
|
* @param array $fields |
|
117
|
|
|
* @return string |
|
118
|
|
|
* @throws \Exception |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function modelProperties($table, $fields): string |
|
121
|
|
|
{ |
|
122
|
|
|
$properties = []; |
|
123
|
|
|
foreach ($fields as $field) { |
|
124
|
|
|
if ($this->isFieldEnabled($field)) { |
|
125
|
|
|
$fieldDefinition = $this->fieldDefinition($table, $field); |
|
126
|
|
|
$properties[] = $fieldDefinition->modelProperty(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return ! empty($properties) ? "\n" . implode("\n", $properties) : ''; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|