<?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\SettingGenerateTables;
use Hyperf\Utils\Filesystem\Filesystem;
use Mine\Exception\NormalStatusException;
use Mine\Helper\Str;
class ServiceGenerator extends MineGenerator implements CodeGenerator
{
protected $model;
protected $codeContent;
protected $filesystem;
public function setGenInfo(SettingGenerateTables $model): ServiceGenerator
{
$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}/Service/";
} else {
$path = BASE_PATH . "/app/{$module}/Service/";
}
$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().'/service.stub';
}
protected function readTemplate(): string
{
return $this->filesystem->sharedGet($this->getTemplatePath());
}
protected function placeholderReplace(): ServiceGenerator
{
$this->setCodeContent(str_replace(
$this->getPlaceHolderContent(),
$this->getReplaceContent(),
$this->readTemplate()
));
return $this;
}
protected function getPlaceHolderContent(): array
{
return [
'{NAMESPACE}',
'{USE}',
'{COMMENT}',
'{CLASS_NAME}',
'{MAPPER}',
'{FIELD_ID}',
'{FIELD_PID}'
];
}
protected function getReplaceContent(): array
{
return [
$this->initNamespace(),
$this->getUse(),
$this->getComment(),
$this->getClassName(),
$this->getMapperName(),
$this->getFieldIdName(),
$this->getFieldPidName(),
];
}
protected function initNamespace(): string
{
return $this->getNamespace() . "\\Service";
}
protected function getComment(): string
{
return $this->model->menu_name. '服务类';
}
protected function getUse(): string
{
return <<<UseNamespace
use {$this->getNamespace()}\\Mapper\\{$this->getBusinessName()}Mapper;
UseNamespace;
}
protected function getClassName(): string
{
return $this->getBusinessName().'Service';
}
protected function getMapperName(): string
{
return $this->getBusinessName().'Mapper';
}
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 '';
}
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;
}
}