Skip to content

Commit

Permalink
db:Specify the entity generation path
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteCcinn committed Apr 25, 2018
1 parent 764b922 commit 45e1f37
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
38 changes: 31 additions & 7 deletions src/Command/EntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class EntityCommand
private $generatorEntity;

/**
* @var string $filePath 实体文件路径
* @var string $entityFilePath 实体文件路径
*/
private $filePath = '@app/Models/Entity';
private $entityFilePath = '@app/Models/Entity';

/**
* Auto create entity by table structure
Expand Down Expand Up @@ -67,6 +67,7 @@ public function create()
$database = $removeTablePrefix = '';
$tablesEnabled = $tablesDisabled = [];

$this->parseEntityFilePath();
$this->parseDatabaseCommand($database);
$this->parseEnableTablesCommand($tablesEnabled);
$this->parseDisableTablesCommand($tablesDisabled);
Expand All @@ -88,7 +89,6 @@ public function create()
*/
private function initDatabase(): bool
{
App::setAlias('@entityPath', $this->filePath);
$pool = App::getBean(DbPool::class);
$schema = new Schema();
$schema->setDriver('MYSQL');
Expand All @@ -99,12 +99,20 @@ private function initDatabase(): bool
return true;
}

/**
* 设置实体生成路径
*/
private function setEntityFilePath(): void
{
App::setAlias('@entityPath', $this->entityFilePath);
}

/**
* 解析需要扫描的数据库
*
* @param string &$database 需要扫描的数据库
*/
private function parseDatabaseCommand(string &$database)
private function parseDatabaseCommand(string &$database): void
{
if (input()->hasSOpt('d') || input()->hasLOpt('database')) {
$database = (string)\input()->getSameOpt(['d','database']);
Expand All @@ -116,7 +124,7 @@ private function parseDatabaseCommand(string &$database)
*
* @param array &$tablesEnabled 需要扫描的表
*/
private function parseEnableTablesCommand(&$tablesEnabled)
private function parseEnableTablesCommand(&$tablesEnabled): void
{
if (input()->hasSOpt('i') || input()->hasLOpt('include')) {
$tablesEnabled = input()->hasSOpt('i') ? input()->getShortOpt('i') : input()->getLongOpt('include');
Expand All @@ -134,7 +142,7 @@ private function parseEnableTablesCommand(&$tablesEnabled)
*
* @param array &$tablesDisabled 不需要扫描的表
*/
private function parseDisableTablesCommand(&$tablesDisabled)
private function parseDisableTablesCommand(&$tablesDisabled): void
{
if (input()->hasSOpt('e') || input()->hasLOpt('exclude')) {
$tablesDisabled = input()->hasSOpt('e') ? input()->getShortOpt('e') : input()->getLongOpt('exclude');
Expand All @@ -147,9 +155,25 @@ private function parseDisableTablesCommand(&$tablesDisabled)
*
* @param string &$removeTablePrefix 需要移除的前缀
*/
private function parseRemoveTablePrefix(&$removeTablePrefix) {
private function parseRemoveTablePrefix(&$removeTablePrefix): void
{
if (input()->hasLOpt('remove-table-prefix')) {
$removeTablePrefix = (string)input()->getLongOpt('remove-table-prefix');
}
}

/**
* 实体生成路径
*/
private function parseEntityFilePath(): void
{
if (input()->hasLOpt('entity-file-path')) {
$entityFilePath = (string)input()->getLongOpt('entity-file-path');
if (preg_match('/^@app(.*)/', $entityFilePath) && is_dir(App::getAlias($entityFilePath))) {
$this->entityFilePath = $entityFilePath;
}
}

$this->setEntityFilePath();
}
}
8 changes: 3 additions & 5 deletions src/Entity/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Swoft\Db\Entity;

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

abstract class AbstractGenerator
Expand Down Expand Up @@ -96,11 +97,8 @@ protected function parseProperty(string $entity, $entityName, array $fields, Sch
if (!empty($removeTablePrefix)) {
$entityClass = StringHelper::replaceFirst($removeTablePrefix, '', $this->entity);
}
$this->entityClass = explode('_', $entityClass);
$this->entityClass = array_map(function ($word) {
return ucfirst($word);
}, $this->entityClass);
$this->entityClass = implode('', $this->entityClass);
$this->entityClass = StringHelper::camel($entityClass);
$this->entityClass = ucfirst($this->entityClass);

$param = [
$schema,
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/SetGetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ public function __invoke(
}

$this->parseFields($fields);
$namespace = preg_replace('/.+a(pp)(.*)(\/?)$/', 'A${1}${2}', alias('@entityPath'));
$namespace = str_replace('/', '\\', $namespace);

$entityFile = str_replace([
'{{namespace}}',
'{{uses}}',
'{{extends}}',
'{{entity}}',
Expand All @@ -111,6 +114,7 @@ public function __invoke(
'{{setter}}',
'{{getter}}',
], [
$namespace,
$usesContent,
$extends,
$entity,
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Stub/Model.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Models\Entity;
namespace {{namespace}};

{{uses}}
/**
Expand Down

0 comments on commit 45e1f37

Please sign in to comment.