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

ActionMailer? rewrite to add mailer views/models

Files:
1 modified

Legend:

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

    r199 r200  
    177177               TRAX_LIB_ROOT . "/templates/helper.php"; 
    178178        $this->view_template_file = 
    179                TRAX_LIB_ROOT . "/templates/view.".$this->view_file_extention; 
     179               TRAX_LIB_ROOT . "/templates/view.phtml"; 
    180180        $this->model_template_file = 
    181181               TRAX_LIB_ROOT . "/templates/model.php"; 
    182182        $this->scaffold_template_path = 
    183183               TRAX_LIB_ROOT . "/templates/scaffolds/generator_templates"; 
     184        $this->mailer_view_template_file = 
     185               TRAX_LIB_ROOT . "/templates/mailer_view.phtml"; 
     186        $this->mailer_model_template_file = 
     187               TRAX_LIB_ROOT . "/templates/mailer_model.php"; 
    184188 
    185189        if (substr(PHP_OS, 0, 3) == 'WIN') { 
     
    249253                //  $command_name has the name of the model 
    250254                //  $_SERVER['argv'][3] has the name of the controller 
     255                case "mailer": 
     256                    if(empty($command_name)) { 
     257                        $this->mailer_help(); 
     258                    } else { 
     259                        $views = array(); 
     260                        if(array_key_exists(3, $_SERVER['argv']) 
     261                           && ($_SERVER["argv"][3] != "")) { 
     262                            for($i=3;$i < count($_SERVER["argv"]);$i++) { 
     263                               $views[] = strtolower($_SERVER["argv"][$i]); 
     264                            } 
     265                        } 
     266                        $this->generate_mailer($command_name, $views); 
     267                    } 
     268                    break;                     
    251269                case "scaffold": 
    252270 
     
    327345     *  @uses $view_path          Must be set before call. 
    328346     */ 
    329     function generate_controller($name, $views="", $scaffolding = false) { 
     347    function generate_controller($name, $views = "", $scaffolding = false) { 
    330348 
    331349        # Set the View and Controller extra path info 
     
    384402            $this->create_view($views,$name); 
    385403        } 
     404    } 
     405 
     406    function generate_mailer($name, $views = "") { 
     407        if(stristr($name, "_")) { 
     408            $model_file = $this->model_path."/".strtolower($name).".php"; 
     409        } else { 
     410            $model_file = $this->model_path."/".Inflector::underscore($name).".php"; 
     411        } 
     412 
     413        $model_class = Inflector::camelize($name); 
     414 
     415        if(!file_exists($model_file)) { 
     416            if(file_exists($this->mailer_model_template_file)) { 
     417                $template = file_get_contents($this->mailer_model_template_file); 
     418                $template = str_replace('[class_name]',$model_class,$template); 
     419                # Add view methods 
     420                if (!empty($views)) { 
     421                    # There are some views, add a method for each 
     422                    if(is_array($views)) { 
     423                        # Multiple views in an array 
     424                        foreach($views as $view) { 
     425                            $method  = "\tfunction $view() {\n"; 
     426                            $method .= "\t\t\$this->subject    = '".$model_class."->".$view."';\n"; 
     427                            $method .= "\t\t\$this->recipients = '';\n"; 
     428                            $method .= "\t\t\$this->from       = '';\n"; 
     429                            $method .= "\t\t\$this->headers    = array();\n"; 
     430                            $method .= "\t\t\$this->body       = array();\n"; 
     431                            $method .= "\t}"; 
     432                            $class_methods[] = $method; 
     433                        } 
     434                        $class_methods = implode("\n\n",$class_methods); 
     435                    } else { 
     436                        $class_methods  = "\tfunction $views() {\n"; 
     437                        $class_methods .= "\t\t\$this->subject    = '".$model_class."->".$views."';\n"; 
     438                        $class_methods .= "\t\t\$this->recipients = '';\n"; 
     439                        $class_methods .= "\t\t\$this->from       = '';\n"; 
     440                        $class_methods .= "\t\t\$this->headers    = array();\n"; 
     441                        $class_methods .= "\t\t\$this->body       = array();\n"; 
     442                        $class_methods .= "\t}";                         
     443                    } 
     444                    $template = str_replace('[class_methods]', $class_methods, $template); 
     445                } else { 
     446                    # No view methods to add, so remove unneeded template 
     447                    $template = str_replace('[class_methods]', '', $template); 
     448                } 
     449                # Write the mailer model to disk 
     450                if(!file_put_contents($model_file, $template)) { 
     451                    echo "error creating mailer model file: $model_file\n"; 
     452                } else { 
     453                    echo "create $model_file\n"; 
     454                } 
     455            } else { 
     456                echo "error mailer model template file doesn't exist: $this->mailer_model_template_file\n"; 
     457            } 
     458        } else { 
     459            echo "exists $model_file\n"; 
     460        }         
     461         
     462        # Now create the view files 
     463        $name = Inflector::underscore($name); 
     464        $this->view_path .= "/$name";  
     465        $this->view_template_file = $this->mailer_view_template_file; 
     466        $this->controller_class = $model_class; 
     467 
     468        # Create the extra folders for View / Controller 
     469        if(file_exists($this->view_path)) { 
     470            echo "exists $this->view_path\n"; 
     471        } else{ 
     472            $this->exec("$this->mkdir_cmd $this->view_path"); 
     473            echo "create $this->view_path\n"; 
     474        } 
     475 
     476        # Create view files if any 
     477        if(is_array($views)) { 
     478            foreach($views as $view) { 
     479                $this->create_view($view, $name); 
     480            } 
     481        } elseif(!empty($views)) { 
     482            $this->create_view($views, $name); 
     483        } 
     484              
     485         
    386486    } 
    387487 
     
    663763                        //  Multiple views in an array 
    664764                        foreach($views as $view) { 
    665                             $classMethods[] = "\tfunction $view() {\n\t}"; 
     765                            $class_methods[] = "\tfunction $view() {\n\t}"; 
    666766                        } 
    667                         $classMethods = implode("\n\n",$classMethods); 
     767                        $class_methods = implode("\n\n",$class_methods); 
    668768                    } else { 
    669                         $classMethods = "\tfunction $views() {\n\t}\n\n"; 
     769                        $class_methods = "\tfunction $views() {\n\t}\n\n"; 
    670770                    } 
    671771                    $template = str_replace('[class_methods]', 
    672                                             $classMethods,$template); 
     772                                            $class_methods,$template); 
    673773                } else { 
    674774 
     
    837937        echo "\tThis will create an Account model:\n"; 
    838938        echo "\t\tModel:      app/models/account.php\n\n"; 
     939    } 
     940 
     941    /** 
     942     *  Output console help message for "generate mailer" 
     943     */ 
     944    function mailer_help() { 
     945        echo "Usage: php script/generate.php mailer MailerName [view1 view2 ...]\n\n"; 
     946        echo "Description:\n"; 
     947        echo "\tThe mailer generator creates class methods for a new mailer and its views.\n\n"; 
     948        echo "\tThe generator takes a mailer name and a list of views as arguments.\n"; 
     949        echo "\tThe mailer name may be given in CamelCase or under_score.\n\n"; 
     950        echo "\tThe generator creates a mailer class in app/models with view templates\n"; 
     951        echo "\tin app/views/mailer_name.\n\n"; 
     952        echo "Example:\n"; 
     953        echo "\tphp script/generate.php mailer Notifications signup forgot_password invoice\n\n"; 
     954        echo "\tThis will create a Notifications mailer class:\n"; 
     955        echo "\t\tMailer:     app/models/notifications.php\n"; 
     956        echo "\t\tViews:      app/views/notifications/signup.phtml [...]\n\n";   
    839957    } 
    840958     
     
    875993        echo "Usage:\n"; 
    876994        echo "Generate Controller:\n"; 
    877         echo "php script/generate.php controller controller_name [view1 view2 ..]\n"; 
     995        echo "php script/generate.php controller controller_name [view1 view2 ...]\n"; 
    878996        echo "for more controller info php script/generate.php controller\n\n"; 
    879997        echo "Generate Model:\n"; 
    880998        echo "php script/generate.php model ModelName\n"; 
    881999        echo "for more model info php script/generate.php model\n\n"; 
     1000        echo "Generate Mailer:\n"; 
     1001        echo "php script/generate.php mailer MailerName [view1 view2 ...]\n"; 
     1002        echo "for more mailer info php script/generate.php mailer\n\n"; 
    8821003        echo "Generate Scaffold:\n"; 
    8831004        echo "php script/generate.php scaffold ModelName [controller_name] [view1 view2 ...]\n";