zoukankan      html  css  js  c++  java
  • php命令行按模板生成应用的入口文件

    接着这篇文章php命令行生成项目结构 继续改造: 

    ghostwu@dev:~/php/php1/12$ tree
    .
    ├── app
    │   └── index.php
    ├── core
    │   ├── frame
    │   │   ├── ghost_frame.php
    │   │   └── template
    │   │       └── index.tpl
    │   └── ghostinit.php
    ├── function.php
    ├── ghost
    └── go.json

    index.tpl:

    <?php echo '<?php' . PHP_EOL; ?>
    /**
     * project name: <?php echo $proj . PHP_EOL; ?>
     * author: <?php echo $author . PHP_EOL; ?>
     * date: <?php echo date('Y-m-d') . PHP_EOL; ?> 
     * 
     */
     echo "hello ghostwu";

    生成的入口文件就是按照这个 模板解释的, 模板中的变量从go.json读取

    ghostinit.php:

        namespace core;
        use coreframeghost_frame;
        class ghostinit{
            static $v = 'ghost version is 1.1';
    
            static function init(){
                echo "pls input project name?" . PHP_EOL;
                $projName = fgets( STDIN );
    
                echo "pls input author?" . PHP_EOL;
                $author = fgets( STDIN );
                
                echo self::buildConfig( [ 'proj' => $projName, 'author' => $author ] );
            }
    
            static function buildConfig( $info ){
                return file_put_contents( getcwd() . '/go.json', json_encode( $info ) ) . ' bytes has written,' . 'config file has created' . PHP_EOL;
            }
    
            static function show(){
                $conf = self::loadConfig();
                foreach( $conf as $k => $v ){
                    echo $k . ':' . $v;
                }
            }
    
            static function loadConfig(){
                return json_decode( file_get_contents( getcwd() . '/go.json' ) );
            }
            
            static function start(){
                $conf = self::loadConfig();
                //$gf = new coreframeghost_frame( trim( $conf->proj ) );
                //用use引入命名空间 就不需要每次都加上命名空间去实例化类
                $gf = new ghost_frame( trim( $conf->proj ) );
                $gf->proj = trim( $conf->proj );
                $gf->author = trim( $conf->author );
                $gf->run();
            }
    
            static function __callstatic( $m, $args ){
                echo 'error function';
            }
    
        }

    ghost_frame.php

    namespace coreframe;
        class ghost_frame{
            
            public $proj = '';
            public $entrace_file = '';
    
            public function __construct( $proj ) {
                $this->proj = $proj;
            }
    
            public function run(){
                $dir = getcwd() . '/' . $this->proj;
                !file_exists( $dir ) && mkdir( $dir );
                
                extract( get_object_vars( $this ) );
                ob_start();
                include( dirname( __FILE__ ) . '/template/index.tpl' );
                $content = ob_get_contents();
                ob_end_clean();
                file_put_contents( $dir . '/index.php', $content );
            }
    
        }

    应用:

    ghostwu@dev:~/php/php1/12$ tree
    .
    ├── core
    │   ├── frame
    │   │   ├── ghost_frame.php
    │   │   └── template
    │   │       └── index.tpl
    │   └── ghostinit.php
    ├── function.php
    └── ghost
    
    3 directories, 5 files
    ghostwu@dev:~/php/php1/12$ ./ghost init
    
    pls input project name?
    Application
    pls input author?
    ghostwu
    45 bytes has written,config file has created
    
    ghostwu@dev:~/php/php1/12$ tree
    .
    ├── core
    │   ├── frame
    │   │   ├── ghost_frame.php
    │   │   └── template
    │   │       └── index.tpl
    │   └── ghostinit.php
    ├── function.php
    ├── ghost
    └── go.json
    
    3 directories, 6 files
    ghostwu@dev:~/php/php1/12$ ./ghost start
    
    
    ghostwu@dev:~/php/php1/12$ tree
    .
    ├── Application
    │   └── index.php
    ├── core
    │   ├── frame
    │   │   ├── ghost_frame.php
    │   │   └── template
    │   │       └── index.tpl
    │   └── ghostinit.php
    ├── function.php
    ├── ghost
    └── go.json
    
    4 directories, 7 files
    ghostwu@dev:~/php/php1/12$ cat Application/index.php 
    <?php
    /**
     * project name: Application
     * author: ghostwu
     * date: 2018-04-29
     
     * 
     */
     echo "hello ghostwu";
    ghostwu@dev:~/php/php1/12$ 
  • 相关阅读:
    random模块
    time模块
    软件目录开发规范
    基于贝叶斯算法实现简单的分类(java)
    百度语音合成简单案例(将文字转化为相应的语音)
    python3 load Iris.data数据集出现报错key words: b'Iris-setosa'
    python3中报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'
    Kafka的安装是否成功的简单测试命令
    伪分布式hadoop启动后jps查不到namenode的解决办法
    Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: user=lenovo, access=WRITE, inode="/user/hadoop/spark/people_savemode_test/_temporary/0":hadoop:supergro
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8970396.html
Copyright © 2011-2022 走看看