修改自动创建的index模板目录为Index

This commit is contained in:
TOP糯米 2019-09-05 20:46:00 +08:00
parent 35250ef91c
commit 28a59a2fd5
4 changed files with 71 additions and 26 deletions

View File

@ -973,6 +973,7 @@ $query
关闭数据库连接。 关闭数据库连接。
#### 注意
Database类的事务与Model类不同Model类进行了更进一步的封装。Database类事务使用示例 Database类的事务与Model类不同Model类进行了更进一步的封装。Database类事务使用示例
``` ```
use top\library\Database; use top\library\Database;

View File

@ -1,18 +1,46 @@
<?php <?php
/**
* 自动创建模块类
* Class Create
*/
class Create class Create
{ {
private $name; /**
* 模块名
* @var string
*/
private $name = '';
private $start; /**
* 命名空间
* @var string
*/
private $namespace = '';
private $namespace; /**
* 入口文件名
* @var string
*/
private $start = '';
private $base; /**
* 默认项目根目录
* @var string
*/
private $base = '';
private $dir; /**
* 当前目录
* @var string
*/
private $dir = '';
/**
* 模块目录
* @var string
*/
private $projectPath; private $projectPath;
public function __construct($start, $namespace, $name) public function __construct($start, $namespace, $name)
@ -27,6 +55,11 @@ class Create
$this->create(); $this->create();
} }
/**
* 替换内容
* @param $content
* @return mixed
*/
public function replaceContent($content) public function replaceContent($content)
{ {
return str_replace([ return str_replace([
@ -38,9 +71,13 @@ class Create
], $content); ], $content);
} }
/**
* 创建入口文件
* @return bool
*/
public function createStartFile() public function createStartFile()
{ {
if ($this->start && !file_exists($this->start)) { if ($this->start && !is_file($this->start)) {
$content = file_get_contents($this->dir . 'tpl/index.tpl'); $content = file_get_contents($this->dir . 'tpl/index.tpl');
$content = $this->replaceContent($content); $content = $this->replaceContent($content);
if (file_put_contents($this->start, $content)) { if (file_put_contents($this->start, $content)) {
@ -51,6 +88,9 @@ class Create
return true; return true;
} }
/**
* 创建配置文件
*/
public function createConfig() public function createConfig()
{ {
$configPath = $this->projectPath . 'config/'; $configPath = $this->projectPath . 'config/';
@ -58,7 +98,7 @@ class Create
if (!is_dir($configPath)) { if (!is_dir($configPath)) {
mkdir($configPath, 0755, true); mkdir($configPath, 0755, true);
} }
if (!file_exists($configFile)) { if (!is_file($configFile)) {
$content = file_get_contents($this->dir . 'tpl/config/config.tpl'); $content = file_get_contents($this->dir . 'tpl/config/config.tpl');
$content = $this->replaceContent($content); $content = $this->replaceContent($content);
$realConfigFile = $this->base . '/' . $this->namespace . '/' . $this->name . '/config/config.php'; $realConfigFile = $this->base . '/' . $this->namespace . '/' . $this->name . '/config/config.php';
@ -66,9 +106,11 @@ class Create
exit('error -2'); exit('error -2');
} }
} }
return true;
} }
/**
* 创建MVC目录及文件
*/
public function createMVC() public function createMVC()
{ {
$dirArray = [ $dirArray = [
@ -82,7 +124,7 @@ class Create
} }
} }
$controllerFile = $this->projectPath . 'controller/index.php'; $controllerFile = $this->projectPath . 'controller/index.php';
if (!file_exists($controllerFile)) { if (!is_file($controllerFile)) {
$content = file_get_contents($this->dir . 'tpl/controller/index.tpl'); $content = file_get_contents($this->dir . 'tpl/controller/index.tpl');
$content = $this->replaceContent($content); $content = $this->replaceContent($content);
if (!file_put_contents($this->projectPath . 'controller/Index.php', $content)) { if (!file_put_contents($this->projectPath . 'controller/Index.php', $content)) {
@ -90,7 +132,7 @@ class Create
} }
} }
$modelFile = $this->projectPath . 'model/demo.php'; $modelFile = $this->projectPath . 'model/demo.php';
if (!file_exists($modelFile)) { if (!is_file($modelFile)) {
$content = file_get_contents($this->dir . 'tpl/model/demo.tpl'); $content = file_get_contents($this->dir . 'tpl/model/demo.tpl');
$content = $this->replaceContent($content); $content = $this->replaceContent($content);
if (!file_put_contents($this->projectPath . 'model/Demo.php', $content)) { if (!file_put_contents($this->projectPath . 'model/Demo.php', $content)) {
@ -98,10 +140,10 @@ class Create
} }
} }
$viewFile = $this->projectPath . 'view/index/index.html'; $viewFile = $this->projectPath . 'view/index/index.html';
if (!file_exists($viewFile)) { if (!is_file($viewFile)) {
$content = file_get_contents($this->dir . 'tpl/view/index.tpl'); $content = file_get_contents($this->dir . 'tpl/view/index.tpl');
if (!is_dir($this->projectPath . 'view/index/')) { if (!is_dir($this->projectPath . 'view/Index/')) {
mkdir($this->projectPath . 'view/index/', 0755, true); mkdir($this->projectPath . 'view/Index/', 0755, true);
} }
if (!file_put_contents($this->projectPath . 'view/Index/index.html', $content)) { if (!file_put_contents($this->projectPath . 'view/Index/index.html', $content)) {
exit('error -6'); exit('error -6');
@ -109,26 +151,35 @@ class Create
} }
} }
/**
* 创建函数库文件
*/
public function createFunctions() public function createFunctions()
{ {
$file = $this->projectPath . 'functions.php'; $file = $this->projectPath . 'functions.php';
if (!file_exists($file)) { if (!is_file($file)) {
if (!file_put_contents($file, "<?php\r\n")) { if (!file_put_contents($file, "<?php\r\n")) {
exit('-7'); exit('-7');
} }
} }
} }
/**
* 创建路由文件
*/
public function createRoute() public function createRoute()
{ {
$file = $this->projectPath . '../route.php'; $file = $this->projectPath . '../route.php';
if (!file_exists($file)) { if (!is_file($file)) {
if (!file_put_contents($file, file_get_contents($this->dir . 'tpl/route.tpl'))) { if (!file_put_contents($file, file_get_contents($this->dir . 'tpl/route.tpl'))) {
exit('-8'); exit('-8');
} }
} }
} }
/**
* 执行创建操作
*/
public function create() public function create()
{ {
$this->createStartFile(); $this->createStartFile();

View File

@ -2,14 +2,14 @@
namespace app\{name}\controller; namespace app\{name}\controller;
use top\library\Controller; use app\{name}\model\Demo;
class Index extends Controller class Index
{ {
public function index() public function index()
{ {
$model = model(\app\{name}\model\Demo::class); $model = model(Demo::class);
return [ return [
'hello' => $model->get() 'hello' => $model->get()
]; ];

View File

@ -2,15 +2,8 @@
namespace app\{name}\model; namespace app\{name}\model;
use top\library\Model; class Demo
class Demo extends Model
{ {
protected $table = '';
protected $pk = '';
protected $map = [];
public function get() public function get()
{ {
return '模块{name}正在运行...'; return '模块{name}正在运行...';