<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare(strict_types=1);
namespace Mine\Generator;
use App\Setting\Model\SettingGenerateColumns;
use App\Setting\Model\SettingGenerateTables;
use App\Setting\Service\SettingGenerateColumnsService;
use Hyperf\Utils\Filesystem\Filesystem;
use Mine\Exception\NormalStatusException;
use Mine\Helper\Str;
class RequestGenerator extends MineGenerator implements CodeGenerator
{
protected $model;
protected $codeContent;
protected $filesystem;
protected $type;
public function setGenInfo(SettingGenerateTables $model, string $type): RequestGenerator
{
$this->model = $model;
$this->type = $type;
$this->filesystem = make(Filesystem::class);
if (empty($model->module_name) || empty($model->menu_name)) {
throw new NormalStatusException(t('setting.gen_code_edit'));
}
$this->setNamespace($this->model->namespace);
return $this;
}
public function generator(): void
{
$module = Str::title($this->model->module_name);
if ($this->model->generate_type == '0') {
$path = BASE_PATH . "/runtime/generate/php/app/{$module}/Request/";
} else {
$path = BASE_PATH . "/app/{$module}/Request/";
}
if (!empty($this->model->package_name)) {
$path .= Str::title($this->model->package_name) . '/';
}
$this->filesystem->makeDirectory($path, 0755, true, false);
$this->filesystem->put($path . "{$this->getClassName()}.php", $this->placeholderReplace()->getCodeContent());
}
public function preview(): string
{
return $this->placeholderReplace()->getCodeContent();
}
protected function getTemplatePath(): string
{
return $this->getStubDir().'/request.stub';
}
protected function readTemplate(): string
{
return $this->filesystem->sharedGet($this->getTemplatePath());
}
protected function placeholderReplace(): RequestGenerator
{
$this->setCodeContent(str_replace(
$this->getPlaceHolderContent(),
$this->getReplaceContent(),
$this->readTemplate()
));
return $this;
}
protected function getPlaceHolderContent(): array
{
return [
'{NAMESPACE}',
'{COMMENT}',
'{CLASS_NAME}',
'{RULES}'
];
}
protected function getReplaceContent(): array
{
return [
$this->initNamespace(),
$this->getComment(),
$this->getClassName(),
$this->getRules(),
];
}
protected function initNamespace(): string
{
$namespace = $this->getNamespace() . "\\Request";
if (!empty($this->model->package_name)) {
return $namespace . "\\" . Str::title($this->model->package_name);
}
return $namespace;
}
protected function getComment(): string
{
return $this->model->menu_name. '验证数据类 ('.$this->type.')';
}
protected function getClassName(): string
{
return $this->getBusinessName().$this->type.'Request';
}
protected function getRules(): string
{
$model = make(SettingGenerateColumnsService::class)->mapper->getModel();
$query = $model->newQuery()->where('table_id', $this->model->id);
if ($this->type == 'Create') {
$query = $query->where('is_insert', 1)->where('is_required', 1);
} else {
$query = $query->where('is_edit', 1)->where('is_required', 1);
}
$columns = $query->get(['column_name', 'column_comment', 'query_type'])->toArray();
$phpContent = '';
foreach ($columns as $column) {
$phpContent .= $this->getRuleCode($column);
}
return $phpContent;
}
protected function getRuleCode(array $column): string
{
return <<<php
'{$column['column_name']}' => 'required',
php;
}
public function getBusinessName(): string
{
return Str::studly(str_replace(env('DB_PREFIX'), '', $this->model->table_name));
}
public function setCodeContent(string $content)
{
$this->codeContent = $content;
}
public function getCodeContent(): string
{
return $this->codeContent;
}
}