php command line to generate project structure

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';
        }

    }

?>

usage:

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

 

Use classes to individually transform

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, because ghost_frame is called, you need to require this file in ghostinit.php

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

 Of course we can use autoloading to transform

First, set up the directory structure of the framework, similar to 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

complete 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 ) );
            //Introducing the namespace with use does not need to add the namespace to instantiate the class every time 
            $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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026337&siteId=291194637