php命令行生成项目结构
2018-06-22 05:39:11来源:未知 阅读 ()
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$
用类来单独改造
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'; } } ?>
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/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'; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash