Model Exist Or Not In Yii Using A Custom Function - Performance

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 ?

Scanning the folder(s) is slow so some performance hit is inevitable.

One possible solution that I can think of is to have a list of allowable models and to check with that list, yes the list would be fixed and every time that you would add/delete some models you would need to update that list too… but regarding performance there is no comparison with scanning the folders…

You can also cache the list of model names, but I would just update that list manually.

Thank you very much :)

All those lines of codes are minimized to this now :)


    /**

 	* Checks for a given model name

 	* @param string $mode is the name of the model that is used to search against the array.

 	* @return String $result, the name of Model. if doesnt exist NULL;

 	*/

    protected function getModelName($name)

    {

        $result = null;

        foreach ($this->modelList as $model) {

            if (strtolower($name) == strtolower($model)) {

                $result = $model;

                break;

            }

        }

        return $result;

    }

it can be even shorter like


if(in_array(strtolower($name), $this->modelList))

[size=“2”]The modelList should already be in all lowercase ;)[/size]

Thanks.

This is my ModelList,


public $modelList = array('ActivityLog', 'AuthAssignment', 'AuthItem', 'AuthItemChild', 'City', 'Class',

        'Configuration', 'Country', 'Device', 'DeviceMake', 'DeviceModel', 'DeviceType', 'DeviceVersion',

        'Grade', 'Institute', 'Parent', 'PostalCode', 'State', 'Student', 'Teacher', 'User', 'Post');

why i am looping through the array is, i need to get the matching name from Model list array and then use it as below.


   $m = TK::get('model');

   $m = $this->getModelName($m);


   $model = $m::model()->findAll($criteria);

so i dont think lower case values in ModelList array would help me.

so this allows, http://localhost/RestApi/index.php/api/User or http://localhost/RestApi/index.php/api/[size="2"]USER or [/size]http://localhost/RestApi/index.php/api/user but not

http://localhost/RestApi/index.php/api/users

Let me know if i am doing anything wrong.

Thanks again M domba. :)

Your code is fine, no need to complicate with in_array