PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/module/Mongo/controller/Generate.php

https://github.com/tquensen/MiniMVC
PHP | 370 lines | 336 code | 34 blank | 0 comment | 27 complexity | 530f09b576e3590009ec3ee66bdace63 MD5 | raw file
  1. <?php
  2. class Mongo_Generate_Controller extends MiniMVC_Controller
  3. {
  4. public function modelAction($params)
  5. {
  6. if (!$params['module']) {
  7. return 'Kein Modul angegeben!' . "\n";
  8. }
  9. if (!$params['model']) {
  10. return 'Kein Modelnamen angegeben!' . "\n";
  11. }
  12. $definition = $this->getModelDefinition($params['module'], $params['model']);
  13. if (!$definition) {
  14. return 'No model definition found for '.$params['model'].' in module '.$params['module'].'...' . "\n";
  15. }
  16. if (!isset($definition['mapReduce'])) {
  17. $definition['mapReduce'] = false;
  18. }
  19. if ($definition['mapReduce']) {
  20. if (!isset($definition['columns']['_id'])) {
  21. $definition['columns']['_id'] = 'mixed';
  22. }
  23. if (!isset($definition['columns']['value'])) {
  24. $definition['columns']['value'] = 'mixed';
  25. }
  26. } elseif (!isset($definition['columns']['_id'])) {
  27. $definition['columns']['_id'] = 'MongoId';
  28. }
  29. if (!isset($definition['relations'])) {
  30. $definition['relations'] = array();
  31. }
  32. if (!isset($definition['embedded'])) {
  33. $definition['embedded'] = array();
  34. }
  35. if (!isset($definition['autoIncrement'])) {
  36. $definition['autoIncrement'] = true;
  37. }
  38. $model = ucfirst($params['model']);
  39. $search = array(
  40. '{name}',
  41. '{module}',
  42. '{modlc}',
  43. '{namelcfirst}',
  44. '{table}',
  45. '{columns_list}',
  46. '{columns_phpdoc}',
  47. '{columns_form}',
  48. '{relations_methods}',
  49. '{relations_list}',
  50. '{embedded_methods}',
  51. '{embedded_list}',
  52. '{auto_increment}',
  53. '{parent_model}'
  54. );
  55. $replace = array(
  56. $model,
  57. $params['module'],
  58. strtolower($params['module']),
  59. strtolower(substr($model, 0, 1)) . substr($model, 1),
  60. strtolower(preg_replace('/(?!^)[[:upper:]]+/', '_$0', $model)),
  61. '\''.implode('\', \'', array_keys($definition['columns'])).'\'',
  62. $this->getPhpDocCode($definition, $model),
  63. $this->getFormCode($definition, $model),
  64. $this->getRelationsMethodsCode($definition, $model),
  65. $this->getRelationsListCode($definition, $model),
  66. $this->getEmbeddedMethodsCode($definition, $model),
  67. $this->getEmbeddedListCode($definition, $model),
  68. (bool) $definition['autoIncrement'] ? 'true' : 'false',
  69. $definition['mapReduce'] ? 'Mongo_MapReduceModel' : 'Mongo_Model'
  70. );
  71. $path = MODULEPATH . $params['module'].'/model';
  72. $dummy = MODULEPATH . 'Mongo/dummies';
  73. $message = 'Erstelle Models...'."\n";
  74. if (!file_exists($path . '/'.$model.'.php')) {
  75. file_put_contents($path . '/'.$model.'.php', str_replace($search, $replace, file_get_contents($dummy . '/Model.php')));
  76. $message .= '-> Datei '.$model.'.php erstellt'."\n";
  77. } else {
  78. $message .= '-> Datei '.$model.'.php existiert bereits'."\n";
  79. }
  80. if (!file_exists($path . '/'.$model.'Repository.php')) {
  81. file_put_contents($path . '/'.$model.'Repository.php', str_replace($search, $replace, file_get_contents($dummy . ($definition['mapReduce'] ? '/MapReduceRepository.php' : '/Repository.php'))));
  82. $message .= '-> Datei '.$model.'Repository.php erstellt'."\n";
  83. } else {
  84. $message .= '-> Datei '.$model.'Repository.php existiert bereits'."\n";
  85. }
  86. return $message;
  87. }
  88. public function embeddedAction($params)
  89. {
  90. if (!$params['module']) {
  91. return 'Kein Modul angegeben!' . "\n";
  92. }
  93. if (!$params['model']) {
  94. return 'Kein Modelnamen angegeben!' . "\n";
  95. }
  96. $model = ucfirst($params['model']);
  97. $search = array(
  98. '{name}'
  99. );
  100. $replace = array(
  101. $model
  102. );
  103. $path = MODULEPATH . $params['module'].'/model';
  104. $dummy = MODULEPATH . 'Mongo/dummies';
  105. $message = 'Erstelle Model...'."\n";
  106. if (!file_exists($path . '/'.$model.'.php')) {
  107. file_put_contents($path . '/'.$model.'.php', str_replace($search, $replace, file_get_contents($dummy . '/Embedded.php')));
  108. $message .= '-> Datei '.$model.'.php erstellt'."\n";
  109. } else {
  110. $message .= '-> Datei '.$model.'.php existiert bereits'."\n";
  111. }
  112. return $message;
  113. }
  114. protected function getPhpDocCode($definition, $model)
  115. {
  116. $return = '';
  117. foreach ($definition['columns'] as $column => $type) {
  118. $return .= '@property ' . $type . ' $' . $column . "\n * ";
  119. }
  120. return $return;
  121. }
  122. protected function getFormCode($definition, $model)
  123. {
  124. $types = array(
  125. 'int' => 'Text',
  126. 'integer' => 'Text',
  127. 'bool' => 'Checkbox',
  128. 'boolean' => 'Checkbox',
  129. 'float' => 'Text',
  130. 'double' => 'Text',
  131. 'string' => 'Text',
  132. 'array' => 'Text'
  133. );
  134. $code = '$form->setElement(new MiniMVC_Form_Element_{type}(\'{column}\',
  135. array(\'label\' => $i18n->{namelcfirst}Form{columncc}Label),
  136. array(
  137. //new MiniMVC_Form_Validator_Required(array(\'errorMessage\' => $i18n->{namelcfirst}Form{columncc}Error))
  138. )));
  139. ';
  140. $output = '';
  141. $search = array('{namelcfirst}', '{column}', '{columnucfirst}', '{columncc}', '{type}');
  142. foreach ($definition['columns'] as $column => $type) {
  143. if ($column == '_id') {
  144. continue;
  145. }
  146. $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');
  147. $output .= str_replace($search, $replace, $code);
  148. }
  149. return $output;
  150. }
  151. protected function getRelationsListCode($definition, $model)
  152. {
  153. $return = array();
  154. foreach ($definition['relations'] as $relation => $data) {
  155. if (empty($data[0]) || empty($data[1]) || empty($data[2])) {
  156. continue;
  157. }
  158. $return[] = '\''.$relation.'\' => array(\''.$data[0].'\', \''.$data[1].'\', \''.$data[2].'\''.(!empty($data[3]) ? ($data[3] === true ? ', true' : ', \''.$data[3].'\'') : '').')';
  159. }
  160. return implode(', ', $return);
  161. }
  162. protected function getEmbeddedListCode($definition, $model)
  163. {
  164. $return = array();
  165. foreach ($definition['embedded'] as $relation => $data) {
  166. if (empty($data[0]) || empty($data[1]) || empty($data[2])) {
  167. continue;
  168. }
  169. $return[] = '\''.$relation.'\' => array(\''.$data[0].'\', \''.$data[1].'\', \''.$data[2].'\''.(!empty($data[3]) ? ', true' : '').')';
  170. }
  171. return implode(', ', $return);
  172. }
  173. protected function getRelationsMethodsCode($definition, $model)
  174. {
  175. $code1 = '
  176. /**
  177. *
  178. * @return {foreignModel}|null
  179. */
  180. public function get{relation}()
  181. {
  182. return $this->getRelated(\'{relation}\');
  183. }
  184. /**
  185. * @param {foreignModel}|mixed $related either a {foreignModel} object or a {foreignModel}->_id-value
  186. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  187. * @return bool
  188. */
  189. public function set{relation}($related, $save = true)
  190. {
  191. return $this->setRelated(\'{relation}\', $related, $save = true);
  192. }
  193. /**
  194. * @param boolean $delete true to delete the related entry from the database, false to only remove the relation (default false)
  195. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  196. * @return bool
  197. */
  198. public function remove{relation}($delete = false, $save = true)
  199. {
  200. return $this->removeRelated(\'{relation}\', true, $delete, $save);
  201. }
  202. ';
  203. $code2 = '
  204. /**
  205. *
  206. * @param array $query Additional fields to filter.
  207. * @param array $sort The fields by which to sort.
  208. * @param int $limit The number of results to return.
  209. * @param int $skip The number of results to skip.
  210. * @return array
  211. */
  212. public function get{relation}($query = array(), $sort = array(), $limit = null, $skip = null)
  213. {
  214. return $this->getRelated(\'{relation}\', $query, $sort, $limit, $skip);
  215. }
  216. /**
  217. * @param {foreignModel}|mixed $related either a {foreignModel} object, a {foreignModel}->_id-value or an array with multiple {foreignModel}s
  218. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  219. * @param bool $multiple true to store multiple related as array (m:n), false to only store a single value (1:1, n:1, default)
  220. * @return bool
  221. */
  222. public function set{relation}($related, $save = true, $multiple = false)
  223. {
  224. return $this->setRelated(\'{relation}\', $related, $save, $multiple);
  225. }
  226. /**
  227. * @param {foreignModel}|mixed $related true to remove all objects or either a {foreignModel} object, a {foreignModel}->_id-value or an array with multiple {foreignModel}s
  228. * @param boolean $delete true to delete the related entry from the database, false to only remove the relation (default false)
  229. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  230. * @return bool
  231. */
  232. public function remove{relation}($related = true, $delete = false, $save = true)
  233. {
  234. return $this->removeRelated(\'{relation}\', $related, $delete, $save);
  235. }
  236. ';
  237. $return = array();
  238. $search = array('{relation}', '{foreignModel}', '{localProperty}', '{foreignProperty}');
  239. foreach ($definition['relations'] as $relation => $data) {
  240. $replace = array($relation, $data[0], $data[1], $data[2]);
  241. $return[] = str_replace($search, $replace, !empty($data[3]) ? $code1 : $code2);
  242. }
  243. return implode("\n", $return);
  244. }
  245. protected function getEmbeddedMethodsCode($definition, $model)
  246. {
  247. $code1 = '
  248. /**
  249. *
  250. * @return {foreignModel}|null
  251. */
  252. public function get{relation}()
  253. {
  254. return $this->getEmbedded(\'{relation}\');
  255. }
  256. /**
  257. *
  258. * @param {foreignModel}|array $data a {foreignModel} object or an array representing a {foreignModel}
  259. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  260. * @return bool
  261. */
  262. public function set{relation}($data, $save = true)
  263. {
  264. return $this->setEmbedded(\'{relation}\', $data);
  265. }
  266. /**
  267. * removes the relation to {foreignModel}
  268. *
  269. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  270. * @return bool
  271. */
  272. public function remove{relation}($save = true)
  273. {
  274. return $this->removeEmbedded(\'{relation}\', true, $save);
  275. }
  276. ';
  277. $code2 = '
  278. /**
  279. *
  280. * @param int|bool $key the identifier of a embedded or true to return all
  281. * @param string $sortBy (optional) if $key == true, order the entries by this property, null to keep the db order
  282. * @param bool $sortDesc false (default) to sort ascending, true to sort descending
  283. * @return {foreignModel}|array
  284. */
  285. public function get{relation}($key = true, $sortBy = null, $sortDesc = false)
  286. {
  287. return $this->getEmbedded(\'{relation}\', $key, $sortBy, $sortDesc);
  288. }
  289. /**
  290. *
  291. * @param {foreignModel}|array $data a {foreignModel} object or an array representing a {foreignModel} or an array with multiple {foreignModel}
  292. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  293. * @return bool
  294. */
  295. public function set{relation}($data, $save = true)
  296. {
  297. return $this->setEmbedded(\'{relation}\', $data, $save);
  298. }
  299. /**
  300. * removes the chosen {foreignModel}s (or all for $key = true) from the embedded list
  301. *
  302. * @param mixed $key one or more keys for {foreignModel} objects or true to remove all
  303. * @param mixed $save set to null to prevent a save() call, otherwise call save($save)
  304. * @return bool
  305. */
  306. public function remove{relation}($key = true, $save = true)
  307. {
  308. return $this->removeEmbedded(\'{relation}\', $key, $save);
  309. }
  310. ';
  311. $return = array();
  312. $search = array('{relation}', '{foreignModel}', '{localProperty}', '{foreignProperty}');
  313. foreach ($definition['embedded'] as $relation => $data) {
  314. $replace = array($relation, $data[0], $data[1], !empty($data[2]) ? $data[2] : null);
  315. $return[] = str_replace($search, $replace, !empty($data[3]) ? $code1 : $code2);
  316. }
  317. return implode("\n", $return);
  318. }
  319. protected function getModelDefinition($module, $model)
  320. {
  321. $modelDefinition = array();
  322. $file = MODULEPATH . $module.'/model/definition.php';
  323. if (file_exists($file)) {
  324. include $file;
  325. }
  326. return isset($modelDefinition[$model]) ? $modelDefinition[$model] : false;
  327. }
  328. }