开发插件的时候想用指令生成模型,发现webman/console 没有实现这个
基于上述插件,浅浅的实现了一下 代码如下
<?php
namespace app\command;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Webman\Console\Util;
class MakePluginModel extends Command
{
protected static $defaultName = 'make:pluginModel';
protected static $defaultDescription = 'make pluginModel 生成插件的模型';
/**
* @return void
*/
protected function configure()
{
$this->addArgument('plugin', InputArgument::REQUIRED, 'plugin name');
$this->addArgument('model', InputArgument::REQUIRED, 'model name');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$plugin = $input->getArgument('plugin');
$model = $input->getArgument('model');
$model = ucfirst($model);
$model_str = Util::guessPath(base_path('/plugin/app/' . $plugin), 'model') ?: 'model';
$file = base_path("/plugin/$plugin/app") . "/$model_str/$model.php";
$namespace = $model_str === 'Model' ? "Plugin\\" . $plugin . "\App\Model" : 'plugin\\' . $plugin . '\app\model';
$this->createModel($model, $namespace, $file, $plugin);
$output->writeln("Make pluginModel $plugin $model");
return self::SUCCESS;
}
protected function createModel($class, $namespace, $file, $plugin)
{
$path = pathinfo($file, PATHINFO_DIRNAME);
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$table = Util::classToName($class);
$table_val = 'null';
$pk = 'id';
$properties = '';
try {
$prefix = config('plugin.' . $plugin . '.database.connections.mysql.prefix') ?? '';
$database = config('plugin.' . $plugin . '.database.connections.mysql.database');
$inflector = InflectorFactory::create()->build();
$table_plura = $inflector->pluralize($inflector->tableize($class));
if (\support\Db::select("show tables like '{$prefix}{$table_plura}'")) {
$table = "{$prefix}{$table_plura}";
} else if (\support\Db::select("show tables like '{$prefix}{$table}'")) {
$table_val = "'$table'";
$table = "{$prefix}{$table}";
}
foreach (\support\Db::select("select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '$table' and table_schema = '$database'") as $item) {
if ($item->COLUMN_KEY === 'PRI') {
$pk = $item->COLUMN_NAME;
$item->COLUMN_COMMENT .= "(主键)";
}
$type = $this->getType($item->DATA_TYPE);
$properties .= " * @property $type \${$item->COLUMN_NAME} {$item->COLUMN_COMMENT}\n";
}
} catch (\Throwable $e) {
}
$properties = rtrim($properties) ?: ' *';
$model_content = <<<EOF
<?php
namespace $namespace;
use support\Model;
/**
$properties
*/
class $class extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected \$table = $table_val;
/**
* The primary key associated with the table.
*
* @var string
*/
protected \$primaryKey = '$pk';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public \$timestamps = false;
}
EOF;
file_put_contents($file, $model_content);
}
protected function getType(string $type)
{
if (str_contains($type, 'int')) {
return 'integer';
}
return match ($type) {
'varchar', 'string', 'text', 'date', 'time', 'guid', 'datetimetz', 'datetime', 'decimal', 'enum' => 'string',
'boolean' => 'integer',
'float' => 'float',
default => 'mixed',
};
}
}
运行方式
php webman make:pluginModel ershou product // ershou 为插件名字 product 为模型名字
没有生成字段注释 protected $table = null; 表名变null