File Check API
Overview
File check methods for use with the AutoLoader API.
existing(...)
Returns an array of filenames that exist in the file map. Accepts any number of arguments of which can be filename or array of filenames. If no files exist, empty array is returned.
$files = SugarAutoloader::existing('include/utils.php', 'include/TimeDate.php');
existingCustom(...)
This method accepts any number of arguments, each of which can be filename or array of filenames. It will return an array of filenames that exist in the file map, adding also files that exist when custom/ is prepended to them. If the original filename already had custom/ prefix, it is not prepended again. custom/ files are added to the list after the root directory files so that if included in order, they will override the data of the root file. If no files exist, empty array is returned.
$files = SugarAutoloader::existingCustom('include/utils.php', 'include/TimeDate.php');
existingCustomOne(...)
Returns the last file of the result returned by existingCustom(), or null if none exist. Accepts any number of arguments of which can be filename or array of filenames. Since customized files are placed after the root files, it will return customized file if exists, otherwise root file.
$files = SugarAutoloader::existingCustomOne('include/utils.php');
You should note that the existingCustomOne() method can be used for loading inline PHP files. An example is shown below:
foreach(SugarAutoLoader::existingCustomOne('custom/myFile.php') as $file)
{
include $file;
}
Alternative to including inline PHP files, loading class files should be done using requireWithCustom() .
fileExists($filename)
Checks if a file exists in the file map. You should note that ".." is not supported by this function and any paths including ".." will return false. The path components should be separated by /. You should also note that multiple slashes are compressed and treated as single slash.
$file = 'include/utils.php';
if (SugarAutoloader::fileExists($file))
{
require_once($file);
}
getDirFiles($dir, $get_dirs = false, $extension = null)
Retrieves the list of files existing in the file map under the specified directory. If no files are found, the method will return an empty array. By default, the method will return file paths, however, If $get_dirs is set to true, the method will return only directories. If $extension is set, it would return only files having that specific extension.
$files = SugarAutoloader::getDirFiles('include');
getFilesCustom($dir, $get_dirs = false, $extension = null)
Retrieves the list of files existing in the file map under the specified directory and under it's custom/ path. If no files are found it will return empty array. By default, the method will return file paths, however, If $get_dirs is set to true, the method will return only directories. If $extension is set, it would return only files having that specific extension.
$files = SugarAutoloader::getFilesCustom('include');
lookupFile($paths, $file)
$paths = array(
'include',
'modules',
);
$files = SugarAutoloader::lookupFile($paths, 'utils.php');
requireWithCustom($file, $both = false)
If a custom/ override of the file or the file exist, require_once it and return true, otherwise return false. If $both is set to true, both files are required with the root file being first and custom/ file being second. Unlike other functions, this function will actually include the file.
$file = SugarAutoloader::requireWithCustom('include/utils.php');
You should note that the requireWithCustom() method should be used for loading class files and not inline PHP files. Inline PHP files should be loaded using the existingCustomOne() method.