Skip to content

Commit

Permalink
merge code
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Apr 22, 2018
2 parents 737f6b3 + 94b6fe4 commit 270f2e8
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 77 deletions.
16 changes: 15 additions & 1 deletion src/db/src/Command/EntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class EntityCommand
* --include 指定特定的数据表,多表之间用逗号分隔
* -e 排除指定的数据表,多表之间用逗号分隔
* --exclude 排除指定的数据表,多表之间用逗号分隔
* --remove-table-prefix 去除前缀
*
* @Example
* php bin/swoft entity:create -d test
Expand All @@ -63,19 +64,21 @@ public function create()
{
$this->initDatabase();

$database = '';
$database = $removeTablePrefix = '';
$tablesEnabled = $tablesDisabled = [];

$this->parseDatabaseCommand($database);
$this->parseEnableTablesCommand($tablesEnabled);
$this->parseDisableTablesCommand($tablesDisabled);
$this->parseRemoveTablePrefix($removeTablePrefix);

if (empty($database)) {
output()->writeln('databases doesn\'t not empty!');
} else {
$this->generatorEntity->db = $database;
$this->generatorEntity->tablesEnabled = $tablesEnabled;
$this->generatorEntity->tablesDisabled = $tablesDisabled;
$this->generatorEntity->removeTablePrefix = $removeTablePrefix;
$this->generatorEntity->execute($this->schema);
}
}
Expand Down Expand Up @@ -138,4 +141,15 @@ private function parseDisableTablesCommand(&$tablesDisabled)
$tablesDisabled = !empty($tablesDisabled) ? explode(',', $tablesDisabled) : [];
}
}

/**
* 移除表前缀
*
* @param string &$removeTablePrefix 需要移除的前缀
*/
private function parseRemoveTablePrefix(&$removeTablePrefix) {
if (input()->hasLOpt('remove-table-prefix')) {
$removeTablePrefix = (string)input()->getLongOpt('remove-table-prefix');
}
}
}
17 changes: 7 additions & 10 deletions src/db/src/Entity/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@
*/
namespace Swoft\Db\Entity;

/**
* 抽象生成实体操作类
*
* @uses AbstractGenerator
* @version 2017年11月06日
* @author caiwh <471113744@qq.com>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
use Swoft\Helper\StringHelper;

abstract class AbstractGenerator
{
/**
Expand Down Expand Up @@ -97,8 +90,12 @@ protected function parseProperty(string $entity, $entityName, array $fields, Sch
$this->entityName = $entityName;
$this->entityDate = date('Y年m月d日');
$this->fields = $fields;
$removeTablePrefix = $this->removeTablePrefix;

$this->entityClass = explode('_', $this->entity);
if (!empty($removeTablePrefix)) {
$entityClass = StringHelper::replaceFirst($removeTablePrefix, '', $this->entity);
}
$this->entityClass = explode('_', $entityClass);
$this->entityClass = array_map(function ($word) {
return ucfirst($word);
}, $this->entityClass);
Expand Down
9 changes: 0 additions & 9 deletions src/db/src/Entity/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
*/
namespace Swoft\Db\Entity;

/**
* 生成实体操作类
*
* @uses Generator
* @version 2017年11月06日
* @author caiwh <471113744@qq.com>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
class Generator extends AbstractGenerator implements GeneratorInterface
{
/**
Expand Down
9 changes: 0 additions & 9 deletions src/db/src/Entity/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
*/
namespace Swoft\Db\Entity;

/**
* 生成实体操作接口
*
* @uses GeneratorInterface
* @version 2017年11月06日
* @author caiwh <471113744@qq.com>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
interface GeneratorInterface
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/db/src/Entity/Mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Schema extends \Swoft\Db\Entity\Schema
'datetime' => 'Types::DATETIME',
'float' => 'Types::FLOAT',
'number' => 'Types::NUMBER',
'decimal' => 'Types::NUMBER',
'decimal' => 'Types::FLOAT',
'bool' => 'Types::BOOLEAN',
'tinyint' => 'Types::INT',
];
Expand All @@ -48,7 +48,7 @@ class Schema extends \Swoft\Db\Entity\Schema
'datetime' => self::TYPE_STRING,
'float' => self::TYPE_FLOAT,
'number' => self::TYPE_INT,
'decimal' => self::TYPE_INT,
'decimal' => self::TYPE_FLOAT,
'bool' => self::TYPE_BOOL,
'tinyint' => self::TYPE_INT
];
Expand Down
10 changes: 0 additions & 10 deletions src/db/src/Entity/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
*/
namespace Swoft\Db\Entity;

/**
* 数据库字段映射关系
*
* @uses Schema
* @version 2017年11月14日
* @author caiwh <471113744@qq.com>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/

abstract class Schema
{
// PHP类型
Expand Down
85 changes: 53 additions & 32 deletions src/db/src/Entity/SetGetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@
* @contact group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace Swoft\Db\Entity;

use Swoft\App;
use Swoft\Helper\StringHelper;

/**
* Stub操作类
*
* @uses SetGetGenerator
* @version 2017年11月7日
* @author caiwh <471113744@qq.com>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
class SetGetGenerator
{
/**
Expand Down Expand Up @@ -117,7 +109,7 @@ public function __invoke(
'{{entityDate}}',
'{{property}}',
'{{setter}}',
'{{getter}}'
'{{getter}}',
], [
$usesContent,
$extends,
Expand All @@ -127,10 +119,10 @@ public function __invoke(
$entityDate,
$this->propertyStub,
$this->setterStub,
$this->getterStub
$this->getterStub,
], $entityStub);

file_put_contents(App::getAlias('@entityPath') . "/{$entityClass}.php", $entityFile);
file_put_contents(alias('@entityPath') . "/{$entityClass}.php", $entityFile);
}

/**
Expand Down Expand Up @@ -160,10 +152,10 @@ private function parseFields(array $fields)
private function parseProperty(string $propertyStub, array $fieldInfo)
{
$property = $fieldInfo['name'];
$aliasProperty = $property;
$aliasProperty = StringHelper::camel($property);
$primaryKey = $fieldInfo['key'] === 'PRI';
$required = $primaryKey ? false : ($fieldInfo['nullable'] === 'NO');
$default = strtolower($fieldInfo['default']) !== 'null' ? $fieldInfo['default'] : false;
$default = $fieldInfo['default'] ?? '';
$dbType = $this->schema->dbSchema[$fieldInfo['type']] ?? '';
$phpType = $this->schema->phpSchema[$fieldInfo['type']] ?? 'mixed';
$length = $fieldInfo['length'];
Expand All @@ -177,6 +169,42 @@ private function parseProperty(string $propertyStub, array $fieldInfo)
// TODO $enumParam never use ?
}

//字段类型
$dbtype = !empty($dbType) ? $dbType : ($isEnum ? '"feature-enum"' : (\is_int($default) ? '"int"' : '"string"'));

//设置默认值
if (in_array(strtolower($default), ['\'\'', '""', 'null']) || empty($default)) {
switch ($dbtype) {
case "Types::INT":
case "Types::NUMBER":
case "Types::BOOLEAN":
$default = '0';
break;
case "Types::FLOAT":
$default = '0.0';
break;
case "Types::DATETIME":
$default = '\'' . date('Y-m-d H:i:s') . '\'';
break;
default:
$default = '\'\'';
break;
}
} else {
$default = trim($default);
switch ($dbtype) {
case "Types::INT":
case "Types::NUMBER":
case "Types::BOOLEAN":
case "Types::FLOAT":
$default = $default;
break;
default:
$default = json_encode($default);
break;
}
}

$this->checkAliasProperty($aliasProperty);

$formatComment = " * @var {$phpType} \${$aliasProperty} {$comment}\n";
Expand All @@ -189,16 +217,16 @@ private function parseProperty(string $propertyStub, array $fieldInfo)
'{{type}}',
'{{length}}',
"{{@Required}}\n",
'{{hasDefault}}'
'{{hasDefault}}',
], [
$formatComment,
$primaryKey ? " * @Id()\n" : '',
$property,
$aliasProperty,
!empty($dbType) ? $dbType : ($isEnum ? '"feature-enum"' : (\is_int($default) ? '"int"' : '"string"')),
$dbtype,
$length !== null ? ", length={$length}" : '',
$required ? " * @Required()\n" : '',
$default !== false ? (\is_int($default) ? " = {$default};" : (trim($default) === '' ? ' = \'\';' : " = '{$default}';")) : ($required ? ' = \'\';' : ';')
" = {$default};",
], $propertyStub);
}

Expand All @@ -213,13 +241,9 @@ private function parseSetter(string $setterStub, array $fieldInfo)
{
$property = $fieldInfo['name'];
$comment = $fieldInfo['column_comment'];
$aliasProperty = $property;
$aliasProperty = StringHelper::camel($property);
$this->checkAliasProperty($aliasProperty);
$function = explode('_', $aliasProperty);
$function = array_map(function ($word) {
return ucfirst($word);
}, $function);
$function = implode('', $function);
$function = StringHelper::snake($aliasProperty);
$function = 'set' . $function;
$primaryKey = $fieldInfo['key'] === 'PRI';
$type = $this->schema->phpSchema[$fieldInfo['type']] ?? 'mixed';
Expand All @@ -228,13 +252,13 @@ private function parseSetter(string $setterStub, array $fieldInfo)
'{{function}}',
'{{attribute}}',
'{{type}}',
'{{hasReturnType}}'
'{{hasReturnType}}',
], [
$comment,
$function,
$aliasProperty,
$type !== 'mixed' ? "{$type} " : '',
$primaryKey ? '' : ': self'
$primaryKey ? '' : ': self',
], $setterStub);
}

Expand All @@ -249,13 +273,9 @@ private function parseGetter(string $getterStub, array $fieldInfo)
{
$property = $fieldInfo['name'];
$comment = $fieldInfo['column_comment'];
$aliasProperty = $property;
$aliasProperty = StringHelper::camel($property);
$this->checkAliasProperty($aliasProperty);
$function = explode('_', $aliasProperty);
$function = array_map(function ($word) {
return ucfirst($word);
}, $function);
$function = implode('', $function);
$function = StringHelper::snake($aliasProperty);
$function = 'get' . $function;
$default = !empty($fieldInfo['default']) ? $fieldInfo['default'] : false;
$primaryKey = $fieldInfo['key'] === 'PRI';
Expand Down Expand Up @@ -333,4 +353,5 @@ private function generateProperty(): string
{
return file_get_contents($this->folder . $this->propertyStubFile);
}

}
12 changes: 8 additions & 4 deletions src/db/src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Swoft\Contract\Arrayable;
use Swoft\Core\ResultInterface;
use Swoft\Db\Bean\Collector\EntityCollector;
use Swoft\Helper\StringHelper;

/**
* ActiveRecord
Expand Down Expand Up @@ -297,8 +298,11 @@ public function toArray(): array
$columns = $entities[static::class]['field'];
$data = [];
foreach ($columns as $propertyName => $column) {
$methodName = sprintf('get%s', ucfirst($propertyName));
if (!isset($column['column']) || !\method_exists($this, $methodName)) {
if (!isset($column['column'])) {
continue;
}
$methodName = StringHelper::camel('get' . $propertyName);
if (!\method_exists($this, $methodName)) {
continue;
}

Expand All @@ -314,7 +318,7 @@ public function toArray(): array
*/
public function toJson(): string
{
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
return \json_encode($this->toArray(), \JSON_UNESCAPED_UNICODE);
}

/**
Expand Down Expand Up @@ -403,7 +407,7 @@ public function key()
*/
public function valid(): bool
{
return ($this->current() !== false);
return $this->current() !== false;
}

/**
Expand Down

0 comments on commit 270f2e8

Please sign in to comment.