Skip to content

Commit

Permalink
Merge pull request #11 from sakuraovq/master
Browse files Browse the repository at this point in the history
add entity gen
  • Loading branch information
stelin authored Jun 9, 2019
2 parents 1838552 + 6c76224 commit 14b4d39
Show file tree
Hide file tree
Showing 20 changed files with 417 additions and 295 deletions.
20 changes: 12 additions & 8 deletions resource/template/entity.stub
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<?php declare(strict_types=1);


namespace {= namespace};

use Swoft\Db\Model;
use Swoft\Db\Bean\Annotation\Column;
use Swoft\Db\Bean\Annotation\Entity;
use Swoft\Db\Bean\Annotation\Id;
use Swoft\Db\Bean\Annotation\Table;
use Swoft\Db\Annotation\Mapping\Column;
use Swoft\Db\Annotation\Mapping\Entity;
use Swoft\Db\Annotation\Mapping\Id;
use Swoft\Db\Eloquent\Model;
{= usespace}

/**
* @Entity()
* @Table(name="{= tableName}")
* {= tableComment}
* Class {= entityName}
*
* @since 2.0
*
* @Entity(table="{= tableName}"{= dbPool | raw})
*/
class {= entityName} extends Model
{
{= properties | raw }

{= methods | raw }

}
}
2 changes: 1 addition & 1 deletion resource/template/getter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
public function {= methodName}()
{
return $this->{= property};
}
}
8 changes: 4 additions & 4 deletions resource/template/property.stub
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @var {= type} {= propertyName}
* {= comment | raw}
{= id}
* @Column(name="{= column}", type="{= columnType}"{= default | raw })
{= required}
* @Column({= columnDetail | raw})
* @var {= type}
*/
private {= propertyName};
private {= propertyName};
4 changes: 2 additions & 2 deletions resource/template/setter.stub
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @param {= type} {= paramName}
* @param {= paramType} {= paramName}
* @return self
*/
public function {= methodName}({= type} {= paramName}): self
{
$this->{= property} = {= paramName};

return $this;
}
}
7 changes: 6 additions & 1 deletion src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function __construct()
\Swoft::setAlias('@devtool', \dirname(__DIR__, 2));
}

/**
*
*
* @return bool
*/
public function enable(): bool
{
return (int)\env('SWOFT_DEBUG', 0) > 0;
Expand All @@ -46,7 +51,7 @@ public function beans(): array
public function getPrefixDirs(): array
{
return [
__NAMESPACE__ => __DIR__
__NAMESPACE__ => __DIR__,
];
}

Expand Down
8 changes: 3 additions & 5 deletions src/Command/AppCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Swoft\Devtool\Command;

use function array_merge;
use InvalidArgumentException;
use RuntimeException;
use Swoft;
use Swoft\Bean\BeanFactory;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Console\Annotation\Mapping\CommandOption;
Expand All @@ -16,8 +14,8 @@
use Swoft\Stdlib\Helper\DirHelper;
use Swoft\Stdlib\Helper\Sys;
use Swoole\Coroutine;
use function array_merge;
use function class_exists;
use function config;
use function extension_loaded;
use function implode;
use function input;
Expand All @@ -27,8 +25,6 @@
use function str_pad;
use function strpos;
use function version_compare;
use const BASE_PATH;
use const PHP_OS;
use const PHP_VERSION;
use const PHP_VERSION_ID;
use const SWOOLE_VERSION;
Expand Down Expand Up @@ -147,6 +143,8 @@ public function check(Output $output): void
* @param string|null $msg
* @param bool $showOnFalse
*
* @param array $opts
*
* @return array
*/
public static function wrap($condition, string $msg = null, $showOnFalse = false, array $opts = []): array
Expand Down
58 changes: 56 additions & 2 deletions src/Command/EntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,66 @@

namespace Swoft\Devtool\Command;

use Leuffen\TextTemplate\TemplateParsingException;
use ReflectionException;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandArgument;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Db\Exception\DbException;
use Swoft\Db\Pool;
use Swoft\Devtool\Model\Logic\EntityLogic;
use function alias;
use function input;

/**
* Class EntityCommand
* Class entityCommand generate entity
*
* @Command(coroutine=false)
*
* @since 2.0
*/
class EntityCommand
{

}
/**
* @Inject()
*
* @var EntityLogic
*/
private $logic;

/**
* Generate database entity
*
* @CommandMapping(alias="c,gen")
* @CommandArgument(name="table", desc="table names", type="string")
* @throws TemplateParsingException
* @throws ReflectionException
* @throws ContainerException
* @throws DbException
*/
public function create(): void
{
$table = input()->get('table', input()->getOpt('table'));
$pool = input()->getOpt('pool', Pool::DEFAULT_POOL);
$path = input()->getOpt('path', '@app/Model/Entity');
$isConfirm = input()->getOpt('y', false);
$fieldPrefix = input()->getOpt('field_prefix', input()->getOpt('fp'));
$tablePrefix = input()->getOpt('table_prefix', input()->getOpt('tp'));
$exclude = input()->getOpt('exc', input()->getOpt('exclude'));
$tplDir = input()->getOpt('tr', alias('@devtool/devtool/resource/template'));

$this->logic->create([
(string)$table,
(string)$tablePrefix,
(string)$fieldPrefix,
(string)$exclude,
(string)$pool,
(string)$path,
(bool)$isConfirm,
(string)$tplDir
]);
}
}
9 changes: 7 additions & 2 deletions src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace Swoft\Devtool\Command;

