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/$ ls
ghost ghostinit.php
ghostwu@dev:~/php/php1/$ ./ghost init
pls input project name?
hello
pls input author?
ghostwu
string() "hello
"
string() "ghostwu
"
bytes has written,config file has created ghostwu@dev:~/php/php1/$ ls
ghost ghostinit.php go.json
ghostwu@dev:~/php/php1/$ ./ghost start ghostwu@dev:~/php/php1/$ ls
ghost ghostinit.php go.json hello
ghostwu@dev:~/php/php1/$ tree hello
hello
└── index.php directories, file
ghostwu@dev:~/php/php1/$

用类来单独改造

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/$ 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';
} } ?>

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', '' );
} }
?>

最后的改造:

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

ghost:

 #!/usr/bin/php
<?php
use core\ghostinit;
require_once( 'function.php' );
$result = ''; if( $argc >= 2 ) {
$p = $argv[1];
//如果以 '-' 开头, 表示属性
if( substr( $p, 0, 1 ) == '-' ) {
// -v变成v
$p = substr( $p, 1 );
$result = isset( ghostinit::$$p ) ? ghostinit::$$p : 'error';
}else {
$result = ghostinit::$p();
}
} 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';
} }

php命令行生成项目结构的更多相关文章

  1. cocos命令行生成项目

    cocos命令行生成项目: cocos new GoodDay(项目名称) -p com.boleban.www(包名字) -l cpp(项目类型) -d D:\DevProject\cocos2dx ...

  2. Maven 命令行创建项目时 Could not find goal ‘create’ in plugin org.apache.maven.plugins:...

    使用maven3.3.9 版本,进行命令行创建项目时输入以下命令创建失败 mvn archetype:create -DgroupId=com.zang.maven  -DartifactId=sys ...

  3. thinkphp命令行生成模型类

    thinkphp命令行生成模型类 当你需要创建大量的模型类的时候,不妨考虑下命令行生成,可以快速创建模型类. 在windows下面,使用Win+R输入cmd进入命令控制台,切换到项目根目录(也就是th ...

  4. 《Entity Framework 6 Recipes》中文翻译系列 (40) ------ 第七章 使用对象服务之从跟踪器中获取实体与从命令行生成模型(想解决EF第一次查询慢的,请阅读)

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-5  从跟踪器中获取实体 问题 你想创建一个扩展方法,从跟踪器中获取实体,用于数 ...

  5. egret命令行编译项目时 版本不对应的问题

    egret 命令行编译项目时 如使用 egret build -e 会出现版本不对应的问题 分析原因 A,B项目 A项目使用1.8的egret引擎, B项目使用2.5引擎 但本地引擎升级至2.5.5, ...

  6. Beyond Compare 命令行生成目录下所有文件比对的Html网页report

    MAC环境下,使用Beyond Compare命令行生成两个文件夹差异的html,按目录递归生成. #1. 创建compare #2. 创建compare/old #3. compare/new #4 ...

  7. 用OpenSSL命令行生成证书文件

    用OpenSSL命令行生成证书文件 1.首先要生成服务器端的私钥(key文件): openssl genrsa -des3 -out server.key 1024 运行时会提示输入密码,此密码用于加 ...

  8. mysql中用命令行复制表结构(数据)

    mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 或 CREATE TABLE 新表 ...

  9. Jmeter之命令行生成HTML报告

    其实每次使用jemter.bat文件启动JMeter时,命令行窗口都会提示我们不要使用GUI窗口进行测试,除非是进行调试脚本 使用命令行生成结果也很测试报告也很简单 jmeter -n -t [jmx ...

随机推荐

  1. Oracle11g手动创建数据库方法

    Oracle11g手动创建数据库方法 参考网页http://www.th7.cn/db/Oracle/201311/36926.shtml 安装路径 我的安装路径是:E:\app\admin\prod ...

  2. HTML引入JS文件

    浏览器解析HTML文件时,先判断script 标签中是否有src属性,有则执行指定路径下的JS文件,没有则执行script标签中的js脚本. 1. HTML内嵌JS head里面添加script元素, ...

  3. JAVA实现微信支付V3

    喜欢的朋友可以关注下,粉丝也缺. 相信很多的码友在项目中都需要接入微信支付,虽说微信支付已成为一个普遍的现象,但是接入的过程中难免会遇到各种各样的坑,这一点支付宝的SDK就做的很好,已经完成的都知道了 ...

  4. js DOM 案例

    模态框 <html> <head> <meta charset="UTF-8"> <title>模态框</title> ...

  5. MyEclipse 编写 JSP 代码时很卡的解决办法

    在网上看到很多方法,都是尝试过,个人感觉都没有说到重点,所以收效甚微. 后来自己总结了一下: 我们都是习惯在MyEclipse 工具,双击jsp 文件打开进行编辑.这时,工具会打开窗口的 Previe ...

  6. Day.js - JavaScript时间处理库

    Day.js简介 在使用JavaScript处理时间方面,使用的时Moment.js,但是它太重了,有200多k,一般项目中可能也只是用了几个api而已,所以,这里推荐一个轻量的时间库 - Day.j ...

  7. C#委托和事件例析

    我是对Java了解相对较多,而对C#则是因工作需要才去看了一下,C#跟Java在语法上非常相似,而最初让我比较困惑的就是委托.事件部分,相信大多数初学者也有类似的困惑.经过跟Java的对比学习,发现这 ...

  8. ASP.NET Core 1.0 基础与应用启动

    .NET Core http://dotnet.github.io/[https://github.com/dotnet/coreclr] ASP.NET Core 1.0 https://get.a ...

  9. 全网最详细的Xshell或SecureCRT下spark-shell里出现无法退格或者删除的问题现象的解决办法(图文详解)

    不多说,直接上干货! 前言 打开spark的命令行后,发现输错字符了,但是无法退格或者删除,这是比较苦恼的问题. 这个问题,得看你是用Xshell,还是SecureCRT. 一般是出现在SecureC ...

  10. 【转】Intellij IDEA使用总结

    原文地址:http://totohust.iteye.com/blog/1035550 1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.ex ...