| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class Ethna_SkeltonGenerator |
|---|
| 21 | { |
|---|
| 22 | Â Â |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | function generateProjectSkelton($basedir, $id) |
|---|
| 29 | Â Â { |
|---|
| 30 | Â Â Â Â $dir_list = array( |
|---|
| 31 | Â Â Â Â Â Â array("app", 0755), |
|---|
| 32 | Â Â Â Â Â Â array("app/action", 0755), |
|---|
| 33 | Â Â Â Â Â Â array("app/action_cli", 0755), |
|---|
| 34 | Â Â Â Â Â Â array("app/action_xmlrpc", 0755), |
|---|
| 35 | Â Â Â Â Â Â array("app/filter", 0755), |
|---|
| 36 | Â Â Â Â Â Â array("app/view", 0755), |
|---|
| 37 | Â Â Â Â Â Â array("bin", 0755), |
|---|
| 38 | Â Â Â Â Â Â array("etc", 0755), |
|---|
| 39 | Â Â Â Â Â Â array("lib", 0755), |
|---|
| 40 | Â Â Â Â Â Â array("locale", 0755), |
|---|
| 41 | Â Â Â Â Â Â array("locale/ja", 0755), |
|---|
| 42 | Â Â Â Â Â Â array("locale/ja/LC_MESSAGES", 0755), |
|---|
| 43 | Â Â Â Â Â Â array("log", 0777), |
|---|
| 44 | Â Â Â Â Â Â array("schema", 0755), |
|---|
| 45 | Â Â Â Â Â Â array("skel", 0755), |
|---|
| 46 | Â Â Â Â Â Â array("template", 0755), |
|---|
| 47 | Â Â Â Â Â Â array("template/ja", 0755), |
|---|
| 48 | Â Â Â Â Â Â array("tmp", 0777), |
|---|
| 49 | Â Â Â Â Â Â array("www", 0755), |
|---|
| 50 | Â Â Â Â Â Â array("www/css", 0755), |
|---|
| 51 | Â Â Â Â Â Â array("www/js", 0755), |
|---|
| 52 | Â Â Â Â ); |
|---|
| 53 | |
|---|
| 54 | Â Â Â Â $r = Ethna_Controller::checkAppId($id); |
|---|
| 55 | Â Â Â Â if (Ethna::isError($r)) { |
|---|
| 56 | Â Â Â Â Â Â return $r; |
|---|
| 57 | Â Â Â Â } |
|---|
| 58 | |
|---|
| 59 | Â Â Â Â $basedir = sprintf("%s/%s", $basedir, strtolower($id)); |
|---|
| 60 | |
|---|
| 61 | Â Â Â Â |
|---|
| 62 | if (is_dir($basedir) == false) { |
|---|
| 63 | Â Â Â Â Â Â |
|---|
| 64 | printf("creating directory ($basedir) [y/n]: "); |
|---|
| 65 | Â Â Â Â Â Â flush(); |
|---|
| 66 | Â Â Â Â Â Â $fp = fopen("php://stdin", "r"); |
|---|
| 67 | Â Â Â Â Â Â $r = trim(fgets($fp, 128)); |
|---|
| 68 | Â Â Â Â Â Â fclose($fp); |
|---|
| 69 | Â Â Â Â Â Â if (strtolower($r) != 'y') { |
|---|
| 70 | Â Â Â Â Â Â Â Â return Ethna::raiseError('aborted by user'); |
|---|
| 71 | Â Â Â Â Â Â } |
|---|
| 72 | |
|---|
| 73 | Â Â Â Â Â Â if (mkdir($basedir, 0775) == false) { |
|---|
| 74 | Â Â Â Â Â Â Â Â return Ethna::raiseError('directory creation failed'); |
|---|
| 75 | Â Â Â Â Â Â } |
|---|
| 76 | Â Â Â Â } |
|---|
| 77 | Â Â Â Â foreach ($dir_list as $dir) { |
|---|
| 78 | Â Â Â Â Â Â $mode = $dir[1]; |
|---|
| 79 | Â Â Â Â Â Â $dir = $dir[0]; |
|---|
| 80 | Â Â Â Â Â Â $target = "$basedir/$dir"; |
|---|
| 81 | Â Â Â Â Â Â if (is_dir($target)) { |
|---|
| 82 | Â Â Â Â Â Â Â Â printf("%s already exists -> skipping...\n", $target); |
|---|
| 83 | Â Â Â Â Â Â Â Â continue; |
|---|
| 84 | Â Â Â Â Â Â } |
|---|
| 85 | Â Â Â Â Â Â if (mkdir($target, $mode) == false) { |
|---|
| 86 | Â Â Â Â Â Â Â Â return Ethna::raiseError('directory creation failed'); |
|---|
| 87 | Â Â Â Â Â Â } else { |
|---|
| 88 | Â Â Â Â Â Â Â Â printf("proejct sub directory created [%s]\n", $target); |
|---|
| 89 | Â Â Â Â Â Â } |
|---|
| 90 | Â Â Â Â Â Â if (chmod($target, $mode) == false) { |
|---|
| 91 | Â Â Â Â Â Â Â Â return Ethna::raiseError('chmod failed'); |
|---|
| 92 | Â Â Â Â Â Â } |
|---|
| 93 | Â Â Â Â } |
|---|
| 94 | |
|---|
| 95 | Â Â Â Â |
|---|
| 96 | $macro['application_id'] = strtoupper($id); |
|---|
| 97 | Â Â Â Â $macro['project_id'] = ucfirst($id); |
|---|
| 98 | Â Â Â Â $macro['project_prefix'] = strtolower($id); |
|---|
| 99 | Â Â Â Â $macro['basedir'] = realpath($basedir); |
|---|
| 100 | |
|---|
| 101 | Â Â Â Â $macro['action_class'] = '{$action_class}'; |
|---|
| 102 | Â Â Â Â $macro['action_form'] = '{$action_form}'; |
|---|
| 103 | Â Â Â Â $macro['action_name'] = '{$action_name}'; |
|---|
| 104 | Â Â Â Â $macro['action_path'] = '{$action_path}'; |
|---|
| 105 | Â Â Â Â $macro['forward_name'] = '{$forward_name}'; |
|---|
| 106 | Â Â Â Â $macro['view_name'] = '{$view_name}'; |
|---|
| 107 | Â Â Â Â $macro['view_path'] = '{$view_path}'; |
|---|
| 108 | |
|---|
| 109 | Â Â Â Â |
|---|
| 110 | if ($this->_generateFile("www.index.php", "$basedir/www/index.php", $macro) == false || |
|---|
| 111 | Â Â Â Â Â Â $this->_generateFile("www.info.php", "$basedir/www/info.php", $macro) == false || |
|---|
| 112 | Â Â Â Â Â Â $this->_generateFile("www.unittest.php", "$basedir/www/unittest.php", $macro) == false || |
|---|
| 113 | Â Â Â Â Â Â $this->_generateFile("www.xmlrpc.php", "$basedir/www/xmlrpc.php", $macro) == false || |
|---|
| 114 | Â Â Â Â Â Â $this->_generateFile("www.css.ethna.css", "$basedir/www/css/ethna.css", $macro) == false || |
|---|
| 115 | Â Â Â Â Â Â $this->_generateFile("dot.ethna", "$basedir/.ethna", $macro) == false || |
|---|
| 116 | Â Â Â Â Â Â $this->_generateFile("app.controller.php", sprintf("$basedir/app/%s_Controller.php", $macro['project_id']), $macro) == false || |
|---|
| 117 | Â Â Â Â Â Â $this->_generateFile("app.error.php", sprintf("$basedir/app/%s_Error.php", $macro['project_id']), $macro) == false || |
|---|
| 118 | Â Â Â Â Â Â $this->_generateFile("app.action.default.php", "$basedir/app/action/Index.php", $macro) == false || |
|---|
| 119 | Â Â Â Â Â Â $this->_generateFile("app.filter.default.php", sprintf("$basedir/app/filter/%s_Filter_ExecutionTime.php", $macro['project_id']), $macro) == false || |
|---|
| 120 | Â Â Â Â Â Â $this->_generateFile("app.view.default.php", "$basedir/app/view/Index.php", $macro) == false || |
|---|
| 121 | Â Â Â Â Â Â $this->_generateFile("app.unittest.php", sprintf("$basedir/app/%s_UnitTestManager.php", $macro['project_id']), $macro) == false || |
|---|
| 122 | Â Â Â Â Â Â $this->_generateFile("etc.ini.php", sprintf("$basedir/etc/%s-ini.php", $macro['project_prefix']), $macro) == false || |
|---|
| 123 | Â Â Â Â Â Â $this->_generateFile("skel.action.php", sprintf("$basedir/skel/skel.action.php"), $macro) == false || |
|---|
| 124 | Â Â Â Â Â Â $this->_generateFile("skel.action_cli.php", sprintf("$basedir/skel/skel.action_cli.php"), $macro) == false || |
|---|
| 125 | Â Â Â Â Â Â $this->_generateFile("skel.action_test.php", sprintf("$basedir/skel/skel.action_test.php"), $macro) == false || |
|---|
| 126 | Â Â Â Â Â Â $this->_generateFile("skel.app_object.php", sprintf("$basedir/skel/skel.app_object.php"), $macro) == false || |
|---|
| 127 | Â Â Â Â Â Â $this->_generateFile("skel.cli.php", sprintf("$basedir/skel/skel.cli.php"), $macro) == false || |
|---|
| 128 | Â Â Â Â Â Â $this->_generateFile("skel.view.php", sprintf("$basedir/skel/skel.view.php"), $macro) == false || |
|---|
| 129 | Â Â Â Â Â Â $this->_generateFile("skel.template.tpl", sprintf("$basedir/skel/skel.template.tpl"), $macro) == false || |
|---|
| 130 | Â Â Â Â Â Â $this->_generateFile("skel.view_test.php", sprintf("$basedir/skel/skel.view_test.php"), $macro) == false || |
|---|
| 131 | Â Â Â Â Â Â $this->_generateFile("template.index.tpl", sprintf("$basedir/template/ja/index.tpl"), $macro) == false) { |
|---|
| 132 | Â Â Â Â Â Â return Ethna::raiseError('generating files failed'); |
|---|
| 133 | Â Â Â Â } |
|---|
| 134 | |
|---|
| 135 | Â Â Â Â return true; |
|---|
| 136 | Â Â } |
|---|
| 137 | |
|---|
| 138 | Â Â |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | function generateActionSkelton($action_name, $app_dir, $gateway = GATEWAY_WWW) |
|---|
| 146 | Â Â { |
|---|
| 147 | Â Â Â Â |
|---|
| 148 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 149 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 150 | Â Â Â Â Â Â return $controller_class; |
|---|
| 151 | Â Â Â Â } |
|---|
| 152 | |
|---|
| 153 | Â Â Â Â $c =& new $controller_class; |
|---|
| 154 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 155 | |
|---|
| 156 | Â Â Â Â $action_dir = $c->getActiondir($gateway); |
|---|
| 157 | Â Â Â Â $action_class = $c->getDefaultActionClass($action_name, $gateway); |
|---|
| 158 | Â Â Â Â $action_form = $c->getDefaultFormClass($action_name, $gateway); |
|---|
| 159 | Â Â Â Â $action_path = $c->getDefaultActionPath($action_name); |
|---|
| 160 | |
|---|
| 161 | Â Â Â Â $macro = array(); |
|---|
| 162 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 163 | Â Â Â Â $macro['action_name'] = $action_name; |
|---|
| 164 | Â Â Â Â $macro['action_class'] = $action_class; |
|---|
| 165 | Â Â Â Â $macro['action_form'] = $action_form; |
|---|
| 166 | Â Â Â Â $macro['action_path'] = $action_path; |
|---|
| 167 | |
|---|
| 168 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 169 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 170 | |
|---|
| 171 | Â Â Â Â $this->_mkdir(dirname("$action_dir$action_path"), 0755); |
|---|
| 172 | |
|---|
| 173 | Â Â Â Â switch ($gateway) { |
|---|
| 174 | Â Â Â Â case GATEWAY_WWW: |
|---|
| 175 | Â Â Â Â Â Â $skelton = "skel.action.php"; |
|---|
| 176 | Â Â Â Â Â Â break; |
|---|
| 177 | Â Â Â Â case GATEWAY_CLI: |
|---|
| 178 | Â Â Â Â Â Â $skelton = "skel.action_cli.php"; |
|---|
| 179 | Â Â Â Â Â Â break; |
|---|
| 180 | Â Â Â Â case GATEWAY_XMLRPC: |
|---|
| 181 | Â Â Â Â Â Â $skelton = "skel.action_xmlrpc.php"; |
|---|
| 182 | Â Â Â Â Â Â break; |
|---|
| 183 | Â Â Â Â } |
|---|
| 184 | |
|---|
| 185 | Â Â Â Â if (file_exists("$action_dir$action_path")) { |
|---|
| 186 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", "$action_dir$action_path"); |
|---|
| 187 | Â Â Â Â } else if ($this->_generateFile($skelton, "$action_dir$action_path", $macro) == false) { |
|---|
| 188 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", "$action_dir$action_path"); |
|---|
| 189 | Â Â Â Â } else { |
|---|
| 190 | Â Â Â Â Â Â printf("action script(s) successfully created [%s]\n", "$action_dir$action_path"); |
|---|
| 191 | Â Â Â Â } |
|---|
| 192 | Â Â } |
|---|
| 193 | |
|---|
| 194 | Â Â |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | function generateViewSkelton($forward_name, $app_dir) |
|---|
| 201 | Â Â { |
|---|
| 202 | Â Â Â Â |
|---|
| 203 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 204 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 205 | Â Â Â Â Â Â return $controller_class; |
|---|
| 206 | Â Â Â Â } |
|---|
| 207 | |
|---|
| 208 | Â Â Â Â $c =& new $controller_class; |
|---|
| 209 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 210 | |
|---|
| 211 | Â Â Â Â $view_dir = $c->getViewdir(); |
|---|
| 212 | Â Â Â Â $view_class = $c->getDefaultViewClass($forward_name, false); |
|---|
| 213 | Â Â Â Â $view_path = $c->getDefaultViewPath($forward_name, false); |
|---|
| 214 | |
|---|
| 215 | Â Â Â Â $macro = array(); |
|---|
| 216 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 217 | Â Â Â Â $macro['forward_name'] = $forward_name; |
|---|
| 218 | Â Â Â Â $macro['view_class'] = $view_class; |
|---|
| 219 | Â Â Â Â $macro['view_path'] = $view_path; |
|---|
| 220 | |
|---|
| 221 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 222 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 223 | |
|---|
| 224 | Â Â Â Â $this->_mkdir(dirname("$view_dir/$view_path"), 0755); |
|---|
| 225 | |
|---|
| 226 | Â Â Â Â if (file_exists("$view_dir$view_path")) { |
|---|
| 227 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", "$view_dir$view_path"); |
|---|
| 228 | Â Â Â Â } else if ($this->_generateFile("skel.view.php", "$view_dir$view_path", $macro) == false) { |
|---|
| 229 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", "$view_dir$view_path"); |
|---|
| 230 | Â Â Â Â } else { |
|---|
| 231 | Â Â Â Â Â Â printf("view script(s) successfully created [%s]\n", "$view_dir$view_path"); |
|---|
| 232 | Â Â Â Â } |
|---|
| 233 | Â Â } |
|---|
| 234 | |
|---|
| 235 | Â Â |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | function generateCliSkelton($action_name, $app_dir) |
|---|
| 242 | Â Â { |
|---|
| 243 | Â Â Â Â |
|---|
| 244 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 245 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 246 | Â Â Â Â Â Â return $controller_class; |
|---|
| 247 | Â Â Â Â } |
|---|
| 248 | |
|---|
| 249 | Â Â Â Â $c =& new $controller_class; |
|---|
| 250 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 251 | |
|---|
| 252 | Â Â Â Â $action_dir = $c->getActiondir($gateway); |
|---|
| 253 | Â Â Â Â $app_dir = $c->getDirectory('app'); |
|---|
| 254 | Â Â Â Â $bin_dir = $c->getDirectory('bin'); |
|---|
| 255 | Â Â Â Â $cli_file = sprintf("%s/%s.%s", $bin_dir, $action_name, $c->getExt('php')); |
|---|
| 256 | |
|---|
| 257 | Â Â Â Â $macro = array(); |
|---|
| 258 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 259 | Â Â Â Â $macro['action_name'] = $action_name; |
|---|
| 260 | Â Â Â Â $macro['dir_app'] = $app_dir; |
|---|
| 261 | Â Â Â Â $macro['dir_bin'] = $bin_dir; |
|---|
| 262 | |
|---|
| 263 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 264 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 265 | |
|---|
| 266 | Â Â Â Â if (file_exists($cli_file)) { |
|---|
| 267 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", $cli_file); |
|---|
| 268 | Â Â Â Â } else if ($this->_generateFile("skel.cli.php", $cli_file, $macro) == false) { |
|---|
| 269 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", $cli_file); |
|---|
| 270 | Â Â Â Â } else { |
|---|
| 271 | Â Â Â Â Â Â printf("action script(s) successfully created [%s]\n", $cli_file); |
|---|
| 272 | Â Â Â Â } |
|---|
| 273 | Â Â } |
|---|
| 274 | |
|---|
| 275 | Â Â |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | function generateTemplateSkelton($forward_name, $app_dir) |
|---|
| 282 | Â Â { |
|---|
| 283 | Â Â Â Â |
|---|
| 284 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 285 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 286 | Â Â Â Â Â Â return $controller_class; |
|---|
| 287 | Â Â Â Â } |
|---|
| 288 | |
|---|
| 289 | Â Â Â Â $c =& new $controller_class; |
|---|
| 290 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 291 | |
|---|
| 292 | Â Â Â Â $tpl_dir = $c->getTemplatedir(); |
|---|
| 293 | Â Â Â Â if ($tpl_dir{strlen($tpl_dir)-1} != '/') { |
|---|
| 294 | Â Â Â Â Â Â $tpl_dir .= '/'; |
|---|
| 295 | Â Â Â Â } |
|---|
| 296 | Â Â Â Â $tpl_path = $c->getDefaultForwardPath($forward_name); |
|---|
| 297 | |
|---|
| 298 | Â Â Â Â $macro = array(); |
|---|
| 299 | Â Â Â Â |
|---|
| 300 | $macro['_project_id'] = $c->getAppId(); |
|---|
| 301 | |
|---|
| 302 | Â Â Â Â $this->_mkdir(dirname("$tpl_dir/$tpl_path"), 0755); |
|---|
| 303 | |
|---|
| 304 | Â Â Â Â if (file_exists("$tpl_dir$tpl_path")) { |
|---|
| 305 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", "$tpl_dir$tpl_path"); |
|---|
| 306 | Â Â Â Â } else if ($this->_generateFile("skel.template.tpl", "$tpl_dir$tpl_path", $macro) == false) { |
|---|
| 307 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", "$tpl_dir$tpl_path"); |
|---|
| 308 | Â Â Â Â } else { |
|---|
| 309 | Â Â Â Â Â Â printf("template file(s) successfully created [%s]\n", "$tpl_dir$tpl_path"); |
|---|
| 310 | Â Â Â Â } |
|---|
| 311 | Â Â } |
|---|
| 312 | |
|---|
| 313 | Â Â |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | function generateAppObjectSkelton($table_name, $app_dir) |
|---|
| 320 | Â Â { |
|---|
| 321 | Â Â Â Â |
|---|
| 322 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 323 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 324 | Â Â Â Â Â Â return $controller_class; |
|---|
| 325 | Â Â Â Â } |
|---|
| 326 | |
|---|
| 327 | Â Â Â Â $c =& new $controller_class; |
|---|
| 328 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 329 | |
|---|
| 330 | Â Â Â Â $table_id = preg_replace('/_(.)/e', "strtoupper('\$1')", ucfirst($table_name)); |
|---|
| 331 | |
|---|
| 332 | Â Â Â Â $app_dir = $c->getDirectory('app'); |
|---|
| 333 | Â Â Â Â $app_path = ucfirst($c->getAppId()) . '_' . $table_id .'.php'; |
|---|
| 334 | |
|---|
| 335 | Â Â Â Â $macro = array(); |
|---|
| 336 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 337 | Â Â Â Â $macro['app_path'] = $app_path; |
|---|
| 338 | Â Â Â Â $macro['app_object'] = ucfirst($c->getAppId()) . '_' . $table_id; |
|---|
| 339 | |
|---|
| 340 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 341 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 342 | |
|---|
| 343 | Â Â Â Â $path = "$app_dir/$app_path"; |
|---|
| 344 | Â Â Â Â $this->_mkdir(dirname($path), 0755); |
|---|
| 345 | Â Â Â Â if (file_exists($path)) { |
|---|
| 346 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", $path); |
|---|
| 347 | Â Â Â Â } else if ($this->_generateFile("skel.app_object.php", $path, $macro) == false) { |
|---|
| 348 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", $path); |
|---|
| 349 | Â Â Â Â } else { |
|---|
| 350 | Â Â Â Â Â Â printf("app-object script(s) successfully created [%s]\n", $path); |
|---|
| 351 | Â Â Â Â } |
|---|
| 352 | Â Â } |
|---|
| 353 | |
|---|
| 354 | Â Â |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | function generateAppManagerSkelton($manager_name, $app_dir) |
|---|
| 361 | Â Â { |
|---|
| 362 | Â Â Â Â |
|---|
| 363 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 364 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 365 | Â Â Â Â Â Â return $controller_class; |
|---|
| 366 | Â Â Â Â } |
|---|
| 367 | |
|---|
| 368 | Â Â Â Â $c =& new $controller_class; |
|---|
| 369 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 370 | |
|---|
| 371 | Â Â Â Â $manager_id = preg_replace('/_(.)/e', "strtoupper('\$1')", ucfirst($manager_name)); |
|---|
| 372 | |
|---|
| 373 | Â Â Â Â $app_dir = $c->getDirectory('app'); |
|---|
| 374 | Â Â Â Â $app_path = ucfirst($c->getAppId()) . '_' . $manager_id .'Manager.php'; |
|---|
| 375 | |
|---|
| 376 | Â Â Â Â $macro = array(); |
|---|
| 377 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 378 | Â Â Â Â $macro['app_path'] = $app_path; |
|---|
| 379 | Â Â Â Â $macro['app_manager'] = ucfirst($c->getAppId()) . '_' . $manager_id; |
|---|
| 380 | |
|---|
| 381 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 382 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 383 | |
|---|
| 384 | Â Â Â Â $path = "$app_dir/$app_path"; |
|---|
| 385 | Â Â Â Â $this->_mkdir(dirname($path), 0755); |
|---|
| 386 | Â Â Â Â if (file_exists($path)) { |
|---|
| 387 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", $path); |
|---|
| 388 | Â Â Â Â } else if ($this->_generateFile("skel.app_manager.php", $path, $macro) == false) { |
|---|
| 389 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", $path); |
|---|
| 390 | Â Â Â Â } else { |
|---|
| 391 | Â Â Â Â Â Â printf("app-manager script(s) successfully created [%s]\n", $path); |
|---|
| 392 | Â Â Â Â } |
|---|
| 393 | Â Â } |
|---|
| 394 | |
|---|
| 395 | Â Â |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | function generateActionTestSkelton($action_name, $app_dir) |
|---|
| 402 | Â Â { |
|---|
| 403 | Â Â Â Â |
|---|
| 404 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 405 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 406 | Â Â Â Â Â Â return $controller_class; |
|---|
| 407 | Â Â Â Â } |
|---|
| 408 | |
|---|
| 409 | Â Â Â Â $c =& new $controller_class; |
|---|
| 410 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 411 | |
|---|
| 412 | Â Â Â Â $action_dir = $c->getActiondir(); |
|---|
| 413 | Â Â Â Â $action_class = $c->getDefaultActionClass($action_name, false); |
|---|
| 414 | Â Â Â Â $action_form = $c->getDefaultFormClass($action_name, false); |
|---|
| 415 | Â Â Â Â $action_path = $c->getDefaultActionPath($action_name . "Test", false); |
|---|
| 416 | |
|---|
| 417 | Â Â Â Â $macro = array(); |
|---|
| 418 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 419 | Â Â Â Â $macro['action_name'] = $action_name; |
|---|
| 420 | Â Â Â Â $macro['action_class'] = $action_class; |
|---|
| 421 | Â Â Â Â $macro['action_form'] = $action_form; |
|---|
| 422 | Â Â Â Â $macro['action_path'] = $action_path; |
|---|
| 423 | |
|---|
| 424 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 425 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 426 | |
|---|
| 427 | Â Â Â Â $this->_mkdir(dirname("$action_dir$action_path"), 0755); |
|---|
| 428 | |
|---|
| 429 | Â Â Â Â if (file_exists("$action_dir$action_path")) { |
|---|
| 430 | Â Â Â Â Â Â printf("file [%s] aleady exists -> skip\n", "$action_dir$action_path"); |
|---|
| 431 | Â Â Â Â } else if ($this->_generateFile("skel.action_test.php", "$action_dir$action_path", $macro) == false) { |
|---|
| 432 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", "$action_dir$action_path"); |
|---|
| 433 | Â Â Â Â } else { |
|---|
| 434 | Â Â Â Â Â Â printf("action test(s) successfully created [%s]\n", "$action_dir$action_path"); |
|---|
| 435 | Â Â Â Â } |
|---|
| 436 | Â Â } |
|---|
| 437 | |
|---|
| 438 | Â Â |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | function generateViewTestSkelton($forward_name, $app_dir) |
|---|
| 445 | Â Â { |
|---|
| 446 | Â Â Â Â |
|---|
| 447 | $controller_class = $this->_discoverController($app_dir); |
|---|
| 448 | Â Â Â Â if (Ethna::isError($controller_class)) { |
|---|
| 449 | Â Â Â Â Â Â return $controller_class; |
|---|
| 450 | Â Â Â Â } |
|---|
| 451 | |
|---|
| 452 | Â Â Â Â $c =& new $controller_class; |
|---|
| 453 | Â Â Â Â $c->setGateway(GATEWAY_CLI); |
|---|
| 454 | |
|---|
| 455 | Â Â Â Â $view_dir = $c->getViewdir(); |
|---|
| 456 | Â Â Â Â $view_class = $c->getDefaultViewClass($forward_name, false); |
|---|
| 457 | Â Â Â Â $view_path = $c->getDefaultViewPath($forward_name . "Test", false); |
|---|
| 458 | |
|---|
| 459 | Â Â Â Â $macro = array(); |
|---|
| 460 | Â Â Â Â $macro['project_id'] = $c->getAppId(); |
|---|
| 461 | Â Â Â Â $macro['forward_name'] = $forward_name; |
|---|
| 462 | Â Â Â Â $macro['view_class'] = $view_class; |
|---|
| 463 | Â Â Â Â $macro['view_path'] = $view_path; |
|---|
| 464 | |
|---|
| 465 | Â Â Â Â $user_macro = $this->_getUserMacro(); |
|---|
| 466 | Â Â Â Â $macro = array_merge($macro, $user_macro); |
|---|
| 467 | |
|---|
| 468 | Â Â Â Â $this->_mkdir(dirname("$view_dir/$view_path"), 0755); |
|---|
| 469 | |
|---|
| 470 | Â Â Â Â if (file_exists("$view_dir$view_path")) { |
|---|
| 471 | Â Â Â Â Â Â printf("file [%s] aleady exists -> skip\n", "$view_dir$view_path"); |
|---|
| 472 | Â Â Â Â } else if ($this->_generateFile("skel.view_test.php", "$view_dir$view_path", $macro) == false) { |
|---|
| 473 | Â Â Â Â Â Â printf("[warning] file creation failed [%s]\n", "$view_dir$view_path"); |
|---|
| 474 | Â Â Â Â } else { |
|---|
| 475 | Â Â Â Â Â Â printf("view test(s) successfully created [%s]\n", "$view_dir$view_path"); |
|---|
| 476 | Â Â Â Â } |
|---|
| 477 | Â Â } |
|---|
| 478 | |
|---|
| 479 | Â Â |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | function _mkdir($dir, $mode) |
|---|
| 487 | Â Â { |
|---|
| 488 | Â Â Â Â if (@is_dir($dir)) { |
|---|
| 489 | Â Â Â Â Â Â return true; |
|---|
| 490 | Â Â Â Â } |
|---|
| 491 | |
|---|
| 492 | Â Â Â Â $parent = dirname($dir); |
|---|
| 493 | Â Â Â Â if ($dir == $parent) { |
|---|
| 494 | Â Â Â Â Â Â return true; |
|---|
| 495 | Â Â Â Â } |
|---|
| 496 | Â Â Â Â if (is_dir($parent) == false) { |
|---|
| 497 | Â Â Â Â Â Â $this->_mkdir($parent, $mode); |
|---|
| 498 | Â Â Â Â } |
|---|
| 499 | |
|---|
| 500 | Â Â Â Â return mkdir($dir, $mode); |
|---|
| 501 | Â Â } |
|---|
| 502 | |
|---|
| 503 | Â Â |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | function _generateFile($skel, $entity, $macro) |
|---|
| 513 | Â Â { |
|---|
| 514 | Â Â Â Â $base = null; |
|---|
| 515 | |
|---|
| 516 | Â Â Â Â if (file_exists($entity)) { |
|---|
| 517 | Â Â Â Â Â Â printf("file [%s] already exists -> skip\n", $entity); |
|---|
| 518 | Â Â Â Â Â Â return true; |
|---|
| 519 | Â Â Â Â } |
|---|
| 520 | Â Â Â Â $c =& Ethna_Controller::getInstance(); |
|---|
| 521 | Â Â Â Â if (is_object($c)) { |
|---|
| 522 | Â Â Â Â Â Â $base = $c->getBasedir(); |
|---|
| 523 | Â Â Â Â Â Â if (file_exists("$base/skel/$skel") == false) { |
|---|
| 524 | Â Â Â Â Â Â Â Â $base = null; |
|---|
| 525 | Â Â Â Â Â Â } |
|---|
| 526 | Â Â Â Â } |
|---|
| 527 | Â Â Â Â if (is_null($base)) { |
|---|
| 528 | Â Â Â Â Â Â $base = dirname(dirname(__FILE__)); |
|---|
| 529 | Â Â Â Â } |
|---|
| 530 | |
|---|
| 531 | Â Â Â Â $rfp = fopen("$base/skel/$skel", "r"); |
|---|
| 532 | Â Â Â Â if ($rfp == null) { |
|---|
| 533 | Â Â Â Â Â Â return false; |
|---|
| 534 | Â Â Â Â } |
|---|
| 535 | Â Â Â Â $wfp = fopen($entity, "w"); |
|---|
| 536 | Â Â Â Â if ($wfp == null) { |
|---|
| 537 | Â Â Â Â Â Â fclose($rfp); |
|---|
| 538 | Â Â Â Â Â Â return false; |
|---|
| 539 | Â Â Â Â } |
|---|
| 540 | |
|---|
| 541 | Â Â Â Â for (;;) { |
|---|
| 542 | Â Â Â Â Â Â $s = fread($rfp, 4096); |
|---|
| 543 | Â Â Â Â Â Â if (strlen($s) == 0) { |
|---|
| 544 | Â Â Â Â Â Â Â Â break; |
|---|
| 545 | Â Â Â Â Â Â } |
|---|
| 546 | |
|---|
| 547 | Â Â Â Â Â Â foreach ($macro as $k => $v) { |
|---|
| 548 | Â Â Â Â Â Â Â Â $s = preg_replace("/{\\\$$k}/", $v, $s); |
|---|
| 549 | Â Â Â Â Â Â } |
|---|
| 550 | Â Â Â Â Â Â fwrite($wfp, $s); |
|---|
| 551 | Â Â Â Â } |
|---|
| 552 | |
|---|
| 553 | Â Â Â Â fclose($wfp); |
|---|
| 554 | Â Â Â Â fclose($rfp); |
|---|
| 555 | |
|---|
| 556 | Â Â Â Â $st = stat("$base/skel/$skel"); |
|---|
| 557 | Â Â Â Â if (chmod($entity, $st[2]) == false) { |
|---|
| 558 | Â Â Â Â Â Â return false; |
|---|
| 559 | Â Â Â Â } |
|---|
| 560 | |
|---|
| 561 | Â Â Â Â printf("file generated [%s -> %s]\n", $skel, $entity); |
|---|
| 562 | |
|---|
| 563 | Â Â Â Â return true; |
|---|
| 564 | Â Â } |
|---|
| 565 | |
|---|
| 566 | Â Â |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | function _getUserMacro() |
|---|
| 572 | Â Â { |
|---|
| 573 | Â Â Â Â $home = $_SERVER['HOME']; |
|---|
| 574 | Â Â Â Â if (is_file("$home/.ethna") == false) { |
|---|
| 575 | Â Â Â Â Â Â return array(); |
|---|
| 576 | Â Â Â Â } |
|---|
| 577 | |
|---|
| 578 | Â Â Â Â $user_macro = parse_ini_file("$home/.ethna"); |
|---|
| 579 | Â Â Â Â return $user_macro; |
|---|
| 580 | Â Â } |
|---|
| 581 | |
|---|
| 582 | Â Â |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | function _discoverController($app_dir) |
|---|
| 587 | Â Â { |
|---|
| 588 | Â Â Â Â $ini_file = null; |
|---|
| 589 | Â Â Â Â while (is_dir($app_dir) && $app_dir != "/") { |
|---|
| 590 | Â Â Â Â Â Â if (is_file("$app_dir/.ethna")) { |
|---|
| 591 | Â Â Â Â Â Â Â Â $ini_file = "$app_dir/.ethna"; |
|---|
| 592 | Â Â Â Â Â Â Â Â break; |
|---|
| 593 | Â Â Â Â Â Â } |
|---|
| 594 | Â Â Â Â Â Â $app_dir = dirname($app_dir); |
|---|
| 595 | Â Â Â Â } |
|---|
| 596 | |
|---|
| 597 | Â Â Â Â if ($ini_file === null) { |
|---|
| 598 | Â Â Â Â Â Â return Ethna::raiseError('no .ethna file found'); |
|---|
| 599 | Â Â Â Â } |
|---|
| 600 | Â Â Â Â |
|---|
| 601 | Â Â Â Â $macro = parse_ini_file($ini_file); |
|---|
| 602 | Â Â Â Â if (isset($macro['controller_file']) == false || isset($macro['controller_class']) == false) { |
|---|
| 603 | Â Â Â Â Â Â return Ethna::raiseError('invalid .ethna file'); |
|---|
| 604 | Â Â Â Â } |
|---|
| 605 | Â Â Â Â $file = $macro['controller_file']; |
|---|
| 606 | Â Â Â Â $class = $macro['controller_class']; |
|---|
| 607 | |
|---|
| 608 | Â Â Â Â $controller_file = "$app_dir/$file"; |
|---|
| 609 | Â Â Â Â if (is_file($controller_file) == false) { |
|---|
| 610 | Â Â Â Â Â Â return Ethna::raiseError("no such file $controller_file"); |
|---|
| 611 | Â Â Â Â } |
|---|
| 612 | |
|---|
| 613 | Â Â Â Â include_once($controller_file); |
|---|
| 614 | Â Â Â Â if (class_exists($class) == false) { |
|---|
| 615 | Â Â Â Â Â Â return Ethna::raiseError("no such class $class"); |
|---|
| 616 | Â Â Â Â } |
|---|
| 617 | |
|---|
| 618 | Â Â Â Â return $class; |
|---|
| 619 | Â Â } |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | ?> |
|---|