PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/module/Dev/controller/Generate.php

https://github.com/tquensen/MiniMVC
PHP | 569 lines | 490 code | 63 blank | 16 comment | 52 complexity | bc2202fce178ee30bac18e202595cfcb MD5 | raw file
  1. <?php
  2. class Dev_Generate_Controller extends MiniMVC_Controller
  3. {
  4. public function i18nAction($params)
  5. {
  6. if (!$params['module']) {
  7. return 'Kein Modul angegeben!' . "\n";
  8. }
  9. if (!file_exists(MODULEPATH . $params['module'])) {
  10. return 'Modul existiert nicht!' . "\n";
  11. }
  12. $return = '';
  13. $languages = array();
  14. $activeLanguages = $this->registry->settings->get('config/enabledLanguages', array());
  15. foreach ($activeLanguages as $language) {
  16. $MiniMVC_i18n = array();
  17. $languages[$language] = array('found' => array(), 'new' => array());
  18. if (file_exists(MODULEPATH . $params['module'].'/i18n/'.$language.'.php')) {
  19. include MODULEPATH . $params['module'].'/i18n/'.$language.'.php';
  20. if (isset($MiniMVC_i18n[$params['module']])) {
  21. $languages[$language]['found'] = $MiniMVC_i18n[$params['module']];
  22. }
  23. }
  24. }
  25. $i18nFound = $this->searchI18n(MODULEPATH . $params['module']);
  26. asort($i18nFound);
  27. foreach ($languages as $currentLanguage => $currentLanguageData) {
  28. foreach ($i18nFound as $newI18n) {
  29. if (!isset($currentLanguageData['found'][$newI18n])) {
  30. $languages[$currentLanguage]['new'][$newI18n] = '$MiniMVC_i18n[\''.$params['module'].'\'][\''.$newI18n.'\'] = \''.$newI18n.'\';';
  31. }
  32. }
  33. }
  34. foreach ($languages as $currentLanguage => $currentLanguageData) {
  35. if (file_exists(MODULEPATH . $params['module'].'/i18n/'.$currentLanguage.'.php')) {
  36. file_put_contents(MODULEPATH . $params['module'].'/i18n/'.$currentLanguage.'.php', "\n".implode("\n", $languages[$currentLanguage]['new']), FILE_APPEND);
  37. $return .= 'Datei '.MODULEPATH . $params['module'].'/i18n/'.$currentLanguage.'.php'.' aktualisiert!'."\n";
  38. } else {
  39. file_put_contents(MODULEPATH . $params['module'].'/i18n/'.$currentLanguage.'.php', '<?php'."\n".implode("\n", $languages[$currentLanguage]['new']));
  40. $return .= 'Datei '.MODULEPATH . $params['module'].'/i18n/'.$currentLanguage.'.php'.' angelegt!'."\n";
  41. }
  42. }
  43. return $return .= 'I18n-Dateien erfolgreich generiert!';
  44. }
  45. protected function searchI18n($folder, $found = array())
  46. {
  47. if (!is_dir($folder)) {
  48. return $found;
  49. }
  50. $regex = '#(\$|->)(t|i18n)->([\w]+)#i';
  51. foreach (scandir($folder) as $file) {
  52. if (is_dir($folder.'/'.$file) && $file !== '.' && $file !== '..') {
  53. $found = $this->searchI18n($folder.'/'.$file, $found);
  54. } elseif (is_file($folder.'/'.$file) && substr($file, -4) == '.php') {
  55. $content = file_get_contents($folder.'/'.$file);
  56. $matches = array();
  57. if (preg_match_all($regex, $content, $matches)) {
  58. foreach ($matches[3] as $match) {
  59. if ($match == 'get') {
  60. continue;
  61. }
  62. $found[$match] = $match;
  63. }
  64. }
  65. }
  66. }
  67. return $found;
  68. }
  69. public function appAction($params)
  70. {
  71. if (!$params['app']) {
  72. return 'Keinen Appnamen angegeben!' . "\n";
  73. }
  74. if (file_exists(APPPATH . $params['app'])) {
  75. return 'App existiert bereits!' . "\n";
  76. }
  77. if (!is_writable(APPPATH)) {
  78. return 'Keine Schreibrechte im App-Ordner!' . "\n";
  79. }
  80. $path = APPPATH . $params['app'];
  81. $dummy = MODULEPATH . 'Dev/dummies';
  82. mkdir($path);
  83. mkdir($path . '/i18n');
  84. mkdir($path . '/settings');
  85. mkdir($path . '/view');
  86. mkdir($path . '/web');
  87. mkdir($path . '/web/js');
  88. mkdir($path . '/web/css');
  89. mkdir($path . '/web/images');
  90. mkdir($path . '/lib');
  91. file_put_contents($path . '/view/default.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_view.php')));
  92. // file_put_contents($path . '/view/default.html.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_view_html.php')));
  93. // file_put_contents($path . '/view/default.json.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_view.json.php')));
  94. // file_put_contents($path . '/view/default.xml.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_view.xml.php')));
  95. file_put_contents($path . '/i18n/de_DE.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/de_DE_app.php')));
  96. file_put_contents($path . '/i18n/en_US.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/en_US_app.php')));
  97. //file_put_contents($path . '/settings/slots.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/slots_app.php')));
  98. file_put_contents($path . '/settings/view.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/view.php')));
  99. file_put_contents($path . '/settings/config.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/config_app.php')));
  100. file_put_contents($path . '/settings/routes.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/routes_app.php')));
  101. file_put_contents($path . '/settings/widgets.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/widgets_app.php')));
  102. file_put_contents($path . '/settings/theme.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/theme.php')));
  103. if ($params['w']) {
  104. file_put_contents(WEBPATH . $params['app'] . '.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_web.php')));
  105. file_put_contents(WEBPATH . $params['app'] . '_dev.php', str_replace('APP', $params['app'], file_get_contents($dummy . '/app_web_dev.php')));
  106. }
  107. return 'App was generated successfully!'."\n"
  108. .'Make sure to define the $MiniMVC_apps[\''.$params['app'].'\'][\'baseurl\'] in your settings/apps.php and settings/apps_dev.php files!'."\n"
  109. .'Optionally, you can modify the web/.htaccess file to enable pretty URLs.';
  110. }
  111. public function moduleAction($params)
  112. {
  113. if (!$params['module']) {
  114. return 'Kein Modul angegeben!' . "\n";
  115. }
  116. if (file_exists(MODULEPATH . $params['module'])) {
  117. return 'Modul existiert bereits!' . "\n";
  118. }
  119. if (!is_writable(MODULEPATH)) {
  120. return 'Keine Schreibrechte im Module-Ordner!' . "\n";
  121. }
  122. $controller = !empty($params['controller']) ? $params['controller'] : $params['module'];
  123. $path = MODULEPATH . $params['module'];
  124. $dummy = MODULEPATH . 'Dev/dummies';
  125. mkdir($path);
  126. mkdir($path . '/controller');
  127. mkdir($path . '/model');
  128. //mkdir($path . '/Model/Schema');
  129. mkdir($path . '/settings');
  130. mkdir($path . '/i18n');
  131. mkdir($path . '/view');
  132. mkdir($path . '/view/'.strtolower($controller));
  133. mkdir($path . '/web');
  134. mkdir($path . '/web/js');
  135. mkdir($path . '/web/css');
  136. mkdir($path . '/web/images');
  137. mkdir($path . '/lib');
  138. //mkdir($path . '/Lib/Migrations');
  139. $search = array(
  140. 'MODULELCFIRST',
  141. 'CONTROLLERLCFIRST',
  142. 'CONTROLLERTABLE',
  143. 'MODLC',
  144. 'MODULE',
  145. 'CONTROLLERLC',
  146. 'CONTROLLER',
  147. );
  148. $replace = array(
  149. strtolower(substr($params['module'], 0, 1)) . substr($params['module'], 1),
  150. strtolower(substr($controller, 0, 1)) . substr($controller, 1),
  151. strtolower(preg_replace('/(?!^)[[:upper:]]+/', '_$0', $controller)),
  152. strtolower($params['module']),
  153. $params['module'],
  154. strtolower($controller),
  155. $controller
  156. );
  157. file_put_contents($path . '/controller/'.$controller.'.php', str_replace($search, $replace, file_get_contents($dummy . '/Controller.php')));
  158. file_put_contents($path . '/Installer.php', str_replace($search, $replace, file_get_contents($dummy . '/Installer.php')));
  159. file_put_contents($path . '/i18n/de_DE.php', str_replace($search, $replace, file_get_contents($dummy . '/de_DE.php')));
  160. file_put_contents($path . '/i18n/en_US.php', str_replace($search, $replace, file_get_contents($dummy . '/en_US.php')));
  161. file_put_contents($path . '/view/'.strtolower($controller).'/index.html.php', str_replace($search, $replace, file_get_contents($dummy . '/index.html.php')));
  162. file_put_contents($path . '/view/'.strtolower($controller).'/index.json.php', str_replace($search, $replace, file_get_contents($dummy . '/index.json.php')));
  163. file_put_contents($path . '/view/'.strtolower($controller).'/widget.html.php', str_replace($search, $replace, file_get_contents($dummy . '/widget.html.php')));
  164. // file_put_contents($path . '/view/'.strtolower($controller).'/new.html.php', str_replace($search, $replace, file_get_contents($dummy . '/new.html.php')));
  165. // file_put_contents($path . '/view/'.strtolower($controller).'/new.json.php', str_replace($search, $replace, file_get_contents($dummy . '/new.json.php')));
  166. file_put_contents($path . '/view/'.strtolower($controller).'/create.html.php', str_replace($search, $replace, file_get_contents($dummy . '/create.html.php')));
  167. file_put_contents($path . '/view/'.strtolower($controller).'/create.json.php', str_replace($search, $replace, file_get_contents($dummy . '/create.json.php')));
  168. file_put_contents($path . '/view/'.strtolower($controller).'/show.html.php', str_replace($search, $replace, file_get_contents($dummy . '/show.html.php')));
  169. file_put_contents($path . '/view/'.strtolower($controller).'/show.json.php', str_replace($search, $replace, file_get_contents($dummy . '/show.json.php')));
  170. // file_put_contents($path . '/view/'.strtolower($controller).'/edit.html.php', str_replace($search, $replace, file_get_contents($dummy . '/edit.html.php')));
  171. // file_put_contents($path . '/view/'.strtolower($controller).'/edit.json.php', str_replace($search, $replace, file_get_contents($dummy . '/edit.json.php')));
  172. file_put_contents($path . '/view/'.strtolower($controller).'/update.html.php', str_replace($search, $replace, file_get_contents($dummy . '/update.html.php')));
  173. file_put_contents($path . '/view/'.strtolower($controller).'/update.json.php', str_replace($search, $replace, file_get_contents($dummy . '/update.json.php')));
  174. file_put_contents($path . '/view/'.strtolower($controller).'/delete.html.php', str_replace($search, $replace, file_get_contents($dummy . '/delete.html.php')));
  175. file_put_contents($path . '/view/'.strtolower($controller).'/delete.json.php', str_replace($search, $replace, file_get_contents($dummy . '/delete.json.php')));
  176. file_put_contents($path . '/settings/config.php', str_replace($search, $replace, file_get_contents($dummy . '/config.php')));
  177. file_put_contents($path . '/settings/routes.php', str_replace($search, $replace, file_get_contents($dummy . '/routes.php')));
  178. file_put_contents($path . '/settings/widgets.php', str_replace($search, $replace, file_get_contents($dummy . '/widgets.php')));
  179. file_put_contents($path . '/model/definition.php', str_replace($search, $replace, file_get_contents($dummy . '/definition.php')));
  180. //file_put_contents($path . '/settings/slots.php', str_replace(array('MODLC', 'MODULE'), array(strtolower($params['module']), $params['module']), file_get_contents($dummy . '/slots.php')));
  181. //file_put_contents($path . '/Model/Schema/schema.yml', str_replace(array('MODLC', 'MODULE'), array(strtolower($params['module']), $params['module']), file_get_contents($dummy . '/schema.yml')));
  182. $routesContent = file_get_contents($path . '/settings/routes.php');
  183. file_put_contents($path . '/settings/routes.php', str_replace($search, $replace, file_get_contents($dummy . '/routes_controller.php')) . $routesContent);
  184. return 'Modul wurde erfolgreich generiert!';
  185. }
  186. public function modelAction($params)
  187. {
  188. if (!$params['module']) {
  189. return 'Kein Modul angegeben!' . "\n";
  190. }
  191. if (!$params['model']) {
  192. return 'Kein Modelnamen angegeben!' . "\n";
  193. }
  194. $definition = $this->getModelDefinition($params['module'], $params['model']);
  195. if (!$definition) {
  196. return 'No model definition found for '.$params['model'].' in module '.$params['module'].'...' . "\n";
  197. }
  198. if (empty($definition['identifier'])) {
  199. $definition['identifier'] = 'id';
  200. }
  201. if (!isset($definition['autoIncrement'])) {
  202. $definition['autoIncrement'] = false;
  203. }
  204. if (!isset($definition['columns'][$definition['identifier']])) {
  205. $definition['columns'][$definition['identifier']] = 'int';
  206. }
  207. if (!isset($definition['relations'])) {
  208. $definition['relations'] = array();
  209. }
  210. $model = ucfirst($params['model']);
  211. $search = array(
  212. '{name}',
  213. '{table}',
  214. '{module}',
  215. '{modlc}',
  216. '{namelcfirst}',
  217. '{columns_list}',
  218. '{columns_sql}',
  219. '{columns_phpdoc}',
  220. '{columns_form}',
  221. '{auto_increment}',
  222. '{identifier}',
  223. '{relations_list}',
  224. '{relations_methods}',
  225. '{modelClass}',
  226. '{tableClass}',
  227. '{collectionClass}'
  228. );
  229. $replace = array(
  230. $model,
  231. strtolower(preg_replace('/(?!^)[[:upper:]]+/', '_$0', $model)),
  232. $params['module'],
  233. strtolower($params['module']),
  234. strtolower(substr($model, 0, 1)) . substr($model, 1),
  235. '\''.implode('\', \'', array_keys($definition['columns'])).'\'',
  236. $this->getSqlCode($definition, $model),
  237. $this->getPhpDocCode($definition, $model),
  238. $this->getFormCode($definition, $model),
  239. $definition['autoIncrement'] ? 'true' : 'false',
  240. $definition['identifier'],
  241. $this->getRelationsListCode($definition, $model),
  242. $this->getRelationsMethodsCode($definition, $model),
  243. $this->registry->settings->get('config/classes/model', 'MiniMVC_Model'),
  244. $this->registry->settings->get('config/classes/table', 'MiniMVC_Table'),
  245. $this->registry->settings->get('config/classes/collection', 'MiniMVC_Collection')
  246. );
  247. $path = MODULEPATH . $params['module'].'/model';
  248. $dummy = MODULEPATH . 'Dev/dummies';
  249. $message = 'Erstelle Models...'."\n";
  250. if (!file_exists($path . '/'.$model.'.php')) {
  251. file_put_contents($path . '/'.$model.'.php', str_replace($search, $replace, file_get_contents($dummy . '/Model.php')));
  252. $message .= '-> Datei '.$model.'.php erstellt'."\n";
  253. } else {
  254. $message .= '-> Datei '.$model.'.php existiert bereits'."\n";
  255. }
  256. if (!file_exists($path . '/'.$model.'Table.php')) {
  257. file_put_contents($path . '/'.$model.'Table.php', str_replace($search, $replace, file_get_contents($dummy . '/Table.php')));
  258. $message .= '-> Datei '.$model.'Table.php erstellt'."\n";
  259. } else {
  260. $message .= '-> Datei '.$model.'Table.php existiert bereits'."\n";
  261. }
  262. if (!file_exists($path . '/'.$model.'Form.php')) {
  263. file_put_contents($path . '/'.$model.'Form.php', str_replace($search, $replace, file_get_contents($dummy . '/Form.php')));
  264. $message .= '-> Datei '.$model.'Form.php erstellt'."\n";
  265. } else {
  266. $message .= '-> Datei '.$model.'Form.php existiert bereits'."\n";
  267. }
  268. if (!file_exists($path . '/'.$model.'Collection.php')) {
  269. file_put_contents($path . '/'.$model.'Collection.php', str_replace($search, $replace, file_get_contents($dummy . '/Collection.php')));
  270. $message .= '-> Datei '.$model.'Collection.php erstellt'."\n";
  271. } else {
  272. $message .= '-> Datei '.$model.'Collection.php existiert bereits'."\n";
  273. }
  274. file_put_contents($path . '/'.$model.'Base.php', str_replace($search, $replace, file_get_contents($dummy . '/ModelBase.php')));
  275. $message .= '-> Datei '.$model.'Base.php erstellt'."\n";
  276. file_put_contents($path . '/'.$model.'TableBase.php', str_replace($search, $replace, file_get_contents($dummy . '/TableBase.php')));
  277. $message .= '-> Datei '.$model.'TableBase.php erstellt'."\n";
  278. return $message;
  279. }
  280. public function controllerAction($params)
  281. {
  282. if (!$params['module']) {
  283. return 'Kein Modul angegeben!' . "\n";
  284. }
  285. if (!$params['controller']) {
  286. return 'Kein Controllername angegeben!' . "\n";
  287. }
  288. $params['module'] = ucfirst($params['module']);
  289. $controller = ucfirst($params['controller']);
  290. $search = array(
  291. 'MODULELCFIRST',
  292. 'CONTROLLERLCFIRST',
  293. 'MODLC',
  294. 'MODULE',
  295. 'MODULELCFIRST',
  296. 'CONTROLLERLC',
  297. 'CONTROLLER'
  298. );
  299. $replace = array(
  300. strtolower(substr($params['module'], 0, 1)) . substr($params['module'], 1),
  301. strtolower(substr($controller, 0, 1)) . substr($controller, 1),
  302. strtolower($params['module']),
  303. $params['module'],
  304. strtolower(substr($params['module'], 0, 1)) . substr($params['module'], 1),
  305. strtolower($controller),
  306. $controller
  307. );
  308. $path = MODULEPATH . $params['module'].'/controller';
  309. $dummy = MODULEPATH . 'Dev/dummies';
  310. $message = 'Erstelle Controller...'."\n";
  311. if (!file_exists($path . '/'.$controller.'.php')) {
  312. mkdir($path . '/../view/'.strtolower($controller));
  313. file_put_contents($path . '/'.$controller.'.php', str_replace($search, $replace, file_get_contents($dummy . '/Controller.php')));
  314. file_put_contents($path . '/../view/'.strtolower($controller).'/index.html.php', str_replace($search, $replace, file_get_contents($dummy . '/index.html.php')));
  315. file_put_contents($path . '/../view/'.strtolower($controller).'/index.json.php', str_replace($search, $replace, file_get_contents($dummy . '/index.json.php')));
  316. file_put_contents($path . '/../view/'.strtolower($controller).'/widget.html.php', str_replace($search, $replace, file_get_contents($dummy . '/widget.html.php')));
  317. // file_put_contents($path . '/../view/'.strtolower($controller).'/new.html.php', str_replace($search, $replace, file_get_contents($dummy . '/new.html.php')));
  318. // file_put_contents($path . '/../view/'.strtolower($controller).'/new.json.php', str_replace($search, $replace, file_get_contents($dummy . '/new.json.php')));
  319. file_put_contents($path . '/../view/'.strtolower($controller).'/create.html.php', str_replace($search, $replace, file_get_contents($dummy . '/create.html.php')));
  320. file_put_contents($path . '/../view/'.strtolower($controller).'/create.json.php', str_replace($search, $replace, file_get_contents($dummy . '/create.json.php')));
  321. file_put_contents($path . '/../view/'.strtolower($controller).'/show.html.php', str_replace($search, $replace, file_get_contents($dummy . '/show.html.php')));
  322. file_put_contents($path . '/../view/'.strtolower($controller).'/show.json.php', str_replace($search, $replace, file_get_contents($dummy . '/show.json.php')));
  323. // file_put_contents($path . '/../view/'.strtolower($controller).'/edit.html.php', str_replace($search, $replace, file_get_contents($dummy . '/edit.html.php')));
  324. // file_put_contents($path . '/../view/'.strtolower($controller).'/edit.json.php', str_replace($search, $replace, file_get_contents($dummy . '/edit.json.php')));
  325. file_put_contents($path . '/../view/'.strtolower($controller).'/update.html.php', str_replace($search, $replace, file_get_contents($dummy . '/update.html.php')));
  326. file_put_contents($path . '/../view/'.strtolower($controller).'/update.json.php', str_replace($search, $replace, file_get_contents($dummy . '/update.json.php')));
  327. file_put_contents($path . '/../view/'.strtolower($controller).'/delete.html.php', str_replace($search, $replace, file_get_contents($dummy . '/delete.html.php')));
  328. file_put_contents($path . '/../view/'.strtolower($controller).'/delete.json.php', str_replace($search, $replace, file_get_contents($dummy . '/delete.json.php')));
  329. $routesContent = file_exists($path . '/../settings/routes.php') ? file_get_contents($path . '/../settings/routes.php') : '';
  330. file_put_contents($path . '/../settings/routes.php', str_replace($search, $replace, file_get_contents($dummy . '/routes_controller.php')) . $routesContent);
  331. $message .= '-> Controller controller/'.$controller.'.php wurde erstellt'."\n";
  332. $message .= '-> Views in view/'.strtolower($controller).'/ wurden erstellt'."\n";
  333. $message .= '-> Routing in settings/routes.php wurde erweitert'."\n";
  334. } else {
  335. $message .= '-> Datei '.$controller.'.php existiert bereits'."\n";
  336. }
  337. return $message;
  338. }
  339. protected function getSqlCode($definition, $model)
  340. {
  341. $types = array(
  342. 'int' => 'INT(11)',
  343. 'integer' => 'INT(11)',
  344. 'bool' => 'TINYINT(1) NOT NULL',
  345. 'boolean' => 'TINYINT(1) NOT NULL',
  346. 'float' => 'FLOAT',
  347. 'double' => 'DOUBLE',
  348. 'string' => 'VARCHAR(255)',
  349. 'array' => 'TEXT'
  350. );
  351. $return = array();
  352. foreach ($definition['columns'] as $column => $type) {
  353. $sqlType = isset($types[strtolower($type)]) ? $types[strtolower($type)] : 'VARCHAR(255)';
  354. if ($column == $definition['identifier']) {
  355. array_unshift($return, $column . ' ' .$sqlType . ($definition['autoIncrement'] ? ' AUTO_INCREMENT' : ''));
  356. } else {
  357. $return[] = ' ' . $column . ' ' .$sqlType;
  358. }
  359. }
  360. return implode(",\n", $return);
  361. }
  362. protected function getPhpDocCode($definition, $model)
  363. {
  364. $return = '';
  365. foreach ($definition['columns'] as $column => $type) {
  366. $return .= '@property ' . strtolower($type) . ' $' . $column . "\n * ";
  367. }
  368. return $return;
  369. }
  370. protected function getFormCode($definition, $model)
  371. {
  372. $types = array(
  373. 'int' => 'Text',
  374. 'integer' => 'Text',
  375. 'bool' => 'Checkbox',
  376. 'boolean' => 'Checkbox',
  377. 'float' => 'Text',
  378. 'double' => 'Text',
  379. 'string' => 'Text',
  380. 'array' => 'Text'
  381. );
  382. $code = '$this->setElement(new MiniMVC_Form_Element_{type}(\'{column}\',
  383. array(\'label\' => $this->i18n->{namelcfirst}Form{columncc}Label),
  384. array(
  385. //new MiniMVC_Form_Validator_Required(array(\'errorMessage\' => $this->i18n->{namelcfirst}Form{columncc}Error))
  386. )));
  387. ';
  388. $output = '';
  389. $search = array('{namelcfirst}', '{column}', '{columnucfirst}', '{columncc}', '{type}');
  390. foreach ($definition['columns'] as $column => $type) {
  391. if ($column == $definition['identifier']) {
  392. continue;
  393. }
  394. $replace = array(strtolower(substr($model, 0, 1)) . substr($model, 1), $column, ucfirst($column), ucfirst(preg_replace('/_(.)/e', 'ucfirst("$1")', $column)), isset($types[strtolower($type)]) ? $types[strtolower($type)] : 'Text');
  395. $output .= str_replace($search, $replace, $code);
  396. }
  397. return $output;
  398. }
  399. protected function getRelationsListCode($definition, $model)
  400. {
  401. $return = array();
  402. foreach ($definition['relations'] as $relation => $data) {
  403. if (empty($data[0]) || empty($data[1]) || empty($data[2])) {
  404. continue;
  405. }
  406. $return[] = '\''.$relation.'\' => array(\''.$data[0].'\', \''.$data[1].'\', \''.$data[2].'\''.(!empty($data[3]) ? ($data[3] === true ? ', true' : ', \''.$data[3].'\'') : '').')';
  407. }
  408. return implode(', ', $return);
  409. }
  410. protected function getRelationsMethodsCode($definition, $model)
  411. {
  412. $code = '
  413. /**
  414. *
  415. * @param mixed $identifier the identifier of the related model or true to return all stored models of this relation
  416. * @return {related_model_or_array}
  417. */
  418. public function get{relation}($identifier = true)
  419. {
  420. return $this->getRelated(\'{relation}\', $identifier = true);
  421. }
  422. /**
  423. *
  424. * @param MiniMVC_Model $identifier the related model
  425. * @param bool $update whether to update the model if it is already stored or not
  426. */
  427. public function set{relation}($identifier = null, $update = true)
  428. {
  429. return $this->setRelated(\'{relation}\', $identifier = null, $update = true);
  430. }
  431. /**
  432. *
  433. * @param mixed $identifier either a model object, an identifier of a related model or true
  434. * @param bool $realDelete whether to delete the model from the database (true) or just from this object(false) defaults to true
  435. * @param bool $realDeleteLoad if the identifier is true only the related models currently assigned to this object will be deleted. with relaDeleteLoad=true, all related models will be deleted
  436. * @param bool $realDeleteCleanRef if relaDeleteLoad is true, set realDeleteCleanRef=true to clean up the ref table (for m:n relations)
  437. */
  438. public function delete{relation}($identifier = true, $realDelete = true, $realDeleteLoad = false, $realDeleteCleanRef = false)
  439. {
  440. return $this->deleteRelated(\'{relation}\', $identifier = true, $realDelete = true, $realDeleteLoad = false, $realDeleteCleanRef = false);
  441. }
  442. /**
  443. *
  444. * @param string $condition the where-condition
  445. * @param array $values values for the placeholders in the condition
  446. * @param string $order the order
  447. * @param int $limit the limit
  448. * @param int $offset the offset
  449. * @return {related_model_or_array}
  450. */
  451. public function load{relation}($condition = null, $values = array(), $order = null, $limit = null, $offset = null)
  452. {
  453. return $this->loadRelated(\'{relation}\', $condition = null, $values = array(), $order = null, $limit = null, $offset = null);
  454. }
  455. /**
  456. *
  457. * @param mixed $identifier a related model object, the identifier of a related model currently asigned to this model or true to save all related models
  458. * @param bool $saveThisOnDemand whether to allow this model to be saved in the database if its new (to generate an auto-increment identifier)
  459. */
  460. public function save{relation}($identifier = true, $saveThisOnDemand = true)
  461. {
  462. return $this->saveRelated(\'{relation}\', $identifier = true, $saveThisOnDemand = true);
  463. }
  464. /**
  465. *
  466. * @param mixed $identifier a related model object, the identifier of a related model
  467. * @param bool $loadRelated whether to load the related object (if identifier is not already loaded and assigned to this model)
  468. */
  469. public function link{relation}($identifier = null, $loadRelated = false)
  470. {
  471. return $this->linkRelated(\'{relation}\', $identifier = null, $loadRelated = false);
  472. }
  473. /**
  474. *
  475. * @param mixed $identifier a related model object, the identifier of a related model or unlink to save all related models
  476. */
  477. public function unlink{relation}($identifier = true)
  478. {
  479. return $this->unlinkRelated(\'{relation}\', $identifier = true);
  480. }
  481. ';
  482. $return = array();
  483. $search = array('{relation}', '{related_model_or_array}');
  484. foreach ($definition['relations'] as $relation => $data) {
  485. $replace = array($relation, isset($data[3]) && $data[3] === true ? $data[0] : $data[0].'Collection');
  486. $return[] = str_replace($search, $replace, $code);
  487. }
  488. return implode("\n", $return);
  489. }
  490. protected function getModelDefinition($module, $model)
  491. {
  492. $modelDefinition = array();
  493. $file = MODULEPATH . $module.'/model/definition.php';
  494. if (file_exists($file)) {
  495. include $file;
  496. }
  497. return isset($modelDefinition[$model]) ? $modelDefinition[$model] : false;
  498. }
  499. }