php 档案,PHP 档案包 (PHAR)
PHP 檔案包 (PHAR)
要獲取 PHPUnit,最簡單的方法是下載 PHPUnit 的 PHP 檔案包 (PHAR),它將 PHPUnit 所需要的所有必要組件(以及某些可選組件)捆綁在單個文件中:
要使用 PHP檔案包(PHAR)需要有 phar 擴展。
要使用 PHAR 的 --self-update 功能需要有 openssl 擴展。
如果啟用了 Suhosin 擴展,需要在 php.ini 中允許執行 PHAR:
suhosin.executor.include.whitelist = phar
Note
要從 https://phar.phpunit.de/ 下載,需要支持 TLS/SNI的客戶端,例如 wget 1.14(或更高版本)。
如果要全局安裝 PHAR:
$ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ sudo mv phpunit.phar /usr/local/bin/phpunit $ phpunit --version PHPUnit x.y.z by Sebastian Bergmann and contributors.
也可以直接使用下載的 PHAR 文件:
$ wget https://phar.phpunit.de/phpunit.phar $ php phpunit.phar --version PHPUnit x.y.z by Sebastian Bergmann and contributors.
Windows
整體上說,在 Windows 下安裝 PHAR 和手工在 Windows 下安裝 Composer 是一樣的過程:
為 PHP 的二進制可執行文件建立一個目錄,例如 C:bin
將 ;C:bin 附加到 PATH 環境變量中(相關幫助)
下載 https://phar.phpunit.de/phpunit.phar 并將文件保存到 C:binphpunit.phar
打開命令行(例如,按 Windows+R ? 輸入 cmd ? ENTER)
建立外包覆批處理腳本(最后得到 C:binphpunit.cmd):
C:Usersusername> cd C:bin C:bin> echo @php "%~dp0phpunit.phar" %* > phpunit.cmd C:bin> exit
新開一個命令行窗口,確認一下可以在任意路徑下執行 PHPUnit:
C:Usersusername> phpunit --version PHPUnit x.y.z by Sebastian Bergmann and contributors.
對于 Cygwin 或 MingW32 (例如 TortoiseGit) shell 環境,可以跳過第五步。 取而代之的是,把文件保存為 phpunit (沒有 .phar 擴展名),然后用 chmod 775 phpunit 將其設為可執行。
校驗 PHPUnit PHAR 發行包
由 PHPUnit 項目分發的所有官方代碼發行包都由發行包管理器進行簽名。在 phar.phpunit.de 上有 PGP 簽名和 SHA1 散列值可用于校驗。
下面的例子詳細說明了如何對發行包進行校驗。首先下載 phpunit.phar 和與之對應的單獨 PGP 簽名 phpunit.phar.asc:
wget https://phar.phpunit.de/phpunit.phar wget https://phar.phpunit.de/phpunit.phar.asc
用單獨的簽名(phpunit.phar)對 PHPUnit 的 PHP 檔案包(phpunit.phar.asc)進行校驗:
gpg phpunit.phar.asc gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20A gpg: Can't check signature: public key not found
在本地系統中沒有發行包管理器的公鑰(6372C20A)。為了能進行校驗,必須從某個密鑰服務器上取得發行包管理器的公鑰。其中一個服務器是 pgp.uni-mainz.de。所有密鑰服務器是鏈接在一起的,因此連接到任一密鑰服務器都可以。
gpg --keyserver pgp.uni-mainz.de --recv-keys 0x4AA394086372C20A gpg: requesting key 6372C20A from hkp server pgp.uni-mainz.de gpg: key 6372C20A: public key "Sebastian Bergmann " imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
現在已經取得了條目名稱為”Sebastian Bergmann sb@sebastian-bergmann.de”的公鑰。不過無法檢驗這個密鑰確實是由名叫 Sebastian Bergmann 的人創建的。但是可以先試著校驗發行包的簽名:
gpg phpunit.phar.asc gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20A gpg: Good signature from "Sebastian Bergmann " gpg: aka "Sebastian Bergmann " gpg: aka "Sebastian Bergmann " gpg: aka "Sebastian Bergmann " gpg: aka "Sebastian Bergmann " gpg: aka "[jpeg image of size 40635]" gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner. Primary key fingerprint: D840 6D0D 8294 7747 2937 7831 4AA3 9408 6372 C20A
此時,簽名已經沒問題了,但是這個公鑰還不能信任。簽名沒問題意味著文件未被篡改。可是由于公鑰加密系統的性質,還需要再校驗密鑰 6372C20A 確實是由真正的 Sebastian Bergmann 創建的。
任何攻擊者都能創建公鑰并將其上傳到公鑰服務器。他們可以建立一個帶惡意的發行包,并用這個假密鑰進行簽名。這樣,如果嘗試對這個損壞了的發行包進行簽名校驗,由于密鑰是“真”密鑰,校驗將成功完成。因此,需要對這個密鑰的真實性進行校驗。如何對公鑰的真實性進行校驗已經超出了本文檔的范疇。
有個比較謹慎的做法是創建一個腳本來管理 PHPUnit 的安裝,在運行測試套件之前校驗 GnuPG 簽名。例如:
#!/usr/bin/env bash clean=1 # 是否在測試完成之后刪除 phpunit.phar ? aftercmd="php phpunit.phar --bootstrap bootstrap.php src/tests" gpg --fingerprint D8406D0D82947747293778314AA394086372C20A if [ $? -ne 0 ]; then echo -e " 33[33mDownloading PGP Public Key... 33[0m" gpg --recv-keys D8406D0D82947747293778314AA394086372C20A # Sebastian Bergmann gpg --fingerprint D8406D0D82947747293778314AA394086372C20A if [ $? -ne 0 ]; then echo -e " 33[31mCould not download PGP public key for verification 33[0m" exit fi fi if [ "$clean" -eq 1 ]; then # 如果存在就清理掉 if [ -f phpunit.phar ]; then rm -f phpunit.phar fi if [ -f phpunit.phar.asc ]; then rm -f phpunit.phar.asc fi fi # 抓取最新的發行版和對應的簽名 if [ ! -f phpunit.phar ]; then wget https://phar.phpunit.de/phpunit.phar fi if [ ! -f phpunit.phar.asc ]; then wget https://phar.phpunit.de/phpunit.phar.asc fi # 在運行前先校驗 gpg --verify phpunit.phar.asc phpunit.phar if [ $? -eq 0 ]; then echo echo -e " 33[33mBegin Unit Testing 33[0m" # 運行測試套件 `$after_cmd` # 清理 if [ "$clean" -eq 1 ]; then echo -e " 33[32mCleaning Up! 33[0m" rm -f phpunit.phar rm -f phpunit.phar.asc fi else echo chmod -x phpunit.phar mv phpunit.phar /tmp/bad-phpunit.phar mv phpunit.phar.asc /tmp/bad-phpunit.phar.asc echo -e " 33[31mSignature did not match! PHPUnit has been moved to /tmp/bad-phpunit.phar 33[0m" exit 1 fi
總結
以上是生活随笔為你收集整理的php 档案,PHP 档案包 (PHAR)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 市值暴跌超万亿,苹果急需AI大模型「新解
- 下一篇: 打砖块小游戏php程序,利用原生js实现