I am writing an REST API and need to check weather the given model name exist or not in the directory.
1 - http://example.com/R…php/api/posts/
2 - http://example.com/R...x.php/api/post/
from these two, URL 1 is incorrect and URL 2 is correct (post). so i am taking this param and doing a search as following,
$model = $m::model()->findAll($criteria);
$m = TK::get('model');
when $m is incorrect, PHP warning include(posts.php): failed to open stream: No such file or directory will trigger.
so for that not to happen, i have written a function as modelExists() and using it like below.
If (!TK::modelExists($m))
$this->sendResponse(false, 1003);
the function body is as below,
/**
* Checks for a given model name
* @param string $modelName is the name of the model that is used to search against the directory.
* @return String $result, the name of Model. if doesnt exist NULL;
*/
public static function modelExists($modelName)
{
$result = null;
$basePath = Yii::getPathOfAlias('application').DIRECTORY_SEPARATOR;
$modelsDir = 'models'.DIRECTORY_SEPARATOR;
$modelName = strtolower($modelName).'.php';
$generalModelDir = scandir($basePath.$modelsDir);
foreach ($generalModelDir as $entry) { // Searching in General model directory
if ($modelName == strtolower($entry)) {
$temp = explode('.', $entry); // array('User','php')
$result = $temp[0];
break;
}
}
if (!$result) {
$modulePath = $basePath.'modules'.DIRECTORY_SEPARATOR;
$moduleDirectory = scandir($modulePath);
foreach ($moduleDirectory as $dir) {
$subModuleDirectory = scandir($modulePath.$dir);
foreach ($subModuleDirectory as $entry) {
if (is_dir($modulePath.$dir.DIRECTORY_SEPARATOR.$entry)) {
$directories = scandir($modulePath.$dir.DIRECTORY_SEPARATOR.$entry);
foreach ($directories as $subDir) {
if ($modelName == strtolower($subDir)) {
$temp = explode('.', $subDir); // array('User','php')
$result = $temp[0];
break;
}
}
}
}
}
}
return $result;
}
My Question is : Can this cause any performance issues since i am checking this for every API call ?