GeneratorController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace App\Core\Generator;
  3. class GeneratorController {
  4. private $langBase =
  5. <<<EOL
  6. <?php
  7. \$lang["{{PLUGIN_NAME}}"]["module_name"] = "{{PLUGIN_NAME_PRETTY}}";
  8. EOL;
  9. private $migrationBase =
  10. <<<EOL
  11. <?php
  12. use Schema\Wrapper as Wrapper;
  13. function {{PLUGIN_NAME}}_upgrade(\$pluginversion) {
  14. if (\$pluginversion < "0.0.1") {
  15. \$table = Wrapper::get_table("{{PLUGIN_NAME}}");
  16. \$table->addColumn("name", "string", Array("null" => false));
  17. \$table->addColumn("time", "timestamp", Array("null" => true, "default"=>null));
  18. \$table->addColumn("areaid", "integer", Array("null" => false));
  19. \$table->addTimestamps();
  20. \$table->addSoftDelete();
  21. \$table->create();
  22. Migrator::getInstance()->update_plugin_version("{{PLUGIN_NAME}}", "1.0.0");
  23. return;
  24. }
  25. //if (\$pluginversion < "0.0.2") {
  26. //\$table = Wrapper::get_table("{{PLUGIN_NAME}}");
  27. //Migrator::getInstance()->update_plugin_version("{{PLUGIN_NAME}}", "1.0.1");
  28. //return;
  29. //}
  30. }
  31. function {{PLUGIN_NAME}}_rollback(\$pluginversion) {
  32. if(\$pluginversion > "0.0.1"){
  33. \$table = Wrapper::get_table("{{PLUGIN_NAME}}");
  34. \$table->drop();
  35. return;
  36. }
  37. }
  38. EOL;
  39. private $versionBase =
  40. <<<EOL
  41. <?php
  42. \$plugin->name = "{{PLUGIN_NAME}}";
  43. \$plugin->version = "0.0.1";
  44. //\$plugin->require = Array( ["name" => "plugin_name", "version" => "x.x.x"] );
  45. EOL;
  46. private $controllerBase =
  47. <<<EOL
  48. <?php
  49. namespace {{NAMESPACE}};
  50. use \{{NAMESPACE}}\Classes\{{OBJECT}} as {{OBJECT}};
  51. use \App\Core\Template\Output as Output;
  52. class {{CONTROLLER}} {
  53. /**
  54. * Index
  55. * Show the main {{OBJECT}} list
  56. */
  57. function index(){
  58. Output::setView('index');
  59. \$list = Array();
  60. for(\$i = 0; \$i <= 10; \$i++){
  61. \$obj = Array();
  62. \$obj['id'] = \$i;
  63. \$obj['value'] = md5(\$i);
  64. \$obj['description'] = sha1(\$i);
  65. \$list[] = \$obj;
  66. }
  67. Output::addValue('list', \$list );
  68. Output::render();
  69. }
  70. /**
  71. * Create
  72. * Render the main {{OBJECT}} formular
  73. */
  74. function create(){
  75. Output::setView('form', ['id' => 0]);
  76. Output::render();
  77. }
  78. /**
  79. * Store
  80. * Store the param on the database
  81. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  82. */
  83. function store({{OBJECT}} \${{OBJECT_LOWER}}){
  84. }
  85. /**
  86. * Show
  87. * Render one register
  88. *
  89. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  90. */
  91. function show({{OBJECT}} \${{OBJECT_LOWER}}){
  92. }
  93. /**
  94. * Edit
  95. * Render the formular for a database {{OBJECT}}
  96. *
  97. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  98. */
  99. function edit({{OBJECT}} \${{OBJECT_LOWER}}){
  100. }
  101. /**
  102. * Update
  103. * Store the changes of the param on the database
  104. *
  105. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  106. */
  107. function update({{OBJECT}} \${{OBJECT_LOWER}}){
  108. }
  109. /**
  110. * Destroy
  111. * If the object has soft delete.
  112. *
  113. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  114. */
  115. function destroy({{OBJECT}} \${{OBJECT_LOWER}}){
  116. }
  117. /**
  118. * Purge
  119. * Remove object even with soft delete.
  120. *
  121. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  122. */
  123. function purge({{OBJECT}} \${{OBJECT_LOWER}}){
  124. }
  125. }
  126. EOL;
  127. private $objectBase =
  128. <<<EOL
  129. <?php
  130. namespace {{NAMESPACE}};
  131. use ORM\Entity as Entity;
  132. class {{OBJECT}} extends Entity {
  133. //const _idPolice = Array("type" => "", "min" => 0, "max" => 100000, "step" => 2);
  134. const _tableName = "{{TABLE_NAME}}";
  135. //const _properties = Array("id", "name");
  136. //const _timestamps = true;
  137. //const _softdelete = true;
  138. //const _connectionName = "";
  139. }
  140. EOL;
  141. private $routeBase =
  142. <<<EOL
  143. <?php
  144. use Routes\RouteCollection as RouteCollection;
  145. RouteCollection::group("/{{PLUGIN_NAME}}", function(){
  146. RouteCollection::get ("/", "\{{NAMESPACE}}\{{CONTROLLER}}@index");
  147. RouteCollection::get ("/form", "\{{NAMESPACE}}\{{CONTROLLER}}@create");
  148. RouteCollection::post ("/", "\{{NAMESPACE}}\{{CONTROLLER}}@store");
  149. RouteCollection::get ("/[i:id]", "\{{NAMESPACE}}\{{CONTROLLER}}@show");
  150. RouteCollection::get ("/[i:id]/edit", "\{{NAMESPACE}}\{{CONTROLLER}}@edit");
  151. RouteCollection::put ("/[i:id]/edit", "\{{NAMESPACE}}\{{CONTROLLER}}@update");
  152. RouteCollection::delete("/[i:id]", "\{{NAMESPACE}}\{{CONTROLLER}}@destroy");
  153. });
  154. EOL;
  155. private $indexTemplate =
  156. <<<EOL
  157. <a href='{{ENDPOINT}}/form' >Adicionar </a>
  158. <div class="table-responsive">
  159. <table class='table'>
  160. <thead>
  161. <tr>
  162. <th>Id</th>
  163. <th>Value</th>
  164. <th>Description</th>
  165. <th>Actions</th>
  166. </tr>
  167. </thead>
  168. {{#list}}
  169. <tr class="table-dark" data-id='{{id}}'>
  170. <td>{{id}}</td>
  171. <td>{{value}}</td>
  172. <td>{{description}}</td>
  173. <td>
  174. <a href="{{ENDPOINT}}/edit"> <i class="fa fa-pencil"></i></a>
  175. <a href="{{ENDPOINT}}/purge"> <i class="fa fa-fire"></i></a>
  176. <a href="{{ENDPOINT}}/"> <i class="fa fa-plus"></i></a>
  177. <a href="{{ENDPOINT}}/destroy"> <i class="fa fa-trash-o"></i></a>
  178. </td>
  179. </tr>
  180. {{/list}}
  181. </table>
  182. </div>
  183. EOL;
  184. private $formTemplate =
  185. <<<EOL
  186. {{@ if( {{id}} ): @}}
  187. <form method="POST" action="{{ENDPOINT}}/{{id}}/edit">
  188. <input type="hidden" name="id" value="{{id}}" />
  189. <input type="hidden" name="_method" value="put" />
  190. {{@ else: @}}
  191. <form method="POST" action="{{ENDPOINT}}">
  192. <input type="hidden" name="id" value="" />
  193. {{@ endif; @}}
  194. </form>
  195. EOL;
  196. private $ctagsHeader =
  197. <<<EOL
  198. !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
  199. !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
  200. !_TAG_OUTPUT_FILESEP slash /slash or backslash/
  201. !_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
  202. !_TAG_PROGRAM_AUTHOR Universal Ctags Team //
  203. !_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
  204. !_TAG_PROGRAM_URL https://ctags.io/ /official site/
  205. !_TAG_PROGRAM_VERSION 0.0.0 /a3c87ab5/
  206. EOL;
  207. function createPluginFromTemplate($subname, $name){
  208. if (is_dir( DIR_APP.$subname . "/" . $name )){
  209. echo "Treco ja existe";
  210. return;
  211. }else{
  212. mkdir(DIR_APP.$subname . "/" . $name, 0755, true);
  213. mkdir(DIR_APP.$subname . "/" . $name . "/lang", 0755, true);
  214. mkdir(DIR_APP.$subname . "/" . $name . "/classes", 0755, true);
  215. mkdir(DIR_APP.$subname . "/" . $name . "/db", 0755, true);
  216. mkdir(DIR_APP.$subname . "/" . $name . "/views", 0755, true);
  217. }
  218. $plugin_name = $name;
  219. $namespace = "App\\".ucfirst($subname)."\\".ucfirst($name);
  220. $plugin_name_pretty = ucwords(str_replace("_", " ", $name));
  221. $object = ucfirst( $name );
  222. $controller = $object."Controller";
  223. $this->langBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->langBase);
  224. $this->langBase = str_replace("{{PLUGIN_NAME_PRETTY}}", $plugin_name_pretty, $this->langBase);
  225. file_put_contents(DIR_APP.$subname . "/" . $name . "/lang/en.php", $this->langBase);
  226. file_put_contents(DIR_APP.$subname . "/" . $name . "/lang/".APP_LANG.".php", $this->langBase);
  227. $this->migrationBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->migrationBase);
  228. file_put_contents(DIR_APP.$subname . "/" . $name . "/db/Migrate.php", $this->migrationBase);
  229. $this->routeBase = str_replace("{{NAMESPACE}}", $namespace, $this->routeBase);
  230. $this->routeBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->routeBase);
  231. $this->routeBase = str_replace("{{CONTROLLER}}", $controller, $this->routeBase);
  232. file_put_contents(DIR_APP.$subname . "/" . $name . "/Routes.php", $this->routeBase);
  233. $this->objectBase = str_replace("{{NAMESPACE}}", $namespace."\Classes", $this->objectBase);
  234. $this->objectBase = str_replace("{{OBJECT}}", $object, $this->objectBase);
  235. $this->objectBase = str_replace("{{TABLE_NAME}}", $name, $this->objectBase);
  236. file_put_contents(DIR_APP.$subname . "/" . $name . "/classes/$object.php", $this->objectBase);
  237. $this->controllerBase = str_replace("{{NAMESPACE}}", $namespace, $this->controllerBase);
  238. $this->controllerBase = str_replace("{{OBJECT}}", $object, $this->controllerBase);
  239. $this->controllerBase = str_replace("{{OBJECT_LOWER}}",strtolower($object), $this->controllerBase);
  240. $this->controllerBase = str_replace("{{CONTROLLER}}", $controller, $this->controllerBase);
  241. file_put_contents(DIR_APP.$subname . "/" . $name . "/$controller.php", $this->controllerBase);
  242. $this->versionBase = str_replace("{{PLUGIN_NAME}}", $subname."_".$plugin_name, $this->versionBase);
  243. file_put_contents(DIR_APP.$subname . "/" . $name . "/version.php", $this->versionBase);
  244. $this->indexTemplate = str_replace("{{ENDPOINT}}", $plugin_name, $this->indexTemplate);
  245. file_put_contents(DIR_APP.$subname . "/" . $name . "/views/index.mustache", $this->indexTemplate);
  246. $this->formTemplate = str_replace("{{ENDPOINT}}", $plugin_name, $this->formTemplate);
  247. file_put_contents(DIR_APP.$subname . "/" . $name . "/views/form.mustache", $this->formTemplate);
  248. }
  249. function listRoutes(){
  250. echo str_pad("VERB", 10, " ", STR_PAD_BOTH) . "||";
  251. echo str_pad("URI" , 35, " ", STR_PAD_BOTH) . "||";
  252. echo str_pad("W", 5, " ", STR_PAD_BOTH) . "||";
  253. echo str_pad("FUNCTION", 60, " ", STR_PAD_BOTH) . "\n";
  254. echo str_pad("", 120, "-") . "\n";
  255. foreach(\Routes\RouteCollection::returnRoutes() as $route){
  256. echo str_pad(implode($route->_verb , ", "), 10, " ", STR_PAD_BOTH) . "||";
  257. echo str_pad($route->_uri, 35, " ", STR_PAD_RIGHT) . "||";
  258. echo str_pad($route->_weight, 5, " ", STR_PAD_BOTH). "||";
  259. if( gettype($route->_callback[0]) == "object" ){
  260. echo str_pad("Clousure", 60, " ", STR_PAD_RIGHT);
  261. }else{
  262. echo str_pad($route->_callback[0], 30, " ", STR_PAD_RIGHT);
  263. }
  264. echo "\n";
  265. }
  266. }
  267. function createTags(){
  268. $outputs = shell_exec("grep -Rnsi '@ctag' . | sed s/\ //g | sed s/*@ctag/\ /g");
  269. $outputs = preg_split("/\R/", $outputs);
  270. $return = $this->ctagsHeader . "\n";
  271. foreach($outputs as $output){
  272. $line = explode(" ", str_replace("\\t", "", $output) );
  273. @$line = $line[1] . "\t " . str_replace("./", "", explode(":", $line[0])[0]) . "\t ".$line[1] . "\n";
  274. if( strpos($line, "app/core/generator/GeneratorController.php") ){
  275. continue;
  276. }
  277. $return .= $line;
  278. //echo "Generating: ".$line."\n";
  279. }
  280. file_put_contents(DIR_ROOT."tags", $return);
  281. }
  282. }