Skip to content

Commit 084afb6

Browse files
authored
fix path alias problem (#12)
fix path alias problem
2 parents 88cd44b + af19e6c commit 084afb6

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

resource/template/getter.stub

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @return {= returnType}
33
*/
4-
public function {= methodName}()
4+
public function {= methodName}(): {= type | raw}
5+
{= nil}
56
{
67
return $this->{= property};
78
}

src/Command/EntityCommand.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
use Swoft\Console\Annotation\Mapping\Command;
1111
use Swoft\Console\Annotation\Mapping\CommandArgument;
1212
use Swoft\Console\Annotation\Mapping\CommandMapping;
13+
use Swoft\Console\Annotation\Mapping\CommandOption;
1314
use Swoft\Db\Exception\DbException;
1415
use Swoft\Db\Pool;
1516
use Swoft\Devtool\Model\Logic\EntityLogic;
16-
use function alias;
1717
use function input;
1818

1919
/**
2020
* Class entityCommand generate entity
2121
*
22-
* @Command(coroutine=false)
22+
* @Command()
2323
*
2424
* @since 2.0
2525
*/
@@ -37,7 +37,16 @@ class EntityCommand
3737
* Generate database entity
3838
*
3939
* @CommandMapping(alias="c,gen")
40-
* @CommandArgument(name="table", desc="table names", type="string")
40+
* @CommandArgument(name="table", desc="database table names", type="string")
41+
* @CommandOption(name="table", desc="database table names", type="string")
42+
* @CommandOption(name="pool", desc="database db pool default is 'db.pool'", type="string")
43+
* @CommandOption(name="path", desc="generate entity file path", type="string", default="@app/Model/Entity")
44+
* @CommandOption(name="y", desc="generating entity file is confirm ", type="string")
45+
* @CommandOption(name="field_prefix", desc="database field prefix ,alias is 'fp'", type="string")
46+
* @CommandOption(name="table_prefix", desc="like match database table prefix ,alias is 'tp'", type="string")
47+
* @CommandOption(name="exclude", desc="expect generate database table entity ,alias is 'exc'", type="string")
48+
* @CommandOption(name="td", desc="generate entity template path",type="string", default="@devtool/devtool/resource/template")
49+
*
4150
* @throws TemplateParsingException
4251
* @throws ReflectionException
4352
* @throws ContainerException
@@ -52,7 +61,7 @@ public function create(): void
5261
$fieldPrefix = input()->getOpt('field_prefix', input()->getOpt('fp'));
5362
$tablePrefix = input()->getOpt('table_prefix', input()->getOpt('tp'));
5463
$exclude = input()->getOpt('exc', input()->getOpt('exclude'));
55-
$tplDir = input()->getOpt('tr', alias('@devtool/devtool/resource/template'));
64+
$tplDir = input()->getOpt('td', '@devtool/devtool/resource/template');
5665

5766
$this->logic->create([
5867
(string)$table,

src/Model/Logic/EntityLogic.php

+31-18
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
use function output;
2121
use function rtrim;
2222
use function sprintf;
23+
use function str_replace;
2324
use function strpos;
2425
use function ucfirst;
25-
use function str_replace;
2626

2727
/**
2828
* EntityLogic
@@ -84,14 +84,16 @@ private function generateEntity(
8484
string $fieldPrefix,
8585
string $tplDir
8686
): void {
87+
$file = alias($path);
88+
$tplDir = alias($tplDir);
89+
8790
$mappingClass = $tableSchema['mapping'];
8891
$config = [
8992
'tplFilename' => 'entity',
9093
'tplDir' => $tplDir,
9194
'className' => $mappingClass,
9295
];
9396

94-
$file = alias($path);
9597
if (!is_dir($file)) {
9698
if (!$isConfirm && !ConsoleHelper::confirm("mkdir path $file, Ensure continue?", true)) {
9799
output()->writeln(' Quit, Bye!');
@@ -107,8 +109,7 @@ private function generateEntity(
107109
$genGetters = [];
108110
$genProperties = [];
109111
foreach ($columnSchemas as $columnSchema) {
110-
$propertyCode = $this->generateProperties($columnSchema, $tplDir);
111-
$genProperties[] = $propertyCode;
112+
$genProperties[] = $this->generateProperties($columnSchema, $tplDir);
112113

113114
$genSetters[] = $this->generateSetters($columnSchema, $tplDir);
114115
$genGetters[] = $this->generateGetters($columnSchema, $tplDir);
@@ -126,14 +127,15 @@ private function generateEntity(
126127
'entityName' => $mappingClass,
127128
'namespace' => $this->getNameSpace($path),
128129
'tableComment' => $tableSchema['comment'],
129-
'dbPool' => $pool == Pool::DEFAULT_POOL ? '' : ',pool="' . $pool . '"',
130+
'dbPool' => $pool == Pool::DEFAULT_POOL ? '' : ', pool="' . $pool . '"',
130131
];
131132
$gen = new FileGenerator($config);
132133

133134
if (!$isConfirm && !ConsoleHelper::confirm("generate entity $file, Ensure continue?", true)) {
134135
output()->writeln(' Quit, Bye!');
135136
return;
136137
}
138+
137139
if ($gen->renderas($file, $data)) {
138140
output()->colored(" Generate entity $file OK!", 'success');
139141
return;
@@ -169,33 +171,43 @@ private function generateProperties(array $colSchema, string $tplDir): string
169171
'tplFilename' => 'property',
170172
'tplDir' => $tplDir,
171173
];
174+
172175
// id
173176
$id = '*';
174177
if (!empty($colSchema['key']) && !$this->readyGenerateId) {
175178
// Is auto increment
176179
$auto = $colSchema['extra'] && strpos($colSchema['extra'], 'auto_increment') !== false ?
177180
'' :
178181
'incrementing=false';
182+
179183
// builder @id
180184
$id = "* @Id($auto)";
181185
$this->readyGenerateId = true;
182186
}
183-
// Is need map
184-
$prop = $colSchema['mappingName'] == $colSchema['name'] ? '' :
185-
sprintf('prop="%s"', $colSchema['mappingName']);
187+
188+
$mappingName = $colSchema['mappingName'];
189+
$fieldName = $colSchema['name'];
190+
191+
// is need map
192+
$prop = $mappingName == $fieldName ? '' :
193+
sprintf('prop="%s"', $mappingName);
194+
186195
// column name
187-
$columnName = $colSchema['name'] == $colSchema['mappingName'] ? '' :
188-
sprintf('name="%s"', $colSchema['name']);
196+
$columnName = $mappingName == $fieldName ? '' :
197+
sprintf('name="%s"', $fieldName);
198+
189199
// is need hidden
190-
$hidden = in_array($colSchema['mappingName'], ['password', 'pwd']) ? "hidden=true" : '';
200+
$hidden = in_array($mappingName, ['password', 'pwd']) ? "hidden=true" : '';
201+
191202
$columnDetail = array_filter([$columnName, $prop, $hidden]);
192203
$data = [
193204
'type' => $colSchema['phpType'],
194-
'propertyName' => sprintf('$%s', $colSchema['mappingName']),
195-
'columnDetail' => $columnDetail ? implode(',', $columnDetail) : '',
205+
'propertyName' => sprintf('$%s', $mappingName),
206+
'columnDetail' => $columnDetail ? implode(', ', $columnDetail) : '',
196207
'id' => $id,
197208
'comment' => $colSchema['columnComment'],
198209
];
210+
199211
$gen = new FileGenerator($entityConfig);
200212
$propertyCode = $gen->render($data);
201213

@@ -212,17 +224,18 @@ private function generateProperties(array $colSchema, string $tplDir): string
212224
private function generateGetters(array $colSchema, string $tplDir): string
213225
{
214226
$getterName = sprintf('get%s', ucfirst($colSchema['mappingName']));
215-
216-
$config = [
227+
$type = $colSchema['is_nullable'] ? '?' . $colSchema['originPHPType'] : $colSchema['originPHPType'];
228+
$config = [
217229
'tplFilename' => 'getter',
218230
'tplDir' => $tplDir,
219231
];
220-
$data = [
232+
$data = [
233+
'type' => $type,
221234
'returnType' => $colSchema['phpType'],
222235
'methodName' => $getterName,
223236
'property' => $colSchema['mappingName'],
224237
];
225-
$gen = new FileGenerator($config);
238+
$gen = new FileGenerator($config);
226239

227240
return (string)$gen->render($data);
228241
}
@@ -248,7 +261,7 @@ private function generateSetters(array $colSchema, string $tplDir): string
248261
'type' => $type,
249262
'paramType' => $colSchema['phpType'],
250263
'methodName' => $setterName,
251-
'paramName' => '$' . $colSchema['mappingName'],
264+
'paramName' => sprintf('$%s', $colSchema['mappingName']),
252265
'property' => $colSchema['mappingName'],
253266
];
254267
$gen = new FileGenerator($config);

0 commit comments

Comments
 (0)