<?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 ControllerGenerator extends MineGenerator implements CodeGenerator
{
protected $model;
protected $codeContent;
protected $filesystem;
public function setGenInfo(SettingGenerateTables $model): ControllerGenerator
{
$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}/Controller/";
} else {
$path = BASE_PATH . "/app/{$module}/Controller/";
}
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();
}
public function getType(): string
{
return ucfirst($this->model->type);
}
protected function getTemplatePath(): string
{
return $this->getStubDir().$this->getType().'/controller.stub';
}
protected function readTemplate(): string
{
return $this->filesystem->sharedGet($this->getTemplatePath());
}
protected function placeholderReplace(): ControllerGenerator
{
$this->setCodeContent(str_replace(
$this->getPlaceHolderContent(),
$this->getReplaceContent(),
$this->readTemplate()
));
return $this;
}
protected function getPlaceHolderContent(): array
{
return [
'{NAMESPACE}',
'{COMMENT}',
'{USE}',
'{CLASS_NAME}',
'{SERVICE}',
'{CONTROLLER_ROUTE}',
'{INDEX_PERMISSION}',
'{RECYCLE_PERMISSION}',
'{SAVE_PERMISSION}',
'{READ_PERMISSION}',
'{UPDATE_PERMISSION}',
'{DELETE_PERMISSION}',
'{REAL_DELETE_PERMISSION}',
'{RECOVERY_PERMISSION}',
'{CREATE_REQUEST}',
'{UPDATE_REQUEST}'
];
}
protected function getReplaceContent(): array
{
return [
$this->initNamespace(),
$this->getComment(),
$this->getUse(),
$this->getClassName(),
$this->getServiceName(),
$this->getControllerRoute(),
$this->getMethodRoute('index'),
$this->getMethodRoute('recycle'),
$this->getMethodRoute('save'),
$this->getMethodRoute('read'),
$this->getMethodRoute('update'),
$this->getMethodRoute('delete'),
$this->getMethodRoute('realDelete'),
$this->getMethodRoute('recovery'),
$this->getCreateRequestName(),
$this->getUpdateRequestName(),
];
}
protected function initNamespace(): string
{
$namespace = $this->getNamespace() . "\\Controller";
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. '控制器';
}
protected function getUse(): string
{
$namespace = "\\";
if (!empty($this->model->package_name)) {
$namespace = "\\" . Str::title($this->model->package_name). "\\";
}
return <<<UseNamespace
use {$this->getNamespace()}\\Service\\{$this->getBusinessName()}Service;
use {$this->getNamespace()}\\Request$namespace{$this->getBusinessName()}CreateRequest;
use {$this->getNamespace()}\\Request$namespace{$this->getBusinessName()}UpdateRequest;
UseNamespace;
}
protected function getClassName(): string
{
return $this->getBusinessName().'Controller';
}
protected function getServiceName(): string
{
return $this->getBusinessName().'Service';
}
protected function getControllerRoute(): string
{
return sprintf(
'%s/%s',
Str::lower($this->model->module_name),
$this->getShortBusinessName()
);
}
protected function getMethodRoute(string $route): string
{
return sprintf(
'%s:%s:%s',
Str::lower($this->model->module_name),
$this->getShortBusinessName(),
$route
);
}
protected function getCreateRequestName(): string
{
return $this->getBusinessName(). 'CreateRequest';
}
protected function getUpdateRequestName(): string
{
return $this->getBusinessName(). 'UpdateRequest';
}
public function getBusinessName(): string
{
return Str::studly(str_replace(env('DB_PREFIX'), '', $this->model->table_name));
}
public function getShortBusinessName(): string
{
return Str::camel(str_replace(
Str::lower($this->model->module_name),
'',
str_replace(env('DB_PREFIX'), '', $this->model->table_name)
));
}
public function setCodeContent(string $content)
{
$this->codeContent = $content;
}
public function getCodeContent(): string
{
return $this->codeContent;
}
}