| Total Complexity | 58 |
| Total Lines | 261 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Modules often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Modules, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Modules extends Module { |
||
| 12 | |||
| 13 | public function createBlankModule($name, $codeName) { |
||
| 14 | $codeName = ucfirst($codeName); |
||
| 15 | Tools::createDir(App::$primary->path . '/modules/' . $codeName); |
||
| 16 | ob_start(); |
||
| 17 | include $this->path . '/tpls/BlankModule.php'; |
||
| 18 | $moduleCode = ob_get_contents(); |
||
| 19 | ob_end_clean(); |
||
| 20 | file_put_contents(App::$primary->path . '/modules/' . $codeName . '/' . $codeName . '.php', $moduleCode); |
||
| 21 | file_put_contents(App::$primary->path . '/modules/' . $codeName . '/info.php', "<?php\nreturn " . CodeGenerator::genArray(['name' => $name])); |
||
| 22 | file_put_contents(App::$primary->path . '/modules/' . $codeName . '/generatorHash.php', "<?php\nreturn " . CodeGenerator::genArray([$codeName . '.php' => md5($moduleCode)])); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function parseColsForModel($cols = []) { |
||
| 26 | $modelCols = [ 'labels' => [], 'cols' => [], 'relations' => []]; |
||
| 27 | foreach ($cols as $col) { |
||
| 28 | $modelCols['labels'][$col['code']] = $col['label']; |
||
| 29 | $colType = !empty($col['type']['primary']) ? $col['type']['primary'] : $col['type']; |
||
| 30 | switch ($colType) { |
||
| 31 | case 'relation': |
||
| 32 | $relationName = Tools::randomString(); |
||
| 33 | $modelCols['cols'][$col['code']] = ['type' => 'select', 'source' => 'relation', 'relation' => $relationName, 'showCol' => 'name']; |
||
| 34 | $modelCols['relations'][$relationName] = [ |
||
| 35 | 'model' => $col['type']['aditional'], |
||
| 36 | 'col' => $col['code'] |
||
| 37 | ]; |
||
| 38 | break; |
||
| 39 | default: |
||
| 40 | $modelCols['cols'][$col['code']] = ['type' => $colType]; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | return $modelCols; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function parseColsForTable($cols, $colPrefix, $tableName) { |
||
| 47 | |||
| 48 | $colsExist = App::$cur->db->getTableCols($tableName); |
||
| 49 | $tableCols = []; |
||
| 50 | if (empty($colsExist[$colPrefix . 'id'])) { |
||
| 51 | $tableCols[$colPrefix . 'id'] = 'pk'; |
||
| 52 | } |
||
| 53 | foreach ($cols as $col) { |
||
| 54 | if (!empty($colsExist[$colPrefix . $col['code']])) { |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | $colType = !empty($col['type']['primary']) ? $col['type']['primary'] : $col['type']; |
||
| 58 | switch ($colType) { |
||
| 59 | case 'image': |
||
| 60 | case 'number': |
||
| 61 | case 'relation': |
||
| 62 | $tableCols[$colPrefix . $col['code']] = 'int(11) NOT NULL'; |
||
| 63 | break; |
||
| 64 | case 'decimal': |
||
| 65 | $tableCols[$colPrefix . $col['code']] = 'decimal(11,2) NOT NULL'; |
||
| 66 | break; |
||
| 67 | case 'dateTime': |
||
| 68 | $tableCols[$colPrefix . $col['code']] = 'timestamp NOT NULL'; |
||
| 69 | break; |
||
| 70 | case 'currentDateTime': |
||
| 71 | $tableCols[$colPrefix . $col['code']] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
||
| 72 | break; |
||
| 73 | case 'text': |
||
| 74 | $tableCols[$colPrefix . $col['code']] = 'varchar(255) NOT NULL'; |
||
| 75 | break; |
||
| 76 | case 'textarea': |
||
| 77 | default: |
||
| 78 | $tableCols[$colPrefix . $col['code']] = 'text NOT NULL'; |
||
| 79 | break; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | return $tableCols; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function generateModel($module, $name, $codeName, $options) { |
||
| 86 | $codeName = ucfirst($codeName); |
||
| 87 | $class = new CodeGenerator\ClassGenerator(); |
||
| 88 | $class->name = $codeName; |
||
| 89 | $class->extends = '\Model'; |
||
| 90 | $modelCols = $this->parseColsForModel(); |
||
| 91 | if (!empty($options['cols'])) { |
||
| 92 | $modelCols = $this->parseColsForModel($options['cols']); |
||
| 93 | $tableName = strtolower($module) . '_' . strtolower($codeName); |
||
| 94 | $tableCols = $this->parseColsForTable($options['cols'], strtolower($codeName) . '_', $tableName); |
||
| 95 | if (App::$cur->db->tableExist($tableName)) { |
||
| 96 | foreach ($tableCols as $colKey => $params) { |
||
| 97 | App::$cur->db->add_col($tableName, $colKey, $params); |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | App::$cur->db->createTable($tableName, $tableCols); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | $class->addProperty('objectName', $name, true); |
||
| 104 | $class->addProperty('cols', $modelCols['cols'], true); |
||
| 105 | $class->addProperty('labels', $modelCols['labels'], true); |
||
| 106 | $class->addMethod('relations', 'return ' . CodeGenerator::genArray($modelCols['relations']), [], true); |
||
| 107 | $modelCode = "<?php \n\nnamespace {$module};\n\n" . $class->generate(); |
||
| 108 | |||
| 109 | $modulePath = Module::getModulePath($module); |
||
| 110 | Tools::createDir($modulePath . '/models'); |
||
| 111 | file_put_contents($modulePath . '/models/' . $codeName . '.php', $modelCode); |
||
| 112 | $config = Config::custom($modulePath . '/generatorHash.php'); |
||
| 113 | $config['models/' . $codeName . '.php'] = md5($modelCode); |
||
| 114 | Config::save($modulePath . '/generatorHash.php', $config); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function install($module, $params = []) { |
||
| 118 | $installed = Module::getInstalled(App::$primary); |
||
| 119 | if (in_array($module, $installed)) { |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | $info = Module::getInfo($module); |
||
| 123 | if (!empty($info['requires'])) { |
||
| 124 | foreach ($info['requires'] as $requireModuleName) { |
||
| 125 | $this->install($requireModuleName); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $config = Config::app(); |
||
| 130 | |||
| 131 | $location = 'modules'; |
||
| 132 | |||
| 133 | $config[$location][] = $module; |
||
| 134 | if (!empty($info['autoload'])) { |
||
| 135 | $config['autoloadModules'][] = $module; |
||
| 136 | } |
||
| 137 | if (!empty($info['menu'])) { |
||
| 138 | foreach ($info['menu'] as $appType => $items) { |
||
| 139 | $this->addInMenu($items, $appType); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | Config::save('app', $config, null, App::$primary); |
||
| 143 | |||
| 144 | $paths = Module::getModulePaths($module); |
||
| 145 | foreach ($paths as $path) { |
||
| 146 | if (file_exists($path . '/install_script.php')) { |
||
| 147 | $installFunction = include $path . '/install_script.php'; |
||
| 148 | $installFunction(1, $params); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | public function unInstall($module, $params = []) { |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | public function addInMenu($items, $appType, $parent = 0) { |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | public function getSelectListModels($module = '') { |
||
| 228 | } |
||
| 229 | |||
| 230 | public function getModelsList($module, $dir = '') { |
||
| 245 | } |
||
| 246 | |||
| 247 | public function createController($module, $controllerType) { |
||
| 259 | } |
||
| 260 | |||
| 261 | public function addActionToController($module, $type, $controller, $url) { |
||
| 272 | } |
||
| 273 | } |