Skip to content

Commit 5326c40

Browse files
committed
Fix a bug in the resource-file:from-database command
1 parent 87e79b6 commit 5326c40

File tree

3 files changed

+23
-69
lines changed

3 files changed

+23
-69
lines changed

src/DatabaseParsers/MysqlParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ protected function getTransfredFields(array $columns)
248248

249249
foreach ($columns as $column) {
250250
$properties['name'] = $column->COLUMN_NAME;
251-
$properties['labels'] = $this->getLabel($column->COLUMN_NAME);
252251
$properties['is-nullable'] = ($column->IS_NULLABLE == 'YES');
253252
$properties['data-value'] = $column->COLUMN_DEFAULT;
254253
$properties['data-type'] = $this->getDataType($column->DATA_TYPE, $column->COLUMN_TYPE);
@@ -260,7 +259,6 @@ protected function getTransfredFields(array $columns)
260259
$properties['comment'] = $column->COLUMN_COMMENT ?: null;
261260
$properties['options'] = $this->getHtmlOptions($column->DATA_TYPE, $column->COLUMN_TYPE);
262261
$properties['is-unsigned'] = (strpos($column->COLUMN_TYPE, 'unsigned') !== false);
263-
$properties['html-type'] = $this->getHtmlType($column->DATA_TYPE);
264262

265263
$constraint = $this->getForeignConstraint($column->COLUMN_NAME);
266264

@@ -275,6 +273,7 @@ protected function getTransfredFields(array $columns)
275273
}
276274
$localeGroup = Helpers::makeLocaleGroup($this->tableName);
277275
$fields = FieldTransformer::fromArray($collection, $localeGroup, $this->languages);
276+
$this->setHtmlType($fields);
278277

279278
return $fields;
280279
}

src/DatabaseParsers/ParserBase.php

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use CrestApps\CodeGenerator\Support\Config;
99
use CrestApps\CodeGenerator\Support\FieldsOptimizer;
1010
use CrestApps\CodeGenerator\Support\Helpers;
11+
use CrestApps\CodeGenerator\Support\ResourceMapper;
12+
use CrestApps\CodeGenerator\Support\Str;
1113
use CrestApps\CodeGenerator\Traits\CommonCommand;
1214
use Exception;
1315

@@ -117,9 +119,7 @@ protected function getFields()
117119
public function getResource()
118120
{
119121
$resource = new Resource($this->getFields());
120-
121122
$resource->indexes = $this->getIndexes();
122-
123123
$resource->relations = $this->getRelations();
124124

125125
return $resource;
@@ -156,7 +156,7 @@ protected function transfer(array $columns)
156156
}
157157

158158
/**
159-
* Set the html type for a giving field.
159+
* Get the html type for a giving field.
160160
*
161161
* @param CrestApps\CodeGenerator\Models\Field $field
162162
* @param string $type
@@ -171,39 +171,22 @@ protected function getHtmlType($type)
171171
return $map[$type];
172172
}
173173

174-
return 'string';
174+
return 'text';
175175
}
176176

177177
/**
178-
* Checks if the request requires languages.
179-
*
180-
* @return bool
181-
*/
182-
protected function hasLanguages()
183-
{
184-
return !empty($this->languages);
185-
}
186-
187-
/**
188-
* Gets the label(s) from a giving name
178+
* Set the html type for a giving field.
189179
*
190-
* @param string $name
180+
* @param CrestApps\CodeGenerator\Models\Field $field
181+
* @param string $type
191182
*
192-
* @return mix (string | array)
183+
* @return string
193184
*/
194-
protected function getLabel($name)
185+
protected function setHtmlType(array &$fields)
195186
{
196-
if (!$this->hasLanguages()) {
197-
return $this->getLabelName($name);
198-
}
199-
$labels = [];
200-
$title = $this->getLabelName($name);
201-
202-
foreach ($this->languages as $language) {
203-
$labels[$language] = $title;
187+
foreach ($fields as $field) {
188+
$field->htmlType = $this->getHtmlType($field->getEloquentDataMethod());
204189
}
205-
206-
return $labels;
207190
}
208191

209192
/**
@@ -225,39 +208,9 @@ protected function getModelNamespace()
225208
*/
226209
protected function getModelName($tableName)
227210
{
228-
$file = base_path(Config::getResourceFilePath(Config::getDefaultMapperFileName()));
229-
230-
$fields = [];
231-
232-
if ($this->isFileExists($file)) {
233-
$content = $this->getFileContent($file);
234-
235-
$maps = json_decode($content, true);
236-
237-
if (is_array($maps)) {
238-
foreach ($maps as $map) {
239-
if (array_key_exists('table-name', $map)
240-
&& $map['table-name'] == $tableName
241-
&& array_key_exists('model-name', $map)
242-
&& !empty($map['model-name'])
243-
) {
244-
return $map['model-name'];
245-
}
246-
}
247-
}
248-
}
211+
$modelName = ResourceMapper::pluckFirst($tableName, 'table-name', 'model-name');
249212

250-
return $tableName;
251-
}
252-
253-
/**
254-
* Gets a labe field's label from a giving name.
255-
*
256-
* @return string
257-
*/
258-
protected function getLabelName($name)
259-
{
260-
return trim(ucwords(str_replace(['-', '_'], ' ', $name)));
213+
return $modelName ?: ucfirst(camel_case(Str::singular($tableName)));
261214
}
262215

263216
/**

src/Support/ResourceMapper.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ public function __construct(Command $command)
2929
/**
3030
* Gets the first map with the giving model name
3131
*
32-
* @param string $modelName
32+
* @param string $value
33+
* @param string $key
3334
*
3435
* @return mix (null | array)
3536
*/
36-
public static function first($modelName)
37+
public static function first($value, $key = 'model-name')
3738
{
3839
$file = self::getMapper();
3940

4041
if (File::Exists($file)) {
4142

4243
$maps = self::getMaps($file);
4344

44-
return Collect($maps)->first(function ($map) use ($modelName) {
45-
return $map['model-name'] == $modelName;
45+
return Collect($maps)->first(function ($map) use ($key, $value) {
46+
return $map[$key] == $value;
4647
});
4748
}
4849

@@ -52,14 +53,15 @@ public static function first($modelName)
5253
/**
5354
* Gets the first map with the giving model name then get a specific property/key
5455
*
55-
* @param string $modelName
56+
* @param string $value
57+
* @param string $key
5658
* @param string $propertyName
5759
*
5860
* @return string
5961
*/
60-
public static function pluckFirst($modelName, $propertyName = 'resource-file')
62+
public static function pluckFirst($value, $key = 'model-name', $propertyName = 'resource-file')
6163
{
62-
$first = self::first($modelName);
64+
$first = self::first($value, $key);
6365

6466
if (!is_null($first) && isset($first[$propertyName])) {
6567
return $first[$propertyName];

0 commit comments

Comments
 (0)