当前位置: 首页 > news >正文

php命令行生成项目结构

ghostinit.php

<?php
    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 );

            var_dump( $projName, $author );
            
            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();
            $dir = getcwd() . '/' . trim( $conf->proj );
            !file_exists( $dir ) && mkdir( $dir );
            !file_exists( $dir . '/index.php' ) && file_put_contents( $dir . '/index.php', '' );
        }

        static function __callstatic( $m, $args ){
            echo 'error function';
        }

    }

?>

用法:

ghostwu@dev:~/php/php1/10$ ls
ghost  ghostinit.php
ghostwu@dev:~/php/php1/10$ ./ghost init
pls input project name?
hello
pls input author?
ghostwu
string(6) "hello
"
string(8) "ghostwu
"
39 bytes has written,config file has created

ghostwu@dev:~/php/php1/10$ ls
ghost  ghostinit.php  go.json
ghostwu@dev:~/php/php1/10$ ./ghost start

ghostwu@dev:~/php/php1/10$ ls
ghost  ghostinit.php  go.json  hello
ghostwu@dev:~/php/php1/10$ tree hello
hello
└── index.php

0 directories, 1 file
ghostwu@dev:~/php/php1/10$ 
View Code

 

用类来单独改造

ghost_frame.php

<?php
    class ghost_frame{
        
        public $proj = '';
        public $entrace_file = '';

        public function __construct( $proj ) {
            $this->proj = $proj;
            $dir = getcwd() . '/' . $proj;
            !file_exists( $dir ) && mkdir( $dir );
            !file_exists( $dir . '/index.php' ) && file_put_contents( $dir . '/index.php', '' );
        }

    }
?>

ghostinit.php,由于调用了ghost_frame,需要在ghostinit.php中require这个文件

         static function start(){
              $conf = self::loadConfig();
              $gf = new ghost_frame( trim( $conf->proj ) );
         }

 当然我们可以用自动加载来改造

首先,建立框架的目录结构,类似于thinkphp( Library\Thinkphp.php )

ghostwu@dev:~/php/php1/10$ ls
core  ghost  ghostinit.php  go.json  hello
ghostwu@dev:~/php/php1/10$ tree core
core
└── frame
    ├── ghost_frame.php
    └── template
ghostwu@dev:~/php/php1/10$ tree
.
├── core
│   └── frame
│       ├── ghost_frame.php
│       └── template
├── ghost
├── ghostinit.php
├── go.json
└── hello
    └── index.php

完整的ghostinit.php

<?php
    use core\frame\ghost_frame;
    function __autoload( $className ) {
        $className = str_replace( '\\', '/', $className );
        require( $className . '.php' );    
    }
    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 core\frame\ghost_frame( trim( $conf->proj ) );
            //用use引入命名空间 就不需要每次都加上命名空间去实例化类
            $gf = new ghost_frame( trim( $conf->proj ) );
        }

        static function __callstatic( $m, $args ){
            echo 'error function';
        }

    }

?>
View Code

ghost_frame.php

<?php
    namespace core\frame;
    class ghost_frame{
        
        public $proj = '';
        public $entrace_file = '';

        public function __construct( $proj ) {
            $this->proj = $proj;
            $dir = getcwd() . '/' . $proj;
            !file_exists( $dir ) && mkdir( $dir );
            !file_exists( $dir . '/index.php' ) && file_put_contents( $dir . '/index.php', '' );
        }

    }
?>
View Code

最后的改造:

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

ghost:

 1 #!/usr/bin/php
 2 <?php
 3 use core\ghostinit;
 4 require_once( 'function.php' );
 5 $result = '';
 6 
 7 if( $argc >= 2 ) {
 8     $p = $argv[1]; 
 9     //如果以 '-' 开头, 表示属性
10     if( substr( $p, 0, 1 ) == '-' ) {
11         // -v变成v
12         $p = substr( $p, 1 );
13         $result = isset( ghostinit::$$p ) ? ghostinit::$$p : 'error';
14     }else {
15         $result = ghostinit::$p();
16     }
17 }
18 
19 echo $result . PHP_EOL;

ghostinit.php

namespace core;
    use core\frame\ghost_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 core\frame\ghost_frame( trim( $conf->proj ) );
            //用use引入命名空间 就不需要每次都加上命名空间去实例化类
            $gf = new ghost_frame( trim( $conf->proj ) );
        }

        static function __callstatic( $m, $args ){
            echo 'error function';
        }

    }
View Code

 

相关文章:

  • P4035 [JSOI2008]球形空间产生器
  • 简述this指向
  • 如何理解angular自定义指令directive的scope属性?
  • zabbix3配置阿里云邮箱告警
  • 小葵花妈妈课堂开课了:《Runnable、Callable、Future、RunnableFuture、FutureTask 源码分析》...
  • 跟我学Shiro电子书
  • 嵌入式视觉应用的疆土在逐步扩大
  • requests 中文乱码
  • [原]Python安装和使用MySQLdb库(Windows系统)
  • c# IPC实现本机进程之间的通信
  • 网络编程--基础TCP
  • 使用jMeter构造大量并发HTTP请求进行微服务性能测试
  • DAY18-Django之分页和中间件
  • jmeter接口测试步骤
  • 网关地址设置
  • Create React App 使用
  • HTML中设置input等文本框为不可操作
  • IDEA常用插件整理
  • iOS编译提示和导航提示
  • java中具有继承关系的类及其对象初始化顺序
  • js正则,这点儿就够用了
  • Linux CTF 逆向入门
  • Netty 4.1 源代码学习:线程模型
  • Promise面试题2实现异步串行执行
  • React系列之 Redux 架构模式
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • 从setTimeout-setInterval看JS线程
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 大数据与云计算学习:数据分析(二)
  • 第十八天-企业应用架构模式-基本模式
  • 复杂数据处理
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 技术发展面试
  • 前端面试总结(at, md)
  • 如何在 Tornado 中实现 Middleware
  • 世界上最简单的无等待算法(getAndIncrement)
  • 微信小程序开发问题汇总
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 原生js练习题---第五课
  • Nginx实现动静分离
  • 阿里云服务器如何修改远程端口?
  • 选择阿里云数据库HBase版十大理由
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • #pragma multi_compile #pragma shader_feature
  • #Spring-boot高级
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (java)关于Thread的挂起和恢复
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (附源码)spring boot基于小程序酒店疫情系统 毕业设计 091931
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (四)linux文件内容查看
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET 表达式计算:Expression Evaluator
  • .Net 高效开发之不可错过的实用工具