GeneratorController.php 14 KB

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