分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

php命令行生成项目结构

发布时间:2023-09-06 01:51责任编辑:彭小芳关键词:暂无标签

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$ lsghost ?ghostinit.phpghostwu@dev:~/php/php1/10$ ./ghost initpls input project name?hellopls input author?ghostwustring(6) "hello"string(8) "ghostwu"39 bytes has written,config file has createdghostwu@dev:~/php/php1/10$ lsghost ?ghostinit.php ?go.jsonghostwu@dev:~/php/php1/10$ ./ghost startghostwu@dev:~/php/php1/10$ lsghost ?ghostinit.php ?go.json ?helloghostwu@dev:~/php/php1/10$ tree hellohello└── index.php0 directories, 1 fileghostwu@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$ lscore ?ghost ?ghostinit.php ?go.json ?helloghostwu@dev:~/php/php1/10$ tree corecore└── 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变成v12 ????????$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

php命令行生成项目结构

原文地址:https://www.cnblogs.com/ghostwu/p/8970277.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved