Sfoglia il codice sorgente

Adicionando mudancas gerador

Artur Welp 5 anni fa
parent
commit
b66dcff003

+ 171 - 62
application/module/core/generator/GeneratorController.php

@@ -1,75 +1,184 @@
 <?php
 
-use PhpSchool\CliMenu\CliMenu;
-use PhpSchool\CliMenu\Builder\CliMenuBuilder;
+use Routes\RouteCollection as RouteCollection;
 
 class GeneratorController {
+    
+    private $langBase = 
+            <<<EOL
+            <?php
+                \$lang['{{PLUGIN_NAME}}']['module_name'] = '{{PLUGIN_NAME_PRETTY}}';
+            EOL;
+    
+    private $migrationBase = 
+            <<<EOL
+            <?php
 
-    //The folder to be created
-    private $name_path = '';
-    //The tables to be created
-    private $tables = Array();
+            use Schema\Wrapper as Wrapper;
 
-    function menu() {
-
-        $default_behavior = function (CliMenu $clm) {
+            function {{PLUGIN_NAME}}_upgrade(\$pluginversion) {
+                if (\$pluginversion < '1.0.0') {
+                    \$table = Wrapper::get_table('{{PLUGIN_NAME}}');
+                    \$table->addColumn("name", 'string', Array('null' => false));
+                    \$table->addColumn("time", 'timestamp', Array('null' => true, 'default'=>null));
+                    \$table->addColumn("areaid", 'integer', Array('null' => false));
+                    \$table->addTimestamps();
+                    \$table->addSoftDelete();
+                    \$table->create();
+        
+                    Migrator::getInstance()->update_plugin_version('{{PLUGIN_NAME}}', '1.0.0');
+                    return;
+                }
             
-        };
-
-        $module_name = function(CliMenu $clm) {
-
-            $result = $clm->askText()
-                    ->setPlaceholderText('Enter something here')
-                    ->ask();
-
-            $this->name_path = $result->fetch();
-            $clm->getSelectedItem()->showItemExtra();
-        };
+                //if (\$pluginversion < '1.0.1') {
+                    //\$table = Wrapper::get_table('{{PLUGIN_NAME}}');
+                    //Migrator::getInstance()->update_plugin_version('{{PLUGIN_NAME}}', '1.0.1');
+                    //return;
+                //}
+            }
+            EOL;
+    
+    private $versionBase = 
+            <<<EOL
+            <?php
 
-        $show_info = function(CliMenu $clm) {
-            $clm->flash('Module name and path \n' . $this->name_path)->display();
-        };
+                \$plugin->name = '{{PLUGIN_NAME}}';
+                \$plugin->version = '0.0.1';
+            
+                //\$plugin->require = Array( ["name" => "plugin_name", "version" => 'x.x.x'] );
+            EOL;
+    
+    private $controllerBase = 
+            <<<EOL
+            <?php
+                class {{CONTROLLER}} {
+            
+                    function index(){
+            
+                    }
+            
+                    function create(){
+            
+                    }
+            
+                    function Store(){
+            
+                    }
+            
+                    function show({{OBJECT}} \$instance){
+            
+                    }
+            
+                    function edit({{OBJECT}} \$instance){
+            
+                    }
+            
+                    function update({{OBJECT}} \$instance){
+            
+                    }
+            
+                    function destroy({{OBJECT}} \$instance){
+            
+                    }
+            
+                    function purge({{OBJECT}} \$instance){
+            
+                    }
+                }
+            EOL;
+    
+    
+    private $objectBase = 
+            <<<EOL
+            <?php
+                use ORM\Entity as Entity;
 
-        /* $menu = (new CliMenuBuilder)
-          ->setTitle(Lang::getString('main_title', 'generator'))
-          ->addItem(Lang::getString('name_definition', 'generator'), $module_name)
-          ->addItem(Lang::getString('show_info', 'generator'), $show_info)
-          ->setItemExtra('[COMPLETE!]')
-          ->addLineBreak('-')
-          ->setBorder(1, 2, 'yellow')
-          ->setPadding(2, 4)
-          //->setMarginAuto()
-          ->build();
+                class {{OBJECT}} extends Entity {
+                    
+                    //const _idPolice = Array('type' => '', 'min' => 0, 'max' => 100000, 'step' => 2);
+                    
+                    const _tableName = '{{TABLE_NAME}}';
+                    //const _properties = Array('id', 'name');
+            
+                    //const _timestamps = true;
+                    //const _softdelete = true;
+            
+                    //const _connectionName = '';
+                }
+            EOL;
+    
+    private $routeBase = 
+            <<<EOL
+            <?php
 
-          $menu->open(); */
-        /* $menu = (new CliMenuBuilder)
-          ->setTitle('CLI Menu')
-          ->addItem('First Item', $default_behavior)
-          ->addItem('Second Item', $default_behavior)
-          ->addLineBreak('-')
-          ->addSubMenu('Options', function (CliMenuBuilder $b) {
-          $b->setTitle('CLI Menu > Options')
-          ->addItem('First option', function (CliMenu $menu) {
-          echo sprintf('Executing option: %s', $menu->getSelectedItem()->getText());
-          })
-          ->addLineBreak('-');
-          })
-          ->setWidth(70)
-          ->setBackgroundColour('yellow')
-          ->build();
-          $menu->open(); */
-        $callable = function (CliMenu $menu) {
-            echo "I'm just a boring selectable item";
-        };
+                use Routes\RouteCollection as RouteCollection;
 
-        $menu = (new CliMenuBuilder)
-                ->addItem('Normal Item', function(){})
-                ->addSubMenu('Super Sub Menu', function (CliMenuBuilder $b) {
-                    $b->setTitle('Behold the awesomeness')
-                    ->addItem('asdasdasdas', function(){});
-                })
-                ->build();
-        $menu->open();
+                RouteCollection::group('/{{PLUGIN_NAME}}', function(){
+                    RouteCollection::get   ('/',            '{{CONTROLLER}}@index');
+                    RouteCollection::get   ('/',            '{{CONTROLLER}}@create');
+                    RouteCollection::post  ('/',            '{{CONTROLLER}}@store');
+                    RouteCollection::get   ('/[i:id]',      '{{CONTROLLER}}@show');
+                    RouteCollection::get   ('/[i:id]/edit', '{{CONTROLLER}}@edit');
+                    RouteCollection::put   ('/[i:id]/edit', '{{CONTROLLER}}@update');
+                    RouteCollection::delete('/[i:id]',      '{{CONTROLLER}}@destroy');
+                });
+            EOL;
+    
+    function createPluginFromTemplate($subname, $name){
+        if (is_dir( DIR_MODULE.$subname . '/' . $name )){
+            echo 'Treco ja existe';
+            return;
+        }else{
+            mkdir(DIR_MODULE.$subname . '/' . $name,              0755, true);
+            mkdir(DIR_MODULE.$subname . '/' . $name . '/lang',    0755, true);
+            mkdir(DIR_MODULE.$subname . '/' . $name . '/classes', 0755, true);
+            mkdir(DIR_MODULE.$subname . '/' . $name . '/db',      0755, true);
+            mkdir(DIR_MODULE.$subname . '/' . $name . '/views',   0755, true);            
+        }
+        
+        $plugin_name = $name;
+        $plugin_name_pretty = ucwords(str_replace('_', ' ', $name));
+        $object      = ucfirst( $name );
+        $controller  = $object.'Controller';
+        
+        $this->langBase = str_replace("{{PLUGIN_NAME}}",        $plugin_name,        $this->langBase);
+        $this->langBase = str_replace("{{PLUGIN_NAME_PRETTY}}", $plugin_name_pretty, $this->langBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . '/lang/en.php',           $this->langBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . '/lang/'.APP_LANG.'.php', $this->langBase);
+        
+        $this->migrationBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->migrationBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . '/db/Migrate.php', $this->migrationBase);
+        
+        $this->routeBase = str_replace("{{PLUGIN_NAME}}", $plugin_name, $this->routeBase);
+        $this->routeBase = str_replace("{{CONTROLLER}}",  $controller,  $this->routeBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . '/Routes.php', $this->routeBase);
+       
+        $this->objectBase = str_replace("{{OBJECT}}",      $object,  $this->objectBase);
+        $this->objectBase = str_replace("{{TABLE_NAME}}",  $name,    $this->objectBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . "/classes/$object.php", $this->objectBase);
+        
+        $this->controllerBase = str_replace("{{OBJECT}}",      $object,     $this->controllerBase);
+        $this->controllerBase = str_replace("{{CONTROLLER}}",  $controller, $this->controllerBase);
+        file_put_contents(DIR_MODULE.$subname . '/' . $name . "/$controller.php", $this->controllerBase);
+       
+        
     }
-
+    
+    function listRoutes(){
+        foreach(RouteCollection::returnRoutes() as $route){
+            //var_dump($route->_callback);continue;//die();
+            echo implode($route->_verb , ', ') ." || $route->_uri || $route->_weight||";
+            if( gettype($route->_callback[0]) == 'object' ){
+                echo 'Clousure ||';
+            }else{
+                echo $route->_callback[0];
+            }
+            echo "\n";
+        }
+        //file_put_contents('/tmp/Route.php', $this->routeBase);
+        //echo $this->objectBase;
+        echo APP_LANG;
+    }
+    
 }
