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/$ 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命令行生成项目结构的更多相关文章
- cocos命令行生成项目
cocos命令行生成项目: cocos new GoodDay(项目名称) -p com.boleban.www(包名字) -l cpp(项目类型) -d D:\DevProject\cocos2dx ...
- Maven 命令行创建项目时 Could not find goal ‘create’ in plugin org.apache.maven.plugins:...
使用maven3.3.9 版本,进行命令行创建项目时输入以下命令创建失败 mvn archetype:create -DgroupId=com.zang.maven -DartifactId=sys ...
- thinkphp命令行生成模型类
thinkphp命令行生成模型类 当你需要创建大量的模型类的时候,不妨考虑下命令行生成,可以快速创建模型类. 在windows下面,使用Win+R输入cmd进入命令控制台,切换到项目根目录(也就是th ...
- 《Entity Framework 6 Recipes》中文翻译系列 (40) ------ 第七章 使用对象服务之从跟踪器中获取实体与从命令行生成模型(想解决EF第一次查询慢的,请阅读)
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-5 从跟踪器中获取实体 问题 你想创建一个扩展方法,从跟踪器中获取实体,用于数 ...
- egret命令行编译项目时 版本不对应的问题
egret 命令行编译项目时 如使用 egret build -e 会出现版本不对应的问题 分析原因 A,B项目 A项目使用1.8的egret引擎, B项目使用2.5引擎 但本地引擎升级至2.5.5, ...
- Beyond Compare 命令行生成目录下所有文件比对的Html网页report
MAC环境下,使用Beyond Compare命令行生成两个文件夹差异的html,按目录递归生成. #1. 创建compare #2. 创建compare/old #3. compare/new #4 ...
- 用OpenSSL命令行生成证书文件
用OpenSSL命令行生成证书文件 1.首先要生成服务器端的私钥(key文件): openssl genrsa -des3 -out server.key 1024 运行时会提示输入密码,此密码用于加 ...
- mysql中用命令行复制表结构(数据)
mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 或 CREATE TABLE 新表 ...
- Jmeter之命令行生成HTML报告
其实每次使用jemter.bat文件启动JMeter时,命令行窗口都会提示我们不要使用GUI窗口进行测试,除非是进行调试脚本 使用命令行生成结果也很测试报告也很简单 jmeter -n -t [jmx ...
随机推荐
- JavaScript对象与JSON字符串的相互转换
JSON(JavaScript Object Notation) 是JavaScript编程语言的一个子集.正因JSON是JavaScript的一个子集,所以它可清晰的运用于此语言中. eval函数 ...
- vue教程3-07 vue-loader
vue-loader: vue-loader: 其他loader -> css-loader.url-loader.html-loader..... 后台: nodeJs -> requi ...
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)
原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想) 总体思想: 希望让调用方通过 http调用传入一个需要生成图片的网页链接生成一个网页的图片并返回图片链接 ...
- CentOS 7.5 安装与配置 Percona Server 5.7
个人比较喜欢 MYSQL 的轻量,今天花了一点时间把阿里云上的 MYSQL5.7 换成了 Percona-Server .Percona 是一个开源的 MySQL 衍生版,TokuDB 的数据库引擎使 ...
- 03-01:springboot 整合jsp
1.修改pom文件,添加坐标 <!-- jstl --> <dependency> <groupId>javax.servlet ...
- getFields和getDeclaredFields
getFields()获得某个类的所有的公共(public)的字段,包括父类. getDeclaredFields()获得某个类的所有申明的字段,即包括public.private和proteced, ...
- MVC源码分析 - Action/Result 过滤器(续)
上一篇 看到了Action/Result过滤器的执行顺序: OnActionExecuting -> Action -> OnActionExecuted -> OnResultEx ...
- postgresql逻辑结构--用户及权限管理(七)
一.用户和角色 二.创建用户和角色 三.权限管理 四.
- WPF绑定的ListBox获取ListBoxItem及GoToState应用
现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...
- 【详解】ThreadPoolExecutor源码阅读(一)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 工作原理简 ...