00001 <?php
00031 class ormMapParser extends zCore{
00032 var $__map_file_name;
00033 var $__struct;
00034
00035 function ormMapParser(){
00036 $this->__construct();
00037 }
00038 function __construct(){
00039 parent::__construct();
00040 $this->__struct = new stdClass();
00041 }
00042 function parse_var_list($str){
00043 trim(&$str);
00044 if(strlen($str) < 3){
00045 return false;
00046 }
00047 $carry_char = '';
00048 $__list = array();
00049 for($offset = 0;$offset<strlen($str);$offset++){
00050 switch(true){
00051 case ($str[$offset] == ':'):
00052 $current_token = trim($carry_char.$str[$offset], "\r\n\t :");
00053 $carry_char = '';
00054 break;
00055 case ($str[$offset] == "\r"):
00056 case ($str[$offset] == "\n"):
00057 case ($offset == strlen($str)-1):
00058 $__list[$current_token] = trim($carry_char.$str[$offset], "\r\n\t :");
00059 $carry_char = '';
00060 break;
00061 default:
00062 $carry_char .= $str[$offset];
00063 }
00064 }
00065 return $__list;
00066 }
00067 function explode_token($token_str){
00068 trim(&$token_str);
00069 $__token = array();
00070 $tmp = preg_split('~\s+~', $token_str);
00071 if(count($tmp) < 2){
00072 perror('EROOER PARSING ORM Map on file '.$this->__map_file_name.' near <br />'.$token_str.'<br />ELEMENT Must be in `STRING as STRING` format');
00073 }
00074 $__token['type'] = $tmp[0];
00075 switch($__token['type']){
00076 case 'connection':
00077 $__token['alias'] = $tmp[count($tmp)-1];
00078 break;
00079 case 'table':
00080 $__token['element'] = $tmp[1];
00081 $__token['alias'] = $tmp[count($tmp)-1];
00082 break;
00083 case 'readonly':
00084 case 'writable':
00085 $__token['type'] = 'attribute';
00086 $__token['permession'] = $tmp[0];
00087 $__token['element'] = $tmp[1];
00088 $__token['alias'] = $tmp[count($tmp)-1];
00089 break;
00090 default:
00091 perror('EROOER PARSING ORM Map on file '.$this->__map_file_name.' near <br />'.$token_str.'<br />Unexpected TOKEN TYPE '.$__token['type']);
00092 }
00093 return $__token;
00094 }
00103 function parseMap($map){
00104 if(!file_exists($map)){
00105 perror('ERROR While Parsing the ORM Map The File '.$map.' Doesn\'t exists');
00106 exit;
00107 }
00108 $this->__map_file_name = $map;
00109 $handle = fopen($map, "r");
00110 $buffer = '';
00111 $offset = 0;
00112 $previous_char = null;
00113 $pstk = array();
00114 $carrier = '';
00115 $__token = array();
00116 $__tree = array();
00117 $p = 0;
00118
00119 while(!feof($handle)){
00120 $buffer = fread($handle, 1);
00121
00122 switch($buffer){
00123 case '{':
00124 $pstk[trim($offset-strlen($offset))] = trim($carrier);
00125 $tmp = array_keys($pstk);
00126 $__tree[$p]['self'] = $pstk[$tmp[count($tmp)-1]];
00127 if(isset($tmp[count($tmp)-2]) && isset($pstk[$tmp[count($tmp)-2]])){
00128 $__tree[$p]['parent'] = $pstk[$tmp[count($tmp)-2]];
00129 }else{
00130 $__tree[$p]['parent'] = false;
00131 }
00132 $p++;
00133 $carrier = '';
00134 break;
00135 case '}':
00136 $__token[trim(array_pop($pstk))] = trim($carrier);
00137 $carrier = '';
00138 break;
00139 default:
00140 $carrier .= $buffer;
00141 }
00142
00143 $offset++;
00144 $previous_char = $buffer;
00145 }
00146
00147 $__struct = array();
00148 $__uid = array();
00149 $__cptr = array();
00150 $p = 0;
00151 $t = 0;
00152 foreach($__tree as $lxr){
00153 $self = &$lxr['self'];
00154 $parent = &$lxr['parent'];
00155 if(!$parent){
00156 $__struct[$p] = array('token' => $this->explode_token($self), 'value' => $this->parse_var_list($__token[$self]), 'child' => false);
00157 $__uid[$self] = $t;
00158 $__cptr[$t] = &$__struct[$p]['child'];
00159 ++$p;
00160 }else{
00161 $p_id = $__uid[$parent];
00162 $__cptr[$p_id][] = array('token' => $this->explode_token($self), 'value' => $this->parse_var_list($__token[$self]), 'child' => false);
00163 $__uid[$self] = $t;
00164 }
00165 $t++;
00166 }
00167 fclose($handle);
00168 return $__struct;
00169 }
00177 function loadMap($map_path){
00178 $__token_table = $this->parseMap($map_path);
00179
00180 if($__token_table[0]['token']['type'] != 'connection'){
00181 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' Unexpected Token `'.$__token_table[0]['token']['type'].'` at the Begening');
00182 }
00183 $this->__struct->connection->alias = $__token_table[0]['token']['alias'];
00184
00185
00186
00187
00188
00189 for($i=1;$i<count($__token_table);$i++){
00190 if($__token_table[$i]['token']['type'] != 'table'){
00191 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' Unexpected Token `'.$__token_table[$i]['token']['type'].'` at '.$i.' Parent Node <br />Expecting table');
00192 exit;
00193 }
00194 $this->__struct->tables->$__token_table[$i]['token']['element'] = new stdClass();
00195 $link = &$this->__struct->tables->$__token_table[$i]['token']['element'];
00196 $link->alias = $__token_table[$i]['token']['alias'];
00197
00198 if(!is_array($__token_table[$i]['child'])){
00199 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' No Child Attributes Found For Table '.$__token_table[$i]['token']['element']);
00200 exit;
00201 }
00202 $link->Attributes = array();
00203 foreach($__token_table[$i]['child'] as $j => $child){
00204 if($child['token']['type'] != 'attribute'){
00205 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' Unexpected Token '.$child['token']['type'].' Expecting attribute near '.$child['token']['permession'].' '.$child['token']['element'].' as '.$child['token']['alias']);
00206 exit;
00207 }
00208 $link->Attributes[$child['token']['element']] = new stdClass();
00209 $link->Attributes[$child['token']['element']]->alias = $child['token']['alias'];
00210 $link->Attributes[$child['token']['element']]->permission = $child['token']['permession'];
00211
00212 switch($child['token']['permession']){
00213 case 'readonly':
00214 if(is_array($child['value'])){
00215 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' a Read only Field `'.$child['token']['element'].'` can not Contain Criteria cause No data is going to be written on that Field By the Layer or By you');
00216 exit;
00217 }
00218 break;
00219 case 'writable':
00220 if(is_array($child['value'])){
00221 $link->Attributes[$child['token']['element']]->criteria = array();
00222 foreach($child['value'] as $criteria => $err_msg){
00223 if($criteria == 'required' && isset($link->Attributes[$child['token']['element']]->default)){
00224 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.'<br />You cannot use a default Value if you use required as a criteria Use optional criteria Instead<br />near<br />'.$criteria.': '.$err_msg.' under Attribute '.$child['token']['element']);
00225 }
00226 if($criteria != '__default'){
00227 $link->Attributes[$child['token']['element']]->criteria[$criteria] = $err_msg;
00228 }else{
00229 $link->Attributes[$child['token']['element']]->default = $err_msg;
00230 }
00231 }
00232 }
00233 break;
00234 default:
00235 perror('ERROR While Parsing the ORM Map on file '.$this->__map_file_name.' Unexpected Permission Type '.$child['token']['permession']);
00236 exit;
00237 }
00238
00239 }
00240
00241 }
00242
00243 }
00244 }
00245
00246 ?>