/** * __getSenchaModel($fileModel): * * This method is used by SenchaModel method to get all the table and column * information inside the Sencha Model .js file * It also tries to load the model from memory if does not exist parse the * model file and store it. * * @param string $fileModel 'App.model.Example' * @return array|bool model array or failure * */ public static function __getSenchaModel($fileModel) { try { // Getting Sencha model as a namespace $jsSenchaModel = self::__getFileContent($fileModel); if (!$jsSenchaModel) { throw new Exception("Error opening the Sencha model file."); } // remove functions at the end if (preg_match("/(?<!convert\\: )function\\([\r\n\t\\w \\-!\$%^&*()_+|~=`\\[\\]:\";'<>?,.\\/{}]*/", $jsSenchaModel)) { $jsSenchaModel = preg_replace("/(?<!convert\\: )function\\([\r\n\t\\w \\-!\$%^&*()_+|~=`\\[\\]:\";'<>?,.\\/{}]*/", '" "', $jsSenchaModel) . PHP_EOL . '});'; } // get the actual Sencha Model. preg_match('/Ext\\.define\\([a-zA-Z0-9\',. ]+(?P<extmodel>.+)\\);/si', $jsSenchaModel, $match); $jsSenchaModel = $match['extmodel']; // clean convert functions with false $jsSenchaModel = preg_replace('/function\\(.*\\){([\\s\\S])*?},/', "false },", $jsSenchaModel); // unnecessary Ext.define functions $jsSenchaModel = preg_replace('/(?P<spaces>[\\t\\n\\r ])|(?P<sencha>[\\d(),.:;A-Za-z{}]+?)|(?P<properties>\'[^\\n\\r\']+\')/', '$2$3', $jsSenchaModel); // add quotes to proxy Ext.Direct functions //$jsSenchaModel = preg_replace("/(read|create|update|destroy)([:])((\w|\.)*)/", "$1$2'$3'", $jsSenchaModel); // wrap with double quotes to all the properties $jsSenchaModel = preg_replace('/(,|\\{)(\\w*):/', "\$1\"\$2\":", $jsSenchaModel); // wrap with double quotes float numbers $jsSenchaModel = preg_replace("/([0-9]+\\.[0-9]+)/", "\"\$1\"", $jsSenchaModel); // replace single quotes for double quotes // TODO: refine this to make sure doesn't replace apostrophes used in comments. example: don't $jsSenchaModel = preg_replace("(')", '"', $jsSenchaModel); $model = (array) json_decode($jsSenchaModel, true); if (!count($model)) { throw new Exception("Something went wrong converting it to an array. Model('{$fileModel}'). JSON Error: " . self::__JSONErrorTranslate(json_last_error())); } // check if there are a defined table from the model if (!isset($model['table'])) { throw new Exception("Table property is not defined on Sencha Model. 'table:'"); } // check if there are a defined fields from the model if (!isset($model['fields'])) { throw new Exception("Fields property is not defined on Sencha Model. 'fields:'"); } if (!MatchaMemory::__storeSenchaModel($fileModel, $model)) { throw new Exception("Error storing sencha model into memory."); } return $model; } catch (Exception $e) { MatchaErrorHandler::__errorProcess($e); return false; } }
/** * __getSenchaModel($fileModel): * * This method is used by SenchaModel method to get all the table and column * information inside the Sencha Model .js file * It also tries to load the model from memory if does not exist parse the * model file and store it. * * @param string $fileModel 'App.model.Example' * @param null $instance * @return array|bool model array or failure * */ public static function __getSenchaModel($fileModel, $instance = null) { try { // Getting Sencha model as a namespace $jsSenchaModel = self::__getFileContent($fileModel); if (!$jsSenchaModel) { throw new Exception("Error opening the Sencha model file."); } $jsSenchaModel = self::__CleanSenchaModel($jsSenchaModel); $model = (array) json_decode($jsSenchaModel, true); if (isset($instance)) { if (is_array($model['table'])) { $model['table']['name'] .= '_' . $instance; } else { $model['table'] .= '_' . $instance; } } if (!count($model)) { throw new Exception("Something went wrong converting it to an array. Model('{$fileModel}'). JSON Error: " . self::__JSONErrorTranslate(json_last_error())); } // check if there are a defined table from the model if (!isset($model['table'])) { throw new Exception("Table property is not defined on Sencha Model. 'table:'"); } // check if there are a defined fields from the model if (!isset($model['fields'])) { throw new Exception("Fields property is not defined on Sencha Model. 'fields:'"); } if (!MatchaMemory::__storeSenchaModel($fileModel, $model, $instance)) { throw new Exception("Error storing sencha model into memory."); } return $model; } catch (Exception $e) { MatchaErrorHandler::__errorProcess($e); return false; } }