• 3)利用Build.php自动创建目录和文件


    (1)首先做法参照:

            thinkphp5的手册的  命令行--->自动生成目录结构

            或者看云的资料:https://www.kancloud.cn/manual/thinkphp5/118021

    (2)build.php(位于我的框架的根目录下)

                    

         build.php内容展示,其实没啥变化

     1 <?php
     2 // +----------------------------------------------------------------------
     3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
     4 // +----------------------------------------------------------------------
     5 // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
     6 // +----------------------------------------------------------------------
     7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
     8 // +----------------------------------------------------------------------
     9 // | Author: liu21st <liu21st@gmail.com>
    10 // +----------------------------------------------------------------------
    11     /**
    12      * 对应那个thinkphp中的library中的think  的Build.php文件
    13      */
    14 return [
    15     // 生成应用公共文件
    16     '__file__' => ['common.php', 'config.php', 'database.php'],
    17 
    18     // 定义demo模块的自动生成 (按照实际定义的文件名生成)
    19     'demo'     => [
    20         '__file__'   => ['common.php','config.php'],
    21         '__dir__'    => ['behavior', 'controller', 'model', 'view'],
    22         'controller' => ['Index', 'Test', 'UserType'],
    23         'model'      => ['User', 'UserType'],
    24         'view'       => ['index/index'],
    25     ],
    26     // 其他更多的模块定义
    27 ];

        观察可知:__file__是单独的,但是你看那个__dir__中的controler   model    view和下面的那个   controller     model    view是对应的   其实就是对应Build.php中的  建立目录   监理文件和建立模板的函数对应  

      下面是那个Build.php文件代码展示:

        

      1 <?php
      2 // +----------------------------------------------------------------------
      3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
      4 // +----------------------------------------------------------------------
      5 // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
      6 // +----------------------------------------------------------------------
      7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
      8 // +----------------------------------------------------------------------
      9 // | Author: liu21st <liu21st@gmail.com>
     10 // +----------------------------------------------------------------------
     11 
     12 namespace think;
     13 /**
     14  * 这个是我自动创建类文件,详细可以看手册的命令行--->自动生成目录结构
     15  * run()方法,你传入一个配置文件的返回值,就创建一个对应的目录
     16  * BuildDir()创建一个目录,传一个数组
     17  *所有的参数参照build.php文件写法
     18  *
     19  * 可以模仿这个建立模块或者目录的函数的写法,自己再编写几个不一样的创建东西的函数。
     20  * Class Build
     21  * @package think
     22  */
     23 class Build
     24 {
     25     /**
     26      * 根据传入的build资料创建目录和文件
     27      * @access protected
     28      * @param  array  $build build列表
     29      * @param  string $namespace 应用类库命名空间
     30      * @param  bool   $suffix 类库后缀
     31      * @return void
     32      */
     33     public static function run(array $build = [], $namespace = 'app', $suffix = false)
     34     {
     35         // 锁定
     36         $lockfile = APP_PATH . 'build.lock';
     37         if (is_writable($lockfile)) {
     38             return;
     39         } elseif (!touch($lockfile)) {
     40             throw new Exception('应用目录[' . APP_PATH . ']不可写,目录无法自动生成!<BR>请手动生成项目目录~', 10006);
     41         }
     42         foreach ($build as $module => $list) {
     43             if ('__dir__' == $module) {
     44                 // 创建目录列表
     45                 self::buildDir($list);
     46             } elseif ('__file__' == $module) {
     47                 // 创建文件列表
     48                 self::buildFile($list);
     49             } else {
     50                 // 创建模块
     51                 self::module($module, $list, $namespace, $suffix);
     52             }
     53         }
     54         // 解除锁定
     55         unlink($lockfile);
     56     }
     57 
     58     /**
     59      * 创建目录
     60      * @access protected
     61      * @param  array $list 目录列表
     62      * @return void
     63      */
     64     protected static function buildDir($list)
     65     {
     66         foreach ($list as $dir) {
     67             if (!is_dir(APP_PATH . $dir)) {
     68                 // 创建目录
     69                 mkdir(APP_PATH . $dir, 0755, true);
     70             }
     71         }
     72     }
     73 
     74     /**
     75      * 创建文件
     76      * @access protected
     77      * @param  array $list 文件列表
     78      * @return void
     79      */
     80     protected static function buildFile($list)
     81     {
     82         foreach ($list as $file) {
     83             if (!is_dir(APP_PATH . dirname($file))) {
     84                 // 创建目录
     85                 mkdir(APP_PATH . dirname($file), 0755, true);
     86             }
     87             if (!is_file(APP_PATH . $file)) {
     88                 file_put_contents(APP_PATH . $file, 'php' == pathinfo($file, PATHINFO_EXTENSION) ? "<?php
    " : '');
     89             }
     90         }
     91     }
     92 
     93     /**
     94      * 创建模块
     95      * @access public
     96      * @param  string $module 模块名
     97      * @param  array  $list build列表
     98      * @param  string $namespace 应用类库命名空间
     99      * @param  bool   $suffix 类库后缀
    100      * @return void
    101      */
    102     public static function module($module = '', $list = [], $namespace = 'app', $suffix = false)
    103     {
    104         $module = $module ? $module : '';
    105         if (!is_dir(APP_PATH . $module)) {
    106             // 创建模块目录
    107             mkdir(APP_PATH . $module);
    108         }
    109         if (basename(RUNTIME_PATH) != $module) {
    110             // 创建配置文件和公共文件
    111             self::buildCommon($module);
    112             // 创建模块的默认页面
    113             self::buildHello($module, $namespace, $suffix);
    114         }
    115         if (empty($list)) {
    116             // 创建默认的模块目录和文件
    117             $list = [
    118                 '__file__' => ['config.php', 'common.php'],
    119                 '__dir__'  => ['controller', 'model', 'view'],
    120             ];
    121         }
    122         // 创建子目录和文件
    123         foreach ($list as $path => $file) {
    124             $modulePath = APP_PATH . $module . DS;
    125             if ('__dir__' == $path) {
    126                 // 生成子目录
    127                 foreach ($file as $dir) {
    128                     self::checkDirBuild($modulePath . $dir);
    129                 }
    130             } elseif ('__file__' == $path) {
    131                 // 生成(空白)文件
    132                 foreach ($file as $name) {
    133                     if (!is_file($modulePath . $name)) {
    134                         file_put_contents($modulePath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "<?php
    " : '');
    135                     }
    136                 }
    137             } else {
    138                 // 生成相关MVC文件
    139                 foreach ($file as $val) {
    140                     $val      = trim($val);
    141                     $filename = $modulePath . $path . DS . $val . ($suffix ? ucfirst($path) : '') . EXT;
    142                     $space    = $namespace . '\' . ($module ? $module . '\' : '') . $path;
    143                     $class    = $val . ($suffix ? ucfirst($path) : '');
    144                     switch ($path) {
    145                         case 'controller': // 控制器
    146                             $content = "<?php
    namespace {$space};
    
    class {$class}
    {
    
    }";
    147                             break;
    148                         case 'model': // 模型
    149                             $content = "<?php
    namespace {$space};
    
    use thinkModel;
    
    class {$class} extends Model
    {
    
    }";
    150                             break;
    151                         case 'view': // 视图
    152                             $filename = $modulePath . $path . DS . $val . '.html';
    153                             self::checkDirBuild(dirname($filename));
    154                             $content = '';
    155                             break;
    156                         default:
    157                             // 其他文件
    158                             $content = "<?php
    namespace {$space};
    
    class {$class}
    {
    
    }";
    159                     }
    160 
    161                     if (!is_file($filename)) {
    162                         file_put_contents($filename, $content);
    163                     }
    164                 }
    165             }
    166         }
    167     }
    168 
    169     /**
    170      * 创建模块的欢迎页面
    171      * @access public
    172      * @param  string $module 模块名
    173      * @param  string $namespace 应用类库命名空间
    174      * @param  bool   $suffix 类库后缀
    175      * @return void
    176      */
    177     protected static function buildHello($module, $namespace, $suffix = false)
    178     {
    179         $filename = APP_PATH . ($module ? $module . DS : '') . 'controller' . DS . 'Index' . ($suffix ? 'Controller' : '') . EXT;
    180         if (!is_file($filename)) {
    181             $content = file_get_contents(THINK_PATH . 'tpl' . DS . 'default_index.tpl');
    182             $content = str_replace(['{$app}', '{$module}', '{layer}', '{$suffix}'], [$namespace, $module ? $module . '\' : '', 'controller', $suffix ? 'Controller' : ''], $content);
    183             self::checkDirBuild(dirname($filename));
    184             file_put_contents($filename, $content);
    185         }
    186     }
    187 
    188     /**
    189      * 创建模块的公共文件
    190      * @access public
    191      * @param  string $module 模块名
    192      * @return void
    193      */
    194     protected static function buildCommon($module)
    195     {
    196         $filename = CONF_PATH . ($module ? $module . DS : '') . 'config.php';
    197 
    198         self::checkDirBuild(dirname($filename));
    199         if (!is_file($filename)) {
    200             file_put_contents($filename, "<?php
    //配置文件
    return [
    
    ];");
    201         }
    202         $filename = APP_PATH . ($module ? $module . DS : '') . 'common.php';
    203         if (!is_file($filename)) {
    204             file_put_contents($filename, "<?php
    ");
    205         }
    206     }
    207 
    208     protected static function checkDirBuild($dirname)
    209     {
    210         if (!is_dir($dirname)) {
    211             mkdir($dirname, 0755, true);
    212         }
    213     }
    214 }

    (3)然后在index.php文件中的写法展示:

          

     1 <?php
     2 // +----------------------------------------------------------------------
     3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
     4 // +----------------------------------------------------------------------
     5 // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
     6 // +----------------------------------------------------------------------
     7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
     8 // +----------------------------------------------------------------------
     9 // | Author: liu21st <liu21st@gmail.com>
    10 // +----------------------------------------------------------------------
    11 
    12 // [ 应用入口文件 ]
    13 define('CONF_PATH',__DIR__.'./config/');
    14 // 定义应用目录
    15 define('APP_PATH', __DIR__.'./application/');
    16     //define('STATIC','/Per_boke/public/static');
    17 // 加载框架引导文件
    18 require __DIR__.'./thinkphp/start.php';
    19     /**
    20      * 下面是自己定义的配置文件的目录
    21      */
    22 $build=include './build.php';
    23 	hinkBuild::run($build);

    详细讲解可看:http://pan.baidu.com/s/1jIx47fK      密码:hvje

  • 相关阅读:
    SpringIoC和DI注解开发
    SpringIoC&DI
    Linux基础(二)
    Linux基础(一)
    代码自动生成
    luaScript目标点限流工具类示例
    SpringMVC请求连接匹配器-工具类
    解决commons.mail.HtmlEmail附件中文名乱码问题
    HtmlToPdfUtils [请参照码云上 https://gitee.com/bbevis/html-to-pdf 最新版]
    ThreadLocalUtils
  • 原文地址:https://www.cnblogs.com/xiaoyoucai/p/7528354.html
Copyright © 2020-2023  润新知