PHP_Beautifier 安裝與使用
最近都在找一些可以讓程式碼好看,讓文件好作,讓事情變得輕鬆愉快的套件或是模組。
記得前一陣子在 讓程式碼好看些 - PHP_Beautifier 有看到 PHP_Beautifier 所以就拿來玩玩看囉!
發現真是個很有趣很不錯的東西,安裝後可以直接在 shell 中將程式碼統一格式,是非常方便又實用的一個套件。
系統需求?
PHP_Beautifier 需要在 php5 的環境下執行!
但是,可以處理的是 php3, php4, php5 的檔案,基本上他是對排版作調整,並不會去調整語法。
安裝方式?
使用 pear 安裝
先確認 pear 的 channel 都更新了..
pear update-channels
然後直接使用 pear install 安裝,這裡要注意的是 PHP_Beautifier 目前還是 beta 版..
pear install PHP_Beautifier-beta
若是正式版的話只要 pear install PHP_Beautifier 即可.
接下來程式會跑跑跑,自動下載相依的 pear 模組,然後就會安裝完成.(意想不到的簡單吧!)
如何使用?
由於他會自動多一個 php_beautifier 的 shell script 在 /usr/bin 中,所以只要直接執行 php_beautifier 就好囉!
先來看一下他的 help
php_beautifier --help
Usage: php_beautifier [options] <infile> <out>
<infile> and/or <out> can be '-', which means stdin/stdout.
you can use ? and * for batch processing
<out> can be a dir (ending with '/' or a real dir)
or a file (without '/')
multiple ins and one out = merge all files in one output
Options:
--input or -f <file> input file - default: stdin
--output or -o <out> output dir or file - default: stdout
--indent_tabs or -t <int> indent with tabs
--indent_spaces or -s <int> indent with spaces - default
--filters or -l <fil_def> Add filter(s)
--directory_filters or -d <path> Include dirs for filters
--compress or -c <type> Compress output
--recursive or -r Search in subdir recursively
--help or -? display help/usage (this message)
Filter definition:
--filters "Filter1(setting1=value1,setting2=value2) Filter2()"
執行的時候通常看你自己的習慣是什麼,通常主要是 -t -s 兩個要選一個用,預設使用的是 -s 也就是所有的縮排都是用『空白』,而 -t 則就是縮排用的是『TAB』
另外會用到的通常還有 -r 這個遞迴參數,這樣才能同時連子目錄下面的一起處理。
你可以這樣試試
php_beautifier -t -r mylibs/* new_mylibs/
這樣就會把 mylibs 底下所有的檔案進行一次 php_beautifier 並且輸出到 new_mylibs 下。





