<?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\Generator\Traits\MapperGeneratorTraits;
use Mine\Helper\Str;
class MapperGenerator extends MineGenerator implements CodeGenerator
{
use MapperGeneratorTraits;
protected $model;
protected $codeContent;
protected $filesystem;
public function setGenInfo(SettingGenerateTables $model): MapperGenerator
{
$this->model = $model;
$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}/Mapper/";
} else {
$path = BASE_PATH . "/app/{$module}/Mapper/";
}
$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();
}
public function getType(): string
{
return ucfirst($this->model->type);
}
protected function getTemplatePath(): string
{
return $this->getStubDir().$this->getType().'/mapper.stub';
}
protected function readTemplate(): string
{
return $this->filesystem->sharedGet($this->getTemplatePath());
}
protected function placeholderReplace(): MapperGenerator
{
$this->setCodeContent(str_replace(
$this->getPlaceHolderContent(),
$this->getReplaceContent(),
$this->readTemplate()
));
return $this;
}
protected function getPlaceHolderContent(): array
{
return [
'{NAMESPACE}',
'{USE}',
'{COMMENT}',
'{CLASS_NAME}',
'{MODEL}',
'{FIELD_ID}',
'{FIELD_PID}',
'{FIELD_NAME}',
'{SEARCH}'
];
}
protected function getReplaceContent(): array
{
return [
$this->initNamespace(),
$this->getUse(),
$this->getComment(),
$this->getClassName(),
$this->getModelName(),
$this->getFieldIdName(),
$this->getFieldPidName(),
$this->getFieldName(),
$this->getSearch()
];
}
protected function initNamespace(): string
{
return $this->getNamespace() . "\\Mapper";
}
protected function getComment(): string
{
return $this->model->menu_name. 'Mapper类';
}
protected function getUse(): string
{
return <<<UseNamespace
use {$this->getNamespace()}\\Model\\{$this->getBusinessName()};
UseNamespace;
}
protected function getClassName(): string
{
return $this->getBusinessName().'Mapper';
}
protected function getModelName(): string
{
return $this->getBusinessName();
}
protected function getFieldIdName(): string
{
if ($this->getType() == 'Tree') {
if ( $this->model->options['tree_id'] ?? false ) {
return $this->model->options['tree_id'];
} else {
return 'id';
}
}
return '';
}
protected function getFieldPidName(): string
{
if ($this->getType() == 'Tree') {
if ( $this->model->options['tree_pid'] ?? false ) {
return $this->model->options['tree_pid'];
} else {
return 'parent_id';
}
}
return '';
}
protected function getFieldName(): string
{
if ($this->getType() == 'Tree') {
if ( $this->model->options['tree_name'] ?? false ) {
return $this->model->options['tree_name'];
} else {
return 'name';
}
}
return '';
}
protected function getSearch(): string
{
$model = make(SettingGenerateColumnsService::class)->mapper->getModel();
$columns = $model->newQuery()
->where('table_id', $this->model->id)
->where('is_query', '1')
->get(['column_name', 'column_comment', 'query_type'])->toArray();
$phpContent = '';
foreach ($columns as $column) {
$phpContent .= $this->getSearchCode($column);
}
return $phpContent;
}
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;
}
}