GeneratorController.php 14 KB

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