Show
Ignore:
Timestamp:
01/12/06 05:24:23 (6 years ago)
Author:
john
Message:

added scaffolding generator

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/trax_generator.php

    r62 r113  
    3535        $helper_template_file, 
    3636        $view_template_file, 
    37         $model_template_file; 
     37        $model_template_file, 
     38        $scaffold_template_path; 
    3839    public 
    39         $view_file_extention = "phtml"; 
     40        $view_file_extention = TRAX_VIEWS_EXTENTION; 
    4041 
    4142    function __construct() { 
     
    4445        $this->helper_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['helpers']; 
    4546        $this->model_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['models']; 
     47        $this->layouts_path = TRAX_ROOT . $GLOBALS['TRAX_INCLUDES']['layouts']; 
    4648        $this->controller_template_file = TRAX_LIB_ROOT . "/templates/controller.php"; 
    4749        $this->helper_template_file = TRAX_LIB_ROOT . "/templates/helper.php"; 
    4850        $this->view_template_file = TRAX_LIB_ROOT . "/templates/view.".$this->view_file_extention; 
    4951        $this->model_template_file = TRAX_LIB_ROOT . "/templates/model.php"; 
     52        $this->scaffold_template_path = TRAX_LIB_ROOT . "/templates/scaffolds/generator_templates"; 
    5053 
    5154        if (substr(PHP_OS, 0, 3) == 'WIN') { 
     
    8487                    } 
    8588                    break; 
     89                case "scaffold": 
     90                    if(empty($command_name)) { 
     91                        $this->scaffold_help(); 
     92                    } else { 
     93                        $controller_name = $_SERVER["argv"][3]; 
     94                        if($_SERVER["argv"][4] != "") { 
     95                            for($i=4;$i < count($_SERVER["argv"]);$i++) { 
     96                                $views[] = strtolower($_SERVER["argv"][$i]); 
     97                            } 
     98                        }                         
     99                        $this->generate_scaffold($command_name,$controller_name,$views); 
     100                    } 
     101                    break;                     
    86102            } 
    87103        } 
     
    89105    } 
    90106 
    91     function generate_controller($name, $views="") { 
     107    function generate_controller($name, $views="", $scaffolding = false) { 
    92108 
    93109        # Set the View and Controller extra path info 
    94110        if(stristr($name, "/")) { 
    95             $extraPath = substr($name,0,strrpos($name, "/")); 
     111            $this->extra_path = substr($name,0,strrpos($name, "/")); 
    96112            $name = Inflector::underscore(substr($name,strrpos($name, "/")+1)); 
    97             $this->view_path .= "/$extraPath/$name"; 
    98             $this->controller_path .= "/$extraPath"; 
    99             $this->helper_path .= "/$extraPath"; 
     113            $this->view_path .= "/$this->extra_path/$name"; 
     114            $this->layouts_path .= "/$this->extra_path/$name"; 
     115            $this->controller_path .= "/$this->extra_path"; 
     116            $this->helper_path .= "/$this->extra_path"; 
    100117        } else { 
    101118            $name = Inflector::underscore($name); 
     
    119136 
    120137        # Create the actual controller/helper files 
    121         $this->create_controller($name,$views); 
     138        if(!$scaffolding) { 
     139            $this->create_controller($name, $views); 
     140        }  
    122141        $this->create_helper($name); 
    123142 
    124         if($extraPath) { 
    125             $name = $extraPath."/".$name; 
     143        if($this->extra_path) { 
     144            $name = $this->extra_path."/".$name; 
    126145        } 
    127146 
     
    154173                } else { 
    155174                    echo "created $model_file\n"; 
     175                    return true; 
    156176                } 
    157177            } else { 
     
    160180        } else { 
    161181            echo "exists $model_file\n"; 
    162         } 
    163     } 
     182            return true; 
     183        } 
     184        return false; 
     185    } 
     186     
     187    function generate_scaffold($model_name, $controller_name, $views="") { 
     188        if(!$model_exists = $this->generate_model($model_name)) { 
     189            echo "Error - Can't create Model: $model_name.\n";     
     190            exit; 
     191        } 
     192 
     193        $GLOBALS['current_controller_object'] =& $this; 
     194        $model_class_name = Inflector::classify($model_name); 
     195        $singluar_model_name = Inflector::singularize($model_name); 
     196        $plural_model_name = Inflector::pluralize($model_name);   
     197        $human_model_name = Inflector::humanize($model_name);       
     198        $this->{$singluar_model_name} = new $model_class_name();             
     199        if(!$controller_name) { 
     200            $controller_name = Inflector::pluralize($model_name);    
     201        } else { 
     202            $controller_name = Inflector::underscore($controller_name);     
     203        } 
     204        $controller_file = "$this->controller_path/".$controller_name."_controller.php"; 
     205        $non_scaffolded_actions = array(); 
     206        $illegal_views = array("index","add","edit","show");       
     207        if(is_array($views)) { 
     208            foreach($views as $view) { 
     209                if(!in_array($view, $illegal_views)) { 
     210                    $non_scaffolded_actions[] = $view;   
     211                }            
     212            } 
     213        }          
     214        $this->generate_controller($controller_name, $non_scaffolded_actions, true);  
     215        if(stristr($controller_name, "/")) { 
     216            $controller_class_name = Inflector::classify(substr($controller_name,strrpos($controller_name, "/")+1)); 
     217            $human_controller_name = Inflector::humanize(substr($controller_name,strrpos($controller_name, "/")+1)); 
     218        } else { 
     219            $controller_class_name = Inflector::classify($controller_name); 
     220            $human_controller_name = Inflector::humanize($controller_name); 
     221        }              
     222      
     223        # Generate the controller 
     224        ob_start();     
     225        include("$this->scaffold_template_path/controller.php"); 
     226        $controller_contents = $this->fix_php_brackets(ob_get_contents()); 
     227        ob_end_clean(); 
     228        if(!file_exists($controller_file)) { 
     229            if(!file_put_contents($controller_file, $controller_contents)) { 
     230                echo "error creating controller class file: $controller_file\n"; 
     231            } else { 
     232                echo "created $controller_file\n"; 
     233            }         
     234        } else { 
     235            echo "exists $controller_file\n";         
     236        }  
     237                 
     238        # Generate the index.phtml view 
     239        $view_file = "$this->view_path/index.".$this->view_file_extention; 
     240        ob_start();     
     241        include("$this->scaffold_template_path/view_index.phtml"); 
     242        $index_contents = $this->fix_php_brackets(ob_get_contents()); 
     243        ob_end_clean(); 
     244        if(!file_exists($view_file)) { 
     245            if(!file_put_contents($view_file, $index_contents)) { 
     246                echo "error creating view file: $view_file\n"; 
     247            } else { 
     248                echo "created $view_file\n"; 
     249            } 
     250        } else { 
     251            echo "exists $view_file\n";         
     252        }  
     253                
     254        # Generate the add.phtml view 
     255        $view_file = "$this->view_path/add.".$this->view_file_extention; 
     256        ob_start();     
     257        include("$this->scaffold_template_path/view_add.phtml"); 
     258        $add_contents = $this->fix_php_brackets(ob_get_contents()); 
     259        ob_end_clean();        
     260        if(!file_exists($view_file)) { 
     261            if(!file_put_contents($view_file, $index_contents)) { 
     262                echo "error creating view file: $view_file\n"; 
     263            } else { 
     264                echo "created $view_file\n"; 
     265            } 
     266        } else { 
     267            echo "exists $view_file\n";         
     268        }  
     269         
     270        # Generate the edit.phtml view 
     271        $view_file = "$this->view_path/edit.".$this->view_file_extention; 
     272        ob_start();     
     273        include("$this->scaffold_template_path/view_edit.phtml"); 
     274        $edit_contents = $this->fix_php_brackets(ob_get_contents()); 
     275        ob_end_clean();  
     276        if(!file_exists($view_file)) { 
     277            if(!file_put_contents($view_file, $index_contents)) { 
     278                echo "error creating view file: $view_file\n"; 
     279            } else { 
     280                echo "created $view_file\n"; 
     281            } 
     282        } else { 
     283            echo "exists $view_file\n";         
     284        }  
     285         
     286        # Generate the show.phtml view 
     287        $view_file = "$this->view_path/show.".$this->view_file_extention; 
     288        ob_start();     
     289        include("$this->scaffold_template_path/view_show.phtml"); 
     290        $show_contents = $this->fix_php_brackets(ob_get_contents()); 
     291        ob_end_clean(); 
     292        if(!file_exists($view_file)) { 
     293            if(!file_put_contents($view_file, $index_contents)) { 
     294                echo "error creating view file: $view_file\n"; 
     295            } else { 
     296                echo "created $view_file\n"; 
     297            } 
     298        } else { 
     299            echo "exists $view_file\n";         
     300        }  
     301                
     302        # Generate the partial containing the form elments from the database 
     303        $view_file = "$this->view_path/_form.".$this->view_file_extention; 
     304        ob_start();     
     305        include("$this->scaffold_template_path/form_scaffolding.phtml"); 
     306        $_form_contents = $this->fix_php_brackets(ob_get_contents()); 
     307        ob_end_clean();   
     308        if(!file_exists($view_file)) { 
     309            if(!file_put_contents($view_file, $index_contents)) { 
     310                echo "error creating view file: $view_file\n"; 
     311            } else { 
     312                echo "created $view_file\n"; 
     313            } 
     314        } else { 
     315            echo "exists $view_file\n";         
     316        }  
     317         
     318        # Generate the layout for the scaffolding 
     319        $layout_file = "$this->layouts_path/$controller_name.".$this->view_file_extention; 
     320        ob_start();     
     321        include("$this->scaffold_template_path/layout.phtml"); 
     322        $layout_contents = $this->fix_php_brackets(ob_get_contents()); 
     323        ob_end_clean();   
     324        if(!file_exists($layout_file)) { 
     325            if(!file_put_contents($layout_file, $index_contents)) { 
     326                echo "error creating layout file: $view_file\n"; 
     327            } else { 
     328                echo "created $view_file\n"; 
     329            } 
     330        } else { 
     331            echo "exists $layout_file\n";         
     332        }                    
     333    }     
    164334 
    165335    function create_controller($controller,$views="") { 
     
    215385    } 
    216386 
    217     function create_view($view,$controller) { 
     387    function create_view($view, $controller) { 
    218388        $view_file = "$this->view_path/".$view.".".$this->view_file_extention; 
    219389        if(!file_exists($view_file)) { 
     
    242412            exec($cmd); 
    243413        } 
     414    } 
     415     
     416    function fix_php_brackets($string) { 
     417        return str_replace("? >", "?>", str_replace("< ?", "<?", $string));             
    244418    } 
    245419 
     
    258432        echo "\tCredit card controller with URLs like /credit_card/debit.\n"; 
    259433        echo "\t\tController: app/controllers/credit_card_controller.php\n"; 
    260         echo "\t\tViews:      app/views/credit_card/debit.phtml [...]\n\n"; 
    261         echo "Folders Example:\n"; 
     434        echo "\t\tViews:      app/views/credit_card/debit.phtml [...]\n"; 
     435        echo "\t\tHelper:     app/helpers/credit_card_helper.php\n\n"; 
     436        echo "Module/Folders Example:\n"; 
    262437        echo "\t./script/generate.php controller 'admin/credit_card' suspend late_fee\n\n"; 
    263438        echo "\tCredit card admin controller with URLs /admin/credit_card/suspend.\n"; 
    264439        echo "\t\tController: app/controllers/admin/credit_card_controller.php\n"; 
    265         echo "\t\tViews:      app/views/admin/credit_card/suspend.phtml [...]\n\n"; 
     440        echo "\t\tViews:      app/views/admin/credit_card/suspend.phtml [...]\n"; 
     441        echo "\t\tHelper:     app/helpers/credit_card_helper.php\n\n"; 
    266442    } 
    267443 
     
    278454        echo "\t\tModel:      app/models/account.php\n\n"; 
    279455    } 
     456     
     457    function scaffold_help() { 
     458        echo "Usage: ./generate scaffold ModelName [ControllerName] [view1 view2 ...]\n\n"; 
     459        echo "Description:\n"; 
     460        echo "\tThe scaffold generator creates a controller to interact with a model.\n"; 
     461        echo "\tIf the model does not exist, it creates the model as well.  The generated\n"; 
     462        echo "\tcode is equivalent to the ( public \$scaffold = \"model\"; ) declaration,\n"; 
     463        echo "\tmaking it easy to migrate when you wish to customize your controller and views.\n\n"; 
     464        echo "\tThe generator takes a model name, an optional controller name, and a\n"; 
     465        echo "\tlist of views as arguments.  Scaffolded actions and views are created\n"; 
     466        echo "\tautomatically.\n\n"; 
     467        echo "\tThe auto scaffolded actions and views are:\n"; 
     468        echo "\t\tindex, show, add, edit, delete\n\n"; 
     469        echo "\tIf a controller name is not given, the plural form of the model name\n"; 
     470        echo "\twill be used.  The model and controller names may be given in CamelCase\n"; 
     471        echo "\tor under_score and should not be suffixed with 'Model' or 'Controller'.\n\n"; 
     472        echo "Example:\n"; 
     473        echo "\t./generate scaffold Account Bank debit credit\n\n"; 
     474        echo "\tThis will generate an Account model and BankController with a basic user interface\n"; 
     475        echo "\tNow create the accounts table in your database and browse to http://localhost/bank/\n"; 
     476        echo "\tvoila, you're on Trax!\n\n"; 
     477        echo "Module/Folders Example:\n"; 
     478        echo "\t./generate scaffold CreditCard 'admin/credit_card' suspend late_fee\n\n"; 
     479        echo "\tThis will generate a CreditCard model and CreditCardController controller\n"; 
     480        echo "\tin the admin module.\n";             
     481    } 
    280482 
    281483    function generator_help() {