GeneratorController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. use Routes\RouteCollection as RouteCollection;
  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. class {{CONTROLLER}} {
  43. function index(){
  44. }
  45. function create(){
  46. }
  47. function Store(){
  48. }
  49. function show({{OBJECT}} \$instance){
  50. }
  51. function edit({{OBJECT}} \$instance){
  52. }
  53. function update({{OBJECT}} \$instance){
  54. }
  55. function destroy({{OBJECT}} \$instance){
  56. }
  57. function purge({{OBJECT}} \$instance){
  58. }
  59. }
  60. EOL;
  61. private $objectBase =
  62. <<<EOL
  63. <?php
  64. use ORM\Entity as Entity;
  65. class {{OBJECT}} extends Entity {
  66. //const _idPolice = Array('type' => '', 'min' => 0, 'max' => 100000, 'step' => 2);
  67. const _tableName = '{{TABLE_NAME}}';
  68. //const _properties = Array('id', 'name');
  69. //const _timestamps = true;
  70. //const _softdelete = true;
  71. //const _connectionName = '';
  72. }
  73. EOL;
  74. private $routeBase =
  75. <<<EOL
  76. <?php
  77. use Routes\RouteCollection as RouteCollection;
  78. RouteCollection::group('/{{PLUGIN_NAME}}', function(){
  79. RouteCollection::get ('/', '{{CONTROLLER}}@index');
  80. RouteCollection::get ('/', '{{CONTROLLER}}@create');
  81. RouteCollection::post ('/', '{{CONTROLLER}}@store');
  82. RouteCollection::get ('/[i:id]', '{{CONTROLLER}}@show');
  83. RouteCollection::get ('/[i:id]/edit', '{{CONTROLLER}}@edit');
  84. RouteCollection::put ('/[i:id]/edit', '{{CONTROLLER}}@update');
  85. RouteCollection::delete('/[i:id]', '{{CONTROLLER}}@destroy');
  86. });
  87. EOL;
  88. function createPluginFromTemplate($subname, $name){
  89. if (is_dir( DIR_APP.$subname . '/' . $name )){
  90. echo 'Treco ja existe';
  91. return;
  92. }else{
  93. mkdir(DIR_APP.$subname . '/' . $name, 0755, true);
  94. mkdir(DIR_APP.$subname . '/' . $name . '/lang', 0755, true);
  95. mkdir(DIR_APP.$subname . '/' . $name . '/classes', 0755, true);
  96. mkdir(DIR_APP.$subname . '/' . $name . '/db', 0755, true);
  97. mkdir(DIR_APP.$subname . '/' . $name . '/views', 0755, true);
  98. }
  99. $plugin_name = $name;
  100. $plugin_name_pretty = ucwords(str_replace('_', ' ', $name));
  101. $object = ucfirst( $name );
  102. $controller = $object.'Controller';
  103. $this->langBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->langBase);
  104. $this->langBase = str_replace("{{PLUGIN_NAME_PRETTY}}", $plugin_name_pretty, $this->langBase);
  105. file_put_contents(DIR_APP.$subname . '/' . $name . '/lang/en.php', $this->langBase);
  106. file_put_contents(DIR_APP.$subname . '/' . $name . '/lang/'.APP_LANG.'.php', $this->langBase);
  107. $this->migrationBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->migrationBase);
  108. file_put_contents(DIR_APP.$subname . '/' . $name . '/db/Migrate.php', $this->migrationBase);
  109. $this->routeBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->routeBase);
  110. $this->routeBase = str_replace("{{CONTROLLER}}", $controller, $this->routeBase);
  111. file_put_contents(DIR_APP.$subname . '/' . $name . '/Routes.php', $this->routeBase);
  112. $this->objectBase = str_replace("{{OBJECT}}", $object, $this->objectBase);
  113. $this->objectBase = str_replace("{{TABLE_NAME}}", $name, $this->objectBase);
  114. file_put_contents(DIR_APP.$subname . '/' . $name . "/classes/$object.php", $this->objectBase);
  115. $this->controllerBase = str_replace("{{OBJECT}}", $object, $this->controllerBase);
  116. $this->controllerBase = str_replace("{{CONTROLLER}}", $controller, $this->controllerBase);
  117. file_put_contents(DIR_APP.$subname . '/' . $name . "/$controller.php", $this->controllerBase);
  118. }
  119. function listRoutes(){
  120. foreach(RouteCollection::returnRoutes() as $route){
  121. //var_dump($route->_callback);continue;//die();
  122. echo implode($route->_verb , ', ') ." || $route->_uri || $route->_weight||";
  123. if( gettype($route->_callback[0]) == 'object' ){
  124. echo 'Clousure ||';
  125. }else{
  126. echo $route->_callback[0];
  127. }
  128. echo "\n";
  129. }
  130. //file_put_contents('/tmp/Route.php', $this->routeBase);
  131. //echo $this->objectBase;
  132. echo APP_LANG;
  133. }
  134. }