GeneratorController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 < '1.0.0') {
  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 < '1.0.1') {
  26. //\$table = Wrapper::get_table('{{PLUGIN_NAME}}');
  27. //Migrator::getInstance()->update_plugin_version('{{PLUGIN_NAME}}', '1.0.1');
  28. //return;
  29. //}
  30. }
  31. EOL;
  32. private $versionBase =
  33. <<<EOL
  34. <?php
  35. \$plugin->name = '{{PLUGIN_NAME}}';
  36. \$plugin->version = '0.0.1';
  37. //\$plugin->require = Array( ["name" => "plugin_name", "version" => 'x.x.x'] );
  38. EOL;
  39. private $controllerBase =
  40. <<<EOL
  41. <?php
  42. namespace {{NAMESPACE}};
  43. use \{{NAMESPACE}}\Classes\{{OBJECT}} as {{OBJECT}};
  44. class {{CONTROLLER}} {
  45. function index(){
  46. }
  47. function create(){
  48. }
  49. function Store(){
  50. }
  51. function show({{OBJECT}} \{{OBJECT_LOWER}}){
  52. }
  53. function edit({{OBJECT}} \{{OBJECT_LOWER}}){
  54. }
  55. function update({{OBJECT}} \{{OBJECT_LOWER}}){
  56. }
  57. function destroy({{OBJECT}} \{{OBJECT_LOWER}}){
  58. }
  59. function purge({{OBJECT}} \{{OBJECT_LOWER}}){
  60. }
  61. }
  62. EOL;
  63. private $objectBase =
  64. <<<EOL
  65. <?php
  66. namespace {{NAMESPACE}};
  67. use ORM\Entity as Entity;
  68. class {{OBJECT}} extends Entity {
  69. //const _idPolice = Array('type' => '', 'min' => 0, 'max' => 100000, 'step' => 2);
  70. const _tableName = '{{TABLE_NAME}}';
  71. //const _properties = Array('id', 'name');
  72. //const _timestamps = true;
  73. //const _softdelete = true;
  74. //const _connectionName = '';
  75. }
  76. EOL;
  77. private $routeBase =
  78. <<<EOL
  79. <?php
  80. use Routes\RouteCollection as RouteCollection;
  81. RouteCollection::group('/{{PLUGIN_NAME}}', function(){
  82. RouteCollection::get ('/', '\{{NAMESPACE}}\{{CONTROLLER}}@index');
  83. RouteCollection::get ('/', '\{{NAMESPACE}}\{{CONTROLLER}}@create');
  84. RouteCollection::post ('/', '\{{NAMESPACE}}\{{CONTROLLER}}@store');
  85. RouteCollection::get ('/[i:id]', '\{{NAMESPACE}}\{{CONTROLLER}}@show');
  86. RouteCollection::get ('/[i:id]/edit', '\{{NAMESPACE}}\{{CONTROLLER}}@edit');
  87. RouteCollection::put ('/[i:id]/edit', '\{{NAMESPACE}}\{{CONTROLLER}}@update');
  88. RouteCollection::delete('/[i:id]', '\{{NAMESPACE}}\{{CONTROLLER}}@destroy');
  89. });
  90. EOL;
  91. private $ctagsHeader =
  92. <<<EOL
  93. !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
  94. !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
  95. !_TAG_OUTPUT_FILESEP slash /slash or backslash/
  96. !_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
  97. !_TAG_PROGRAM_AUTHOR Universal Ctags Team //
  98. !_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
  99. !_TAG_PROGRAM_URL https://ctags.io/ /official site/
  100. !_TAG_PROGRAM_VERSION 0.0.0 /a3c87ab5/
  101. EOL;
  102. function createPluginFromTemplate($subname, $name){
  103. if (is_dir( DIR_APP.$subname . '/' . $name )){
  104. echo 'Treco ja existe';
  105. return;
  106. }else{
  107. mkdir(DIR_APP.$subname . '/' . $name, 0755, true);
  108. mkdir(DIR_APP.$subname . '/' . $name . '/lang', 0755, true);
  109. mkdir(DIR_APP.$subname . '/' . $name . '/classes', 0755, true);
  110. mkdir(DIR_APP.$subname . '/' . $name . '/db', 0755, true);
  111. mkdir(DIR_APP.$subname . '/' . $name . '/views', 0755, true);
  112. }
  113. $plugin_name = $name;
  114. $namespace = "App\\".ucfirst($subname).'\\'.ucfirst($name);
  115. $plugin_name_pretty = ucwords(str_replace('_', ' ', $name));
  116. $object = ucfirst( $name );
  117. $controller = $object.'Controller';
  118. $this->langBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->langBase);
  119. $this->langBase = str_replace("{{PLUGIN_NAME_PRETTY}}", $plugin_name_pretty, $this->langBase);
  120. file_put_contents(DIR_APP.$subname . '/' . $name . '/lang/en.php', $this->langBase);
  121. file_put_contents(DIR_APP.$subname . '/' . $name . '/lang/'.APP_LANG.'.php', $this->langBase);
  122. $this->migrationBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->migrationBase);
  123. file_put_contents(DIR_APP.$subname . '/' . $name . '/db/Migrate.php', $this->migrationBase);
  124. $this->routeBase = str_replace("{{NAMESPACE}}", $namespace, $this->routeBase);
  125. $this->routeBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->routeBase);
  126. $this->routeBase = str_replace("{{CONTROLLER}}", $controller, $this->routeBase);
  127. file_put_contents(DIR_APP.$subname . '/' . $name . '/Routes.php', $this->routeBase);
  128. $this->objectBase = str_replace("{{NAMESPACE}}", $namespace.'\Classes', $this->objectBase);
  129. $this->objectBase = str_replace("{{OBJECT}}", $object, $this->objectBase);
  130. $this->objectBase = str_replace("{{TABLE_NAME}}", $name, $this->objectBase);
  131. file_put_contents(DIR_APP.$subname . '/' . $name . "/classes/$object.php", $this->objectBase);
  132. $this->controllerBase = str_replace("{{NAMESPACE}}", $namespace, $this->controllerBase);
  133. $this->controllerBase = str_replace("{{OBJECT}}", $object, $this->controllerBase);
  134. $this->controllerBase = str_replace("{{OBJECT_LOWER}}",strlower($object), $this->controllerBase);
  135. $this->controllerBase = str_replace("{{CONTROLLER}}", $controller, $this->controllerBase);
  136. file_put_contents(DIR_APP.$subname . '/' . $name . "/$controller.php", $this->controllerBase);
  137. }
  138. function listRoutes(){
  139. echo str_pad('VERB', 10, ' ', STR_PAD_BOTH) . "||";
  140. echo str_pad('URI' , 35, ' ', STR_PAD_BOTH) . "||";
  141. echo str_pad('W', 5, ' ', STR_PAD_BOTH) . "||";
  142. echo str_pad('FUNCTION', 60, ' ', STR_PAD_BOTH) . "\n";
  143. echo str_pad('', 120, '-') . "\n";
  144. foreach(\Routes\RouteCollection::returnRoutes() as $route){
  145. echo str_pad(implode($route->_verb , ', '), 10, ' ', STR_PAD_BOTH) . "||";
  146. echo str_pad($route->_uri, 35, ' ', STR_PAD_RIGHT) . "||";
  147. echo str_pad($route->_weight, 5, ' ', STR_PAD_BOTH). "||";
  148. if( gettype($route->_callback[0]) == 'object' ){
  149. echo str_pad('Clousure', 60, ' ', STR_PAD_RIGHT);
  150. }else{
  151. echo str_pad($route->_callback[0], 30, ' ', STR_PAD_RIGHT);
  152. }
  153. echo "\n";
  154. }
  155. }
  156. function createTags(){
  157. $outputs = shell_exec("grep -Rnsi '@ctag' . | sed s/\ //g | sed s/*@ctag/\ /g");
  158. $outputs = preg_split('/\R/', $outputs);
  159. $return = $this->ctagsHeader . "\n";
  160. foreach($outputs as $output){
  161. $line = explode(' ', str_replace('\\t', '', $output) );
  162. @$line = $line[1] . "\t " . str_replace('./', '', explode(':', $line[0])[0]) . "\t ".$line[1] . "\n";
  163. if( strpos($line, 'app/core/generator/GeneratorController.php') ){
  164. continue;
  165. }
  166. $return .= $line;
  167. //echo "Generating: ".$line."\n";
  168. }
  169. file_put_contents(DIR_ROOT.'tags', $return);
  170. }
  171. }