149 lines
3.8 KiB
PHP
Raw Normal View History

2020-04-28 16:34:17 +08:00
<?php
2020-04-28 22:02:03 +08:00
namespace catcher\generate\factory;
2020-04-28 16:34:17 +08:00
use catcher\exceptions\FailedException;
2020-04-28 22:02:03 +08:00
use catcher\generate\template\Controller as Template;
2020-04-28 16:34:17 +08:00
use think\helper\Str;
class Controller extends Factory
{
protected $methods = [];
/**
*
* @time 2020年04月27日
* @param $params
* @return bool|string|string[]
*/
public function done($params)
2020-04-28 22:02:03 +08:00
{
// 写入成功之后
if (file_put_contents($this->getGeneratePath($params['controller']), $this->getContent($params))) {
return (new Route())->controller($params['controller'])
->restful($params['restful'])
->methods($this->parseOtherMethods($params['other_function']))
->done();
}
throw new FailedException($params['controller'] . ' generate failed~');
}
/**
* 获取内容
*
* @time 2020年04月28日
* @param $params
* @return bool|string|string[]
*/
2020-04-29 11:41:54 +08:00
public function getContent($params)
2020-04-28 16:34:17 +08:00
{
if (!$params['controller']) {
2020-04-28 22:02:03 +08:00
throw new FailedException('params has lost');
2020-04-28 16:34:17 +08:00
}
$template = new Template();
// parse controller
[$className, $namespace] = $this->parseFilename($params['controller']);
if (!$className) {
throw new FailedException('未填写控制器名称');
}
2020-04-29 11:41:54 +08:00
2020-04-28 16:34:17 +08:00
// parse model
[$model, $modelNamespace] = $this->parseFilename($params['model']);
2020-04-28 22:02:03 +08:00
$use = implode(';',[
'use ' . $params['model'] .' as '. $model . 'Model',
]) . ';';
2020-04-28 16:34:17 +08:00
2020-04-28 22:02:03 +08:00
$content = $template->header() .
$template->nameSpace($namespace) .
str_replace('{USE}', $model ? $use : '', $template->uses()) .
$template->createClass($className);
2020-04-28 16:34:17 +08:00
2020-04-28 22:02:03 +08:00
return str_replace('{CONTENT}', ($model ? $template->construct($model.'Model') : '') . rtrim($this->content($params, $template), "\r\n"), $content);
2020-04-28 16:34:17 +08:00
}
2020-04-28 22:02:03 +08:00
/**
* parse use
*
* @time 2020年04月28日
* @param $params
* @return string
*/
protected function parseUse($params)
{
}
2020-04-28 16:34:17 +08:00
/**
* content
*
* @time 2020年04月27日
* @param $params
* @param $template
* @return string
*/
protected function content($params, $template)
{
$content = '';
if ($params['restful']) {
$methods = $this->restful();
$this->methods = array_merge($this->methods, $methods);
foreach ($methods as $method) {
$content .= $template->{$method[0]}();
}
}
if (!empty($params['other_function'])) {
$others = $this->parseOtherMethods($params['other_function']);
$this->methods = array_merge($this->methods, $others);
foreach ($others as $other) {
$content .= $template->otherFunction($other[0], $other[1]);
}
}
return $content;
}
/**
* parse $method
* class_method/http_method
* @time 2020年04月27日
* @param $methods
* @return false|string[]
*/
public function parseOtherMethods($methods)
{
$_methods = [];
foreach ($methods as $method) {
if (Str::contains($method, '/')) {
$_methods[] = explode('/', $method);
} else {
// 默认使用 Get 方式
$_methods[] = [$method, 'get'];
}
}
return $_methods;
}
/**
* restful 路由
*
* @time 2020年04月27日
* @return \string[][]
*/
public function restful()
{
return [
['index', 'get'],
['save', 'post'],
['read', 'get'],
['update', 'put'],
['delete', 'delete'],
];
}
}