You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.8 KiB
126 lines
2.8 KiB
<?php |
|
/** |
|
* |
|
* Make.php |
|
* User: ChenLong |
|
* DateTime: 2020/3/11 14:37 |
|
*/ |
|
|
|
|
|
namespace sdModule\makeAdminBasics; |
|
|
|
|
|
|
|
use sdModule\common\Sc; |
|
use sdModule\makeAdminBasics\makeItem\MakeItemInterface; |
|
|
|
class Make |
|
{ |
|
/** |
|
* @var |
|
*/ |
|
public $pageName; |
|
/** |
|
* @var |
|
*/ |
|
public $table; |
|
/** |
|
* @var array |
|
*/ |
|
public $make; |
|
/** |
|
* @var mixed|string |
|
*/ |
|
public $makeData; |
|
/** |
|
* @var |
|
*/ |
|
public $primaryKey; |
|
/** |
|
* @var |
|
*/ |
|
public $tableComment; |
|
/** |
|
* 当前表字段详情 信息 |
|
* @var |
|
*/ |
|
public $tableInfo; |
|
|
|
/** |
|
* Make constructor. |
|
* @param $data |
|
*/ |
|
public function __construct($data) |
|
{ |
|
$this->table = $data['table_name'] ?? ''; |
|
$this->tableComment = (new Basics())->getTableComment($this->table); |
|
$this->pageName = $data['page_name'] ?: $this->tableComment; |
|
$this->make = $data['make'] ?? []; |
|
$this->tableInfo = array_column((new Basics())->getTableInfo($this->table), null, 'column_name'); |
|
unset($data['page_name'], $data['table_name'], $data['make']); |
|
$this->primaryKey = strtr(ConfigHelper::get('primary_key'), ['{table}' => $this->table]); |
|
|
|
$this->makeData = $this->dataJoinHandle($data); |
|
} |
|
|
|
|
|
/** |
|
* @return bool|int |
|
* @throws \ReflectionException |
|
*/ |
|
public function make() |
|
{ |
|
$error = ''; |
|
foreach (ConfigHelper::get('make_class') as $class => $value) { |
|
if (!isset($value['tag']) || !in_array($value['tag'], $this->make)) { |
|
continue; |
|
} |
|
$classInstance = Sc::reflex()->getInstance($class, ['make' => $this]); |
|
if (!$classInstance instanceof MakeItemInterface) { |
|
$error .= "{$class} 调用出错,请引用接口:" . MakeItemInterface::class . PHP_EOL; |
|
} |
|
|
|
$classInstance->make() or $error .= $class . ' 创建失败!' . PHP_EOL; |
|
} |
|
|
|
return $error; |
|
} |
|
|
|
/** |
|
* @param $url |
|
* @param $content |
|
* @return bool|int |
|
*/ |
|
public function makeFile($url, $content) |
|
{ |
|
if (!is_dir(dirname($url))) { |
|
mkdir(dirname($url), 0777, true); |
|
chmod(dirname($url), 0777); |
|
} |
|
//兼容linux |
|
return file_put_contents($url, $content); |
|
|
|
} |
|
|
|
|
|
/** |
|
* @param $data |
|
* @return mixed |
|
*/ |
|
private function dataJoinHandle($data) |
|
{ |
|
foreach ($data as &$datum) { |
|
if (empty($datum['join']) || !json_decode($datum['join'])) continue; |
|
|
|
$join = []; |
|
foreach (json_decode($datum['join']) as $item) { |
|
list($key, $value) = explode('=', $item); |
|
$join[trim($key)] = trim($value); |
|
} |
|
|
|
$datum['join'] = $join; |
|
} |
|
return $data; |
|
} |
|
} |
|
|
|
|