我正在實現一個名爲attach的函數。我正在尋找一種方法來取得一個文件路徑或文件內容的參數。以下是我的:區分可以採用PHP中的文件內容或文件路徑的參數
/**
* @param name - name of the file
* @param file - either a file or a file path
*/
function attach($name, $file) {
$attachment = array();
$attachment['name'] = $name;
if(map($file)) {
$attachment['filepath'] = $file;
$attachment['file'] = file_get_contents($file);
} else {
$attachment['file'] = $file;
$attachment['filepath'] = getcwd();
}
}
/**
* @param filepath - can take multiple forms
* ie. ui:form:text.css => ui/form/text.css
* text.css => getcwd().'text.css'
* /ui/form/text.css => /ui/form/text.css
*
* @return if file exists - return file path
* if not found - return false
*/
function map($filepath) {
// ... too long to post
}
map函數允許您將名稱空間(使用「:」)轉換爲文件路徑。
我擔心的問題是,如果在文件路徑中發生錯誤(即某人在文件路徑中輸入錯誤),我不希望它認爲由於文件不存在,它必須是文件內容
另外:如果可能,我寧願不編輯map()
,因爲它會要求我改變一堆代碼 - 考慮地圖作爲一個黑匣子。
最後:我把這個例子放在一起很快 - 所以請不要討論getcwd()
和其他語法問題的缺點。我在map()
謝謝! Matt Mueller
嗯,不幸的是,這是我的想法。我希望有些東西是我用文件資源或其他東西忽略的......我寧願只是不向API添加批量,但第三個參數不是太糟糕的選擇。 – Matt 2010-06-22 13:53:35
那麼,你也可以要求它們傳遞文件指針資源而不是文件路徑字符串。然後,由內部命名空間轉換爲文件路徑(使用助手方法),並在調用attach()之前對它們進行初始化。然後,你可以檢查類型。 – jmans 2010-06-22 13:57:56
然後再次 - 這可能是有風險的。您可能希望能夠驗證路徑。 – jmans 2010-06-22 13:58:55