GeneratorController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. //https://book.cakephp.org/phinx/0/en/migrations.html#valid-column-types
  15. function {{PLUGIN_NAME}}_upgrade(\$pluginversion) {
  16. if (\$pluginversion < "0.0.1") {
  17. \$table = Wrapper::get_table("{{PLUGIN_NAME}}s");
  18. \$table->addColumn("area_id", "integer", Array("null" => false));
  19. \$table->addColumn("name", "string", Array("null" => false));
  20. \$table->addColumn("description", "text", Array("null" => true));
  21. //\$table->addColumn("time", "timestamp", Array("null" => true, "default"=>null));
  22. \$table->addTimestamps();
  23. \$table->addSoftDelete();
  24. \$table->create();
  25. Migrator::getInstance()->update_plugin_version("{{PLUGIN_NAME}}", "1.0.0");
  26. return;
  27. }
  28. //if (\$pluginversion < "1.0.1") {
  29. // \$table = Wrapper::get_table("{{PLUGIN_NAME}}s");
  30. // Migrator::getInstance()->update_plugin_version("{{PLUGIN_NAME}}", "1.0.1");
  31. // return;
  32. //}
  33. }
  34. function {{PLUGIN_NAME}}_rollback(\$pluginversion) {
  35. if(\$pluginversion > "0.0.1"){
  36. \$table = Wrapper::get_table("{{PLUGIN_NAME}}s");
  37. \$table->drop();
  38. return;
  39. }
  40. }
  41. EOL;
  42. private $versionBase =
  43. <<<EOL
  44. <?php
  45. \$plugin->name = "{{PLUGIN_NAME}}";
  46. \$plugin->version = "0.0.0";
  47. //\$plugin->require = Array( ["name" => "plugin_name", "version" => "x.x.x"] );
  48. EOL;
  49. private $controllerBase =
  50. <<<EOL
  51. <?php
  52. namespace {{NAMESPACE}};
  53. use \App\Core\Template\Output as Output;
  54. use \{{NAMESPACE}}\Classes\{{OBJECT}} as {{OBJECT}};
  55. class {{CONTROLLER}} extends \DefaultController{
  56. protected \$_class = {{OBJECT}}::class;
  57. protected \$_baseUrl = '/{{PLUGIN_NAME}}';
  58. /**
  59. * Index
  60. * Show the main {{OBJECT}} list
  61. */
  62. function index(){
  63. \$list = Array();
  64. for(\$i = 0; \$i <= 10; \$i++){
  65. \$obj = Array();
  66. \$obj['id'] = \$i;
  67. \$obj['value'] = md5(\$i);
  68. \$obj['description'] = sha1(\$i);
  69. \$list[] = \$obj;
  70. }
  71. Output::render('index', Array('list' => \$list));
  72. }
  73. /**
  74. * Create
  75. *
  76. * Render the main {{OBJECT}} formulary
  77. */
  78. function create(){
  79. Output::render('form', ['id' => 0]);
  80. }
  81. /**
  82. * Store
  83. *
  84. * Store the param on the database
  85. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  86. */
  87. function store({{OBJECT}} \${{OBJECT_LOWER}}){
  88. }
  89. /**
  90. * Search
  91. *
  92. * Store the param on the database
  93. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  94. */
  95. function search(){
  96. }
  97. /**
  98. * Show
  99. *
  100. * Render one register
  101. *
  102. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  103. */
  104. function show({{OBJECT}} \${{OBJECT_LOWER}}){
  105. }
  106. /**
  107. * Edit
  108. *
  109. * Render the formular for a database {{OBJECT}}
  110. *
  111. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  112. */
  113. function edit({{OBJECT}} \${{OBJECT_LOWER}}){
  114. Output::render('form', \${{OBJECT_LOWER}});
  115. }
  116. /**
  117. * Update
  118. * Store the changes of the param on the database
  119. *
  120. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  121. */
  122. function update({{OBJECT}} \${{OBJECT_LOWER}}){
  123. }
  124. /**
  125. * Destroy
  126. * If the object has soft delete.
  127. *
  128. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  129. */
  130. function destroy({{OBJECT}} \${{OBJECT_LOWER}}){
  131. }
  132. /**
  133. * Purge
  134. * Remove object even with soft delete.
  135. *
  136. * @param {{OBJECT}} \${{OBJECT_LOWER}}
  137. */
  138. function purge({{OBJECT}} \${{OBJECT_LOWER}}){
  139. }
  140. }
  141. EOL;
  142. private $objectBase =
  143. <<<EOL
  144. <?php
  145. namespace {{NAMESPACE}};
  146. use ORM\Entity as Entity;
  147. class {{OBJECT}} extends Entity {
  148. //const _idPolice = Array("type" => "", "min" => 0, "max" => 100000, "step" => 2);
  149. const _tableName = "{{TABLE_NAME}}s";
  150. /*const _properties = Array(
  151. "id" => ['listable' => true, 'searcheble' => false, 'orderable' => false],
  152. "name" => ['listable' => true, 'searcheble' => true]);
  153. */
  154. const _properties = Array(
  155. 'id',
  156. 'name',
  157. 'description'
  158. );
  159. /*protected \$_ignore = Array(
  160. );*/
  161. const _listable = Array(
  162. 'id',
  163. 'name'
  164. );
  165. const _searchable = Array(
  166. 'name'
  167. );
  168. const _orderable = Array(
  169. );
  170. /*const _datatables = Array(
  171. );*/
  172. //const _timestamps = true;
  173. //const _softdelete = true;
  174. //const _connectionName = "";
  175. }
  176. EOL;
  177. private $routeBase =
  178. <<<EOL
  179. <?php
  180. use App\Core\Template\Output as Output;
  181. use Routes\RouteCollection as RouteCollection;
  182. //Menu itens
  183. RouteCollection::get('*', function() {
  184. Output::addMenu('/{{PLUGIN_NAME}}', '{{PLUGIN_NAME}}', icon('cubes'), ['class' => 'nav-link']);
  185. }, -10)->doIgnore();
  186. RouteCollection::group("/{{PLUGIN_NAME}}", function(){
  187. RouteCollection::get ("/", "\{{NAMESPACE}}\{{CONTROLLER}}@index");
  188. RouteCollection::get ("/form", "\{{NAMESPACE}}\{{CONTROLLER}}@create");
  189. RouteCollection::post ("/", "\{{NAMESPACE}}\{{CONTROLLER}}@store");
  190. RouteCollection::post ("/search", "\{{NAMESPACE}}\{{CONTROLLER}}@search");
  191. RouteCollection::get ("/[i:id]", "\{{NAMESPACE}}\{{CONTROLLER}}@show");
  192. RouteCollection::get ("/[i:id]/edit", "\{{NAMESPACE}}\{{CONTROLLER}}@edit");
  193. RouteCollection::put ("/[i:id]/edit", "\{{NAMESPACE}}\{{CONTROLLER}}@update");
  194. RouteCollection::delete("/[i:id]", "\{{NAMESPACE}}\{{CONTROLLER}}@destroy");
  195. });
  196. EOL;
  197. private $indexTemplate =
  198. <<<EOL
  199. <a href='/{{ENDPOINT}}/form' > Adicionar </a>
  200. EOL;
  201. private $formTemplate =
  202. <<<EOL
  203. {{#id}}
  204. <form method="POST" action="/{{ENDPOINT}}/{{id}}/edit">
  205. <input type="hidden" name="id" value="{{id}}" />
  206. <input type="hidden" name="_method" value="put" />
  207. {{/id}}
  208. {{^id}}
  209. <form method="POST" action="/{{ENDPOINT}}">
  210. <input type="hidden" name="id" value="" />
  211. {{/id}}
  212. <div class='row'>
  213. <div class="col-md-8 col-xs-12 g-3">
  214. <div class="card">
  215. <div class="card-header">Header</div>
  216. <div class="card-body">
  217. <div class="row g-3">
  218. <div class="col-xs-12 col-3">
  219. <label class="form-label">Código</label>
  220. <input type="text" class="form-control" name='code' value="{{code}}">
  221. </div>
  222. <div class="col-xs-12 col-9">
  223. <label class="form-label">Nome</label>
  224. <input type="text" class="form-control" name='name' value="{{name}}">
  225. </div>
  226. </div>
  227. <div class="row">
  228. <div class="col-xs-12 g-3">
  229. <label class="form-label">Descrição</label>
  230. <textarea class="form-control" name="descricao">{{description}}</textarea>
  231. </div>
  232. </div>
  233. </div>
  234. </div>
  235. </div>
  236. <div class="col-md-4 col-xs-12 g-3">
  237. <div class="card">
  238. <div class="card-header">Header</div>
  239. <div class="card-body">
  240. <div class="row g-3">
  241. <div class="col-xs-12 col-3">
  242. <label class="form-label">Código</label>
  243. <input type="text" class="form-control" name='code' value="{{code}}">
  244. </div>
  245. <div class="col-xs-12 col-9">
  246. <label class="form-label">Nome</label>
  247. <input type="text" class="form-control" name='name' value="{{name}}">
  248. </div>
  249. </div>
  250. <div class="row">
  251. <div class="col-xs-12 g-3">
  252. <label class="form-label">Descrição</label>
  253. <textarea class="form-control" name="descricao">{{description}}</textarea>
  254. </div>
  255. </div>
  256. </div>
  257. </div>
  258. </div>
  259. </div>
  260. <div class='row'>
  261. <div class="col-md-8 col-xs-12 g-3">
  262. <div class="card">
  263. <div class="card-header">Header</div>
  264. <div class="card-body">
  265. <div class="row g-3">
  266. <div class="col-xs-12 col-3">
  267. <label class="form-label">Código</label>
  268. <input type="text" class="form-control" name='code' value="{{code}}">
  269. </div>
  270. <div class="col-xs-12 col-9">
  271. <label class="form-label">Nome</label>
  272. <input type="text" class="form-control" name='name' value="{{name}}">
  273. </div>
  274. </div>
  275. <div class="row">
  276. <div class="col-xs-12 g-3">
  277. <label class="form-label">Descrição</label>
  278. <textarea class="form-control" name="descricao">{{description}}</textarea>
  279. </div>
  280. </div>
  281. </div>
  282. </div>
  283. </div>
  284. </div>
  285. </form>
  286. EOL;
  287. private $todoList =
  288. <<<EOL
  289. EOL;
  290. function createPluginFromTemplate($subname, $name){
  291. if (is_dir( DIR_APP.$subname . "/" . $name )){
  292. echo "This module allready exist";
  293. return;
  294. }else{
  295. mkdir(DIR_APP.$subname . "/" . $name, 0755, true);
  296. mkdir(DIR_APP.$subname . "/" . $name . "/lang", 0755, true);
  297. mkdir(DIR_APP.$subname . "/" . $name . "/classes", 0755, true);
  298. mkdir(DIR_APP.$subname . "/" . $name . "/db", 0755, true);
  299. mkdir(DIR_APP.$subname . "/" . $name . "/views", 0755, true);
  300. }
  301. $plugin_name = $name;
  302. $namespace = "App\\".ucfirst($subname)."\\".ucfirst($name);
  303. $plugin_name_pretty = ucwords(str_replace("_", " ", $name));
  304. $object = ucfirst( $name );
  305. $controller = $object."Controller";
  306. $this->langBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->langBase);
  307. $this->langBase = str_replace("{{PLUGIN_NAME_PRETTY}}", $plugin_name_pretty, $this->langBase);
  308. file_put_contents(DIR_APP.$subname . "/" . $name . "/lang/en.php", $this->langBase);
  309. file_put_contents(DIR_APP.$subname . "/" . $name . "/lang/".APP_LANG.".php", $this->langBase);
  310. $this->migrationBase = str_replace("{{PLUGIN_NAME}}", $subname."_".$plugin_name, $this->migrationBase);
  311. file_put_contents(DIR_APP.$subname . "/" . $name . "/db/Migrate.php", $this->migrationBase);
  312. $this->routeBase = str_replace("{{NAMESPACE}}", $namespace, $this->routeBase);
  313. $this->routeBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->routeBase);
  314. $this->routeBase = str_replace("{{CONTROLLER}}", $controller, $this->routeBase);
  315. file_put_contents(DIR_APP.$subname . "/" . $name . "/Routes.php", $this->routeBase);
  316. $this->objectBase = str_replace("{{NAMESPACE}}", $namespace."\Classes", $this->objectBase);
  317. $this->objectBase = str_replace("{{OBJECT}}", $object, $this->objectBase);
  318. $this->objectBase = str_replace("{{TABLE_NAME}}", $name, $this->objectBase);
  319. file_put_contents(DIR_APP.$subname . "/" . $name . "/classes/$object.php", $this->objectBase);
  320. $this->controllerBase = str_replace("{{NAMESPACE}}", $namespace, $this->controllerBase);
  321. $this->controllerBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->controllerBase);
  322. $this->controllerBase = str_replace("{{OBJECT}}", $object, $this->controllerBase);
  323. $this->controllerBase = str_replace("{{OBJECT_LOWER}}",strtolower($object), $this->controllerBase);
  324. $this->controllerBase = str_replace("{{CONTROLLER}}", $controller, $this->controllerBase);
  325. file_put_contents(DIR_APP.$subname . "/" . $name . "/$controller.php", $this->controllerBase);
  326. $this->versionBase = str_replace("{{PLUGIN_NAME}}", $subname."_".$plugin_name, $this->versionBase);
  327. file_put_contents(DIR_APP.$subname . "/" . $name . "/version.php", $this->versionBase);
  328. $this->indexTemplate = str_replace("{{ENDPOINT}}", $plugin_name, $this->indexTemplate);
  329. file_put_contents(DIR_APP.$subname . "/" . $name . "/views/index.mustache", $this->indexTemplate);
  330. $this->formTemplate = str_replace("{{ENDPOINT}}", $plugin_name, $this->formTemplate);
  331. file_put_contents(DIR_APP.$subname . "/" . $name . "/views/form.mustache", $this->formTemplate);
  332. file_put_contents(DIR_APP.$subname . "/" . $name . "/todo.md", $this->todoList);
  333. }
  334. function listRoutes(){
  335. echo str_pad("VERB", 10, " ", STR_PAD_BOTH) . "||";
  336. echo str_pad("URI" , 35, " ", STR_PAD_BOTH) . "||";
  337. echo str_pad("W", 5, " ", STR_PAD_BOTH) . "||";
  338. echo str_pad("FUNCTION", 60, " ", STR_PAD_BOTH) . "\n";
  339. echo str_pad("", 120, "-") . "\n";
  340. foreach(\Routes\RouteCollection::returnRoutes() as $route){
  341. echo str_pad(implode($route->_verb , ", "), 10, " ", STR_PAD_BOTH) . "||";
  342. echo str_pad($route->_uri, 35, " ", STR_PAD_RIGHT) . "||";
  343. echo str_pad($route->_weight, 5, " ", STR_PAD_BOTH). "||";
  344. if( gettype($route->_callback[0]) == "object" ){
  345. echo str_pad("Clousure", 60, " ", STR_PAD_RIGHT);
  346. }else{
  347. echo str_pad($route->_callback[0], 30, " ", STR_PAD_RIGHT);
  348. }
  349. echo "\n";
  350. }
  351. }
  352. }