+

+ 16 - 17
application/module/core/generator/Routes.php

@@ -2,24 +2,23 @@
 
 use Routes\RouteCollection as RouteCollection;
 
+RouteCollection::group('/core', function(){
+    
+    include_once 'GeneratorController.php';
+    
+    RouteCollection::cli('/routes', function(){
+        last_class()->listRoutes();    
+    });
 
-RouteCollection::cli('/core/purge', function(){
-    echo "Purging Everything\n";
-});
-
-RouteCollection::cli('/core/purge/[h:module]', function($module){
-    echo "Purgin $module\n";
-});
-
-RouteCollection::cli('/core/routes', function(){
-    foreach(RouteCollection::returnRoutes() as $route){
-        echo str_pad( implode(',', $route->_verb), 10) . "| $route->_uri \n";
-    }
-});
-
-RouteCollection::cli('/core/generator', function(){
+    
+    RouteCollection::cli('/purge/[h:module]', function($module){
+        echo "Purgin $module\n";
+    });
+    
+    RouteCollection::cli('/purge', function(){
+        echo "Purging Everything\n";
+    });
 
-    include_once 'GeneratorController.php';
-    last_class()->menu();
+    RouteCollection::cli('/create/[h:subname]/[h:name]', 'GeneratorController@createPluginFromTemplate');
     
 });