| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace crocodicstudio\crudbooster\Modules\ModuleGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class ScaffoldingParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     static function parse($code, $type = 'form') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         $colsItem = self::extractLines($code, $type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         foreach ($colsItem as &$item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |             $item = str_replace(' ','', $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |             $item = str_replace('\',]',']', $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |             $item = trim($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             $item = trim($item, '['); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |             $item = trim($item, '];'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             $item = trim($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             $item = trim(preg_replace("/[\n\r\t]/", "", $item)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             $strSplit = str_split($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             $innerCount = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             foreach ($strSplit as $index => $s) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                 if ($s == '[') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                     $innerCount++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 if ($s == ']') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                     $innerCount--; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     $strSplit[$index] = "|SPLIT|"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $item = implode("", $strSplit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         foreach ($colsItem as &$col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $colInnerItem = self::prepareFields(explode('|SPLIT|', $col)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $col = $colInnerItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self::formOptions($type, $colsItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $colsItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private static function extractLines($code, $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($type == 'form') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $cols = extract_unit($code, "# START FORM DO NOT REMOVE THIS LINE", "# END FORM DO NOT REMOVE THIS LINE"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $cols = str_replace('"', "'", $cols); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $cols = trim(str_replace('$this->form = [];', '', $cols)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $colsItem = explode('$this->form[] = ', $cols); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } elseif ($type == 'col') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $cols = extract_unit($code, "# START COLUMNS DO NOT REMOVE THIS LINE", "# END COLUMNS DO NOT REMOVE THIS LINE"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $cols = str_replace('"', "'", $cols); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $cols = trim(str_replace('$this->col = [];', '', $cols)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $colsItem = explode('$this->col[] = ', $cols); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $colsItem = array_filter($colsItem); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $colsItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param $colsItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     private static function formOptions($type, $colsItem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($colsItem as &$form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             if ($type !== 'form') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if ($form['options']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 @eval("\$options = $form[options];"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 @$form['options'] = $options; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $form['options'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param $s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     private static function parseCallback($s) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $s = str_replace("return", "return ", $s); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s)); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $val = substr($val, 0, -1); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return $val; //to remove last } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param $split | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private static function prepareFields($split) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $colInnerItem = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         foreach ($split as $s) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             if (strpos($s, 'options') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $colInnerItem['options'] = trim(str_replace("'options'=>", "", $s)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } elseif (strpos($s, 'callback') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $colInnerItem['callback'] = self::parseCallback($s); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 $s = trim($s, "'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $sSplit = explode('=>', $s); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $colInnerItem[$sSplit[0]] = $sSplit[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return $colInnerItem; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | } | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.