php命令行生成項目結構

来源:https://www.cnblogs.com/ghostwu/archive/2018/04/29/8970277.html
-Advertisement-
Play Games

ghostinit.php 用法: ghostwu@dev:~/php/php1/10$ ls ghost ghostinit.php ghostwu@dev:~/php/php1/10$ ./ghost init pls input project name? hello pls input au ...


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

    }

?>
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變成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';
        }

    }
View Code

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 動畫效果slideDown(100)下到上顯示slideToggle(100)上到下顯示slideUp(100)上到下隱藏fadeIn(100)淡淡顯示fadeOut(100)淡淡顯示fadeToggle()自動切換hide(100)右下角隱藏show(100)左上角顯示animate({},100 ...
  • 這幾種方式的搭配使用可以輕鬆搞定 PC 端頁面的常見需求,比如實現水平居中可以使用 margin: 0 auto,實現水平垂直同時居中可以如下設置: 然而,這些寫法都存在一些缺陷:缺少語義並且不夠靈活。我們需要的是通過 1 個屬性就能優雅的實現子元素居中或均勻分佈,甚至可以隨著視窗縮放自動適應。在這 ...
  • 最近在用electron開發PC桌面應用,其中有個需求就是整個應用以管理員許可權啟動。很頭痛,各種google,baidu。 最後終於解決了,可以分為三個步驟,做個總結分享。 一、如果沒有manifest.xml文件的話 可通過執行命令:mt.exe -inputresource:某某.exe -ou ...
  • 1.獲取一組radio單選框的值(name屬性為一組的radio的name屬性) var q1 = $("input[name=element_name]:checked").val(); 2.獲取select下拉框的值 var q2 = $("#element").val(); 3.獲取幾個下拉框 ...
  • 區塊類用 JavaScript 寫出來大致的樣子: 創造一個鏈Blockchain 類中將區塊鏈接起來 使用區塊鏈 區塊鏈是不可變的。一旦添加,區塊就不可能再變更了。在這裡可以試一下。 以上僅僅是一個簡單的區塊鏈工作原理 ...
  • js代碼 把代碼直接放到需要放的位置即可 效果 ...
  • 什麼是腳本語言? ①腳本語言介於HTML和C,C++,Java,C#等編程語言之間 ②腳本語言與編程語言有相似地方,其函數與編程語言類似,也有變數。與編程語言之間最大的區別是編程語言的語法和規則更為嚴格和複雜一些. ③腳本語言是一種解釋性語言,例如Python、vbscript,javascript ...
  • Nginx與瀏覽器緩存 一、瀏覽器對緩存的處理:Internet選項 ★ 控制請求伺服器策略:是忽略資源的緩存策略的情況下額外強制請求伺服器的意思。 ★ 檢查存儲的頁面較新版本 1.每次訪問網頁時 不管是否有緩存、資源狀態是否過期,都會再次請求伺服器。 2.每次啟動Internet Explorer ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...