<?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 ApiGenerator extends MineGenerator implements CodeGenerator
{
protected $model;
protected $codeContent;
protected $filesystem;
public function setGenInfo(SettingGenerateTables $model): ApiGenerator
{
$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'));
}
return $this;
}
public function generator(): void
{
$filename = Str::camel(str_replace(env('DB_PREFIX'), '', $this->model->table_name));
$module = Str::lower($this->model->module_name);
$this->filesystem->makeDirectory(BASE_PATH . "/runtime/generate/vue/src/api/apis/{$module}", 0755, true, true);
$path = BASE_PATH . "/runtime/generate/vue/src/api/apis/{$module}/{$filename}.js";
$this->filesystem->put($path, $this->placeholderReplace()->getCodeContent());
}
public function preview(): string
{
return $this->placeholderReplace()->getCodeContent();
}
protected function getTemplatePath(): string
{
return $this->getStubDir().'/api.stub';
}
protected function readTemplate(): string
{
return $this->filesystem->sharedGet($this->getTemplatePath());
}
protected function placeholderReplace(): ApiGenerator
{
$this->setCodeContent(str_replace(
$this->getPlaceHolderContent(),
$this->getReplaceContent(),
$this->readTemplate()
));
return $this;
}
protected function getPlaceHolderContent(): array
{
return [
'{COMMENT}',
'{BUSINESS_NAME}',
'{REQUEST_ROUTE}',
];
}
protected function getReplaceContent(): array
{
return [
$this->getComment(),
$this->getBusinessName(),
$this->getRequestRoute(),
];
}
protected function getComment(): string
{
return $this->getBusinessName(). ' API JS';
}
protected function getRequestRoute(): string
{
return Str::lower($this->model->module_name) . '/' . $this->getShortBusinessName();
}
protected function getBusinessName(): string
{
return $this->model->menu_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;
}
}