Files
kiri-core/Gii/GiiBase.php
T

236 lines
4.6 KiB
PHP
Raw Normal View History

2020-09-07 17:12:46 +08:00
<?php
2020-10-30 01:07:28 +08:00
declare(strict_types=1);
2020-09-07 17:12:46 +08:00
2020-09-08 15:59:15 +08:00
namespace Gii;
2020-09-07 17:12:46 +08:00
use Database\Connection;
2020-12-17 14:09:14 +08:00
use JetBrains\PhpStorm\ArrayShape;
2020-12-17 14:12:44 +08:00
2020-12-17 14:09:14 +08:00
use ReflectionClass;
2020-09-08 15:59:15 +08:00
use Snowflake\Abstracts\Input;
2020-09-07 17:12:46 +08:00
/**
* Class GiiBase
2020-09-08 15:59:15 +08:00
* @package Gii
2020-09-07 17:12:46 +08:00
*/
abstract class GiiBase
{
2020-12-17 14:09:14 +08:00
public array $fileList = [];
2020-09-07 17:12:46 +08:00
2020-09-08 15:59:15 +08:00
/** @var Input */
2020-12-17 14:09:14 +08:00
protected Input $input;
2020-09-08 15:59:15 +08:00
2020-12-17 14:09:14 +08:00
public string $modelPath = APP_PATH . '/app/Models/';
public string $modelNamespace = 'App\Models\\';
2020-09-07 17:12:46 +08:00
2020-12-17 14:09:14 +08:00
public string $controllerPath = APP_PATH . '/app/Http/Controllers/';
public string $controllerNamespace = 'App\\Http\\Controllers\\';
2020-09-07 17:12:46 +08:00
2020-12-17 14:09:14 +08:00
public ?string $module = null;
2020-09-07 17:12:46 +08:00
2020-12-17 14:09:14 +08:00
public array $rules = [];
public array $type = [
2020-09-07 17:12:46 +08:00
'int' => ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'],
'string' => ['char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext', 'enum'],
'date' => ['date'],
'time' => ['time'],
'year' => ['year'],
'datetime' => ['datetime'],
'timestamp' => ['timestamp'],
'float' => ['float', 'double', 'decimal',],
];
2020-12-17 14:09:14 +08:00
public ?string $tableName = NULL;
2020-09-07 17:12:46 +08:00
2020-12-17 14:09:14 +08:00
public ?Connection $db = null;
2020-09-07 17:12:46 +08:00
/**
* @param string $modelPath
*/
public function setModelPath(string $modelPath): void
{
$this->modelPath = $modelPath;
}
/**
* @param string $modelNamespace
*/
public function setModelNamespace(string $modelNamespace): void
{
$this->modelNamespace = $modelNamespace;
}
/**
* @param string $controllerPath
*/
public function setControllerPath(string $controllerPath): void
{
$this->controllerPath = $controllerPath;
}
/**
* @param $module
*/
public function setModule($module)
{
$this->module = $module;
}
/**
* @param string $controllerNamespace
*/
public function setControllerNamespace(string $controllerNamespace): void
{
$this->controllerNamespace = $controllerNamespace;
}
2020-09-08 15:59:15 +08:00
/**
* @param Input $input
*/
public function setInput(Input $input)
{
$this->input = $input;
}
2020-09-07 17:12:46 +08:00
/**
2020-12-17 14:09:14 +08:00
* @param ReflectionClass $object
2020-09-07 17:12:46 +08:00
* @param $className
*
* @return string
*/
2020-12-17 14:09:14 +08:00
public function getUseContent(ReflectionClass $object, $className): string
2020-09-07 17:12:46 +08:00
{
if (empty($object)) {
return '';
}
$file = $this->getFilePath($className);
if (!file_exists($file)) {
return '';
}
$content = file_get_contents($file);
$explode = explode(PHP_EOL, $content);
$exists = array_slice($explode, 0, $object->getStartLine());
$_tmp = [];
foreach ($exists as $key => $val) {
if (trim($val) == '/**') {
break;
}
$_tmp[] = $val;
}
return trim(implode(PHP_EOL, $_tmp));
}
/**
* @param $fields
2020-12-17 14:09:14 +08:00
* @return mixed 返回表主键
2020-09-07 17:12:46 +08:00
* 返回表主键
*/
2020-12-17 14:09:14 +08:00
public function getPrimaryKey($fields): mixed
2020-09-07 17:12:46 +08:00
{
$condition = ['PRI', 'UNI'];
foreach ($fields as $field) {
if ($field['Extra'] == 'auto_increment') {
return $field['Field'];
}
if (in_array($field['Key'], $condition)) {
return $field['Field'];
}
}
return null;
}
/**
* @param $className
* @return string
*/
2020-12-17 14:09:14 +08:00
private function getFilePath($className): string
2020-09-07 17:12:46 +08:00
{
if (strpos($className, '\\')) {
$className = str_replace('\\', '/', $className);
}
if (strpos($className, '\\')) {
$className = str_replace('\\', '/', $className);
}
return APP_PATH . $className;
}
/**
2020-12-17 14:09:14 +08:00
* @param ReflectionClass $object
2020-09-07 17:12:46 +08:00
* @param $className
* @param $method
* @return string
* @throws \Exception
*/
2020-12-17 14:09:14 +08:00
public function getFuncLineContent(ReflectionClass $object, $className, $method): string
2020-09-07 17:12:46 +08:00
{
$fun = $object->getMethod($method);
$content = file_get_contents($this->getFilePath($className));
$explode = explode(PHP_EOL, $content);
2021-01-19 20:12:52 +08:00
$exists = array_slice($explode, $fun->getStartLine() - 1, $fun->getEndLine() - $fun->getStartLine() + 1);
2020-09-07 17:12:46 +08:00
return implode(PHP_EOL, $exists);
}
/**
* @return array
*/
2020-12-17 14:09:14 +08:00
protected function getModelPath(): array
2020-09-07 17:12:46 +08:00
{
$dbName = $this->db->id;
if (empty($dbName) || $dbName == 'db') {
$dbName = '';
}
$modelPath = [
'namespace' => $this->modelNamespace,
'path' => $this->modelPath,
];
if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']);
}
if (!empty($dbName)) {
$modelPath['namespace'] = $this->modelNamespace . ucfirst($dbName);
$modelPath['path'] = $this->modelPath . ucfirst($dbName);
}
if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']);
}
return $modelPath;
}
/**
* @param $db
*/
public function setConnection($db)
{
$this->db = $db;
}
/**
* @param $val
* @return string
*/
2020-12-17 14:12:44 +08:00
protected function checkIsRequired($val): string
2020-09-07 17:12:46 +08:00
{
return strtolower($val['Null']) == 'no' && $val['Default'] === NULL ? 'true' : 'false';
}
/**
* @return array
*/
2020-12-17 14:09:14 +08:00
public function getFileLists(): array
2020-09-07 17:12:46 +08:00
{
return $this->fileList;
}
}