Hey all,
I'm implementing a function called attach. I'm looking for a way to take a param that is either a file path or a file's contents. Here's what I have:
/**
* @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
}
The map function allows you to turn namespaces (using ":") into filepaths.
The issue I'm worried about is if an error is made in the filepath (ie. someone types in the file path wrong) I don't want it to think that since the file doesn't exist, it must be file contents
Also: if possible, i'd rather not edit map()
as it would require me to change a bunch of code - consider map as a black box.
Finally: I put this example together quickly - so please do not discuss the shortcomings of getcwd()
, and other syntactical issues. I have a more elaborate system in place in map()
Thanks! Matt Mueller