use InvalidArgumentException;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandArgument;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Console\Annotation\Mapping\CommandOption;
use Swoft\Devtool\Model\Logic\MigrateLogic;
use Leuffen\TextTemplate\TemplateParsingException;
use ReflectionException;
use Swoft\Bean\Exception\ContainerException;

/**
* Class MigrateCommand
Expand Down Expand Up @@ -37,6 +39,9 @@ class MigrateCommand
*
* @CommandMapping()
* @CommandArgument(name="name", desc="the name of the new migration", type="string", mode=Command::OPT_REQUIRED)
* @throws ContainerException
* @throws ReflectionException
* @throws TemplateParsingException
*/
public function create(): void
{
Expand Down Expand Up @@ -92,4 +97,4 @@ public function to(): void
{

}
}
}
54 changes: 54 additions & 0 deletions src/Helper/ConsoleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);


namespace Swoft\Devtool\Helper;


/**
* Class ConsoleHelper
*
* @since 2.0
*/
class ConsoleHelper
{


/**
* Send confirm question
*
* @param string $question question
* @param bool $default Default value
*
* @return bool
*/
public static function confirm(string $question, $default = true): bool
{
if (!$question = trim($question)) {
\output()->writeln('Please provide a question message!', true);
}

$question = ucfirst(trim($question, '?'));
$default = (bool)$default;
$defaultText = $default ? 'yes' : 'no';
$message = "<comment>$question ?</comment>\nPlease confirm (yes|no)[default:<info>$defaultText</info>]: ";

while (true) {
\output()->writeln($message, false);
$answer = \input()->read();

if (empty($answer)) {
return $default;
}

if (0 === stripos($answer, 'y')) {
return true;
}

if (0 === stripos($answer, 'n')) {
return false;
}
}

return false;
}
}
1 change: 0 additions & 1 deletion src/Http/Controller/HttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Swoft\Devtool\Http\Controller;

use Swoft\Bean\BeanFactory;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Controller/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Swoft\Devtool\Http\Controller;

use ReflectionException;
use Swoft\Bean\BeanFactory;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
Expand Down Expand Up @@ -57,6 +59,8 @@ public function wsRoutes(): array
/**
* @RequestMapping("rpc/routes", method=RequestMethod::GET)
* @return array
* @throws ReflectionException
* @throws ContainerException
*/
public function rpcRoutes(): array
{
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Controller/RpcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Swoft\Devtool\Http\Controller;

use ReflectionException;
use Swoft\Bean\BeanFactory;
use Swoft\Http\Message\Request;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
Expand All @@ -18,6 +19,8 @@ class RpcController
/**
* @RequestMapping("rpc/routes", method=RequestMethod::GET)
* @return array
* @throws ReflectionException
* @throws ContainerException
*/
public function rpcRoutes(): array
{
Expand Down
2 changes: 0 additions & 2 deletions src/Listener/WorkStartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Swoft\Devtool\Bootstrap\Listener;

use Swoft\Config\Annotation\Mapping\Config;
use Swoft\Devtool\DevTool;
use Swoft\Devtool\WebSocket\DevToolController;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
Expand Down
Loading

0 comments on commit 14b4d39

Please sign in to comment.