[ Index ]
 

Code source de PHPonTrax 2.6.6-svn

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/vendor/trax/ -> trax_generator.php (source)

   1  <?php
   2  /**
   3   *  File containing the TraxGenerator class
   4   *
   5   *  (PHP 5)
   6   *
   7   *  @package PHPonTrax
   8   *  @version $Id: trax_generator.php 260 2006-09-02 07:19:54Z john $
   9   *  @copyright (c) 2005 John Peterson
  10   *
  11   *  Permission is hereby granted, free of charge, to any person obtaining
  12   *  a copy of this software and associated documentation files (the
  13   *  "Software"), to deal in the Software without restriction, including
  14   *  without limitation the rights to use, copy, modify, merge, publish,
  15   *  distribute, sublicense, and/or sell copies of the Software, and to
  16   *  permit persons to whom the Software is furnished to do so, subject to
  17   *  the following conditions:
  18   *
  19   *  The above copyright notice and this permission notice shall be
  20   *  included in all copies or substantial portions of the Software.
  21   *
  22   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23   *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24   *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25   *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26   *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27   *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28   *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29   *
  30   *  @package PHPonTrax
  31   */
  32  
  33  /**
  34   *  Generate application files in the Trax work area
  35   *
  36   *  Implements the commands of {@link generate.php script/generate.php}
  37   *  <p>Legal commands:</p>
  38   *  <ul>
  39   *    <li>{@link generate_controller() controller}</li>
  40   *    <li>{@link generate_model() model}</li>
  41   *    <li>{@link generate_scaffold() scaffold}</li>
  42   *  </ul>
  43   */
  44  class TraxGenerator {
  45  
  46      /**
  47       *  Filesystem path to the app/views directory in the Trax work area
  48       *  @var string
  49       */
  50      private $view_path;
  51  
  52      /**
  53       *  Filesystem path to the app/controllers directory in the Trax work area
  54       *  @var string
  55       */
  56      private $controller_path;
  57  
  58      /**
  59       *  Filesystem path to the app/helpers directory in the Trax work area
  60       *  @var string
  61       */
  62      private $helper_path;
  63  
  64      /**
  65       *  Filesystem path to the app/model directory in the Trax work area
  66       *  @var string
  67       */
  68      private $model_path;
  69  
  70      /**
  71       *  Generated subdirectories in the Trax work area
  72       *
  73       *  When a controller is generated with a name that includes '/',
  74       *  $extra_path is set to the implied subdirectories.
  75       *  @var string
  76       */
  77      private $extra_path;
  78  
  79      /**
  80       *  Platform-dependent command to make a directory
  81       *  @var string
  82       */
  83      private $mkdir_cmd;
  84  
  85      /**
  86       *  Filesystem path to the templates/controller.php file
  87       *  @var string
  88       */
  89      private $controller_template_file;
  90  
  91      /**
  92       *  Filesystem path to the templates/helper.php file
  93       *  @var string
  94       */
  95      private $helper_template_file;
  96  
  97      /**
  98       *  Filesystem path to the templates/view.phtml file
  99       *  @var string
 100       */
 101      private $view_template_file;
 102  
 103      /**
 104       *  Filesystem path to the templates/model.php file
 105       *  @var string
 106       */
 107      private $model_template_file;
 108  
 109      /**
 110       *  Filesystem path to templates/scaffolds/generator_templates directory
 111       *  @var string
 112       */
 113      private $scaffold_template_path;
 114  
 115      /**
 116       *  Filesystem path to the app/views/layouts/ directory in the
 117       *  Trax work area
 118       *  @var string
 119       */
 120      private $layouts_path;
 121  
 122      /**
 123       *  @todo Document this variable
 124       *
 125       *  Value is set by {@link generate_controller()} and used by
 126       *  {@link generate_scaffold()}
 127       *  @var string
 128       */
 129      private $layout_filename;
 130  
 131      /**
 132       *  CamelCase name of the controller class
 133       *  @var string
 134       */
 135      private $controller_class;
 136  
 137      /**
 138       *  Constructor for the TraxGenerator object
 139       *
 140       *  Compute and store filesystem paths to the various
 141       *  subdirectories of the Trax work area and the template files
 142       *  used to generate application files
 143       *
 144       *  @uses controller_path
 145       *  @uses controller_template_file
 146       *  @uses helper_path
 147       *  @uses helper_template_file
 148       *  @uses layouts_path
 149       *  @uses model_path
 150       *  @uses model_template_file
 151       *  @uses scaffold_template_path
 152       *  @uses view_path
 153       *  @uses view_template_file
 154       */
 155      function __construct() {
 156          $this->view_path = Trax::$views_path;
 157          $this->controller_path = Trax::$controllers_path;
 158          $this->helper_path = Trax::$helpers_path;
 159          $this->model_path = Trax::$models_path;
 160          $this->layouts_path = Trax::$layouts_path;
 161          $this->controller_template_file =
 162                 TRAX_LIB_ROOT . "/templates/controller.php";
 163          $this->helper_template_file =
 164                 TRAX_LIB_ROOT . "/templates/helper.php";
 165          $this->view_template_file =
 166                 TRAX_LIB_ROOT . "/templates/view.phtml";
 167          $this->model_template_file =
 168                 TRAX_LIB_ROOT . "/templates/model.php";
 169          $this->scaffold_template_path =
 170                 TRAX_LIB_ROOT . "/templates/scaffolds/generator_templates";
 171          $this->mailer_view_template_file =
 172                 TRAX_LIB_ROOT . "/templates/mailer_view.phtml";
 173          $this->mailer_model_template_file =
 174                 TRAX_LIB_ROOT . "/templates/mailer_model.php";
 175  
 176          if (substr(PHP_OS, 0, 3) == 'WIN') {
 177              $this->mkdir_cmd = "mkdir";
 178          } else {
 179              $this->mkdir_cmd = "mkdir -p";
 180          }
 181      }
 182  
 183      /**
 184       *  Parse command line and carry out the command
 185       *
 186       *  Command line arguments, if any are in $_SERVER['argv']
 187       *  @uses controller_help()
 188       *  @uses generate_controller()
 189       *  @uses generate_model()
 190       *  @uses generate_scaffold()
 191       *  @uses generator_help()
 192       *  @uses model_help()
 193       *  @uses scaffold_help()
 194       */
 195      function run() {
 196  
 197          //  If command line arguments exist, parse them
 198          if (array_key_exists('argv', $_SERVER)) {
 199              if (array_key_exists(1, $_SERVER['argv'])) {
 200                  $command = strtolower($_SERVER["argv"][1]);
 201              }
 202              if (array_key_exists(2, $_SERVER['argv'])) {
 203                  $command_name = $_SERVER["argv"][2];
 204              }
 205          }
 206  
 207          //  Execute command or output a diagnostic
 208          if(empty($command)) {
 209              $this->generator_help();
 210          } else {
 211              switch($command) {
 212  
 213                  //  Process "controller" command
 214                  case "controller":
 215                      if(empty($command_name)) {
 216                          $this->controller_help();
 217                      } else {
 218                          $views = array();
 219                          if(array_key_exists(3, $_SERVER['argv'])
 220                             && ($_SERVER["argv"][3] != "")) {
 221                              for($i=3;$i < count($_SERVER["argv"]);$i++) {
 222                                 $views[] = strtolower($_SERVER["argv"][$i]);
 223                              }
 224                          }
 225                          $this->generate_controller($command_name, $views);
 226                      }
 227                      break;
 228  
 229                  //  Process "model" command
 230                  //  $command_name is the name of the model
 231                  case "model":
 232                      if(empty($command_name)) {
 233                          $this->model_help();
 234                      } else {
 235                          $this->generate_model($command_name);
 236                      }
 237                      break;
 238  
 239                  //  Process "scaffold" command
 240                  //  $command_name has the name of the model
 241                  //  $_SERVER['argv'][3] has the name of the controller
 242                  case "mailer":
 243                      if(empty($command_name)) {
 244                          $this->mailer_help();
 245                      } else {
 246                          $views = array();
 247                          if(array_key_exists(3, $_SERVER['argv'])
 248                             && ($_SERVER["argv"][3] != "")) {
 249                              for($i=3;$i < count($_SERVER["argv"]);$i++) {
 250                                 $views[] = strtolower($_SERVER["argv"][$i]);
 251                              }
 252                          }
 253                          $this->generate_mailer($command_name, $views);
 254                      }
 255                      break;                    
 256                  case "scaffold":
 257  
 258                      //  Model name is required
 259                      if( empty($command_name) ) {
 260                          echo "Error: name of model omitted\n";
 261                          $this->scaffold_help();
 262                          break;
 263                      }
 264  
 265                      //  Controller name is optional
 266                      if (array_key_exists(3, $_SERVER["argv"])) {
 267                          $controller_name = $_SERVER["argv"][3];
 268                      } else {
 269                          $controller_name = null;
 270                      }
 271                          
 272                      //  Views are optional following controller name
 273                      $views = array();
 274                      if (array_key_exists(4, $_SERVER["argv"])
 275                          && ($_SERVER["argv"][4] != "")) {
 276                          for($i=4;$i < count($_SERVER["argv"]);$i++) {
 277                              $views[] = strtolower($_SERVER["argv"][$i]);
 278                          }
 279                      }
 280                      $this->generate_scaffold($command_name,
 281                                               $controller_name, $views);
 282                      break;                    
 283  
 284              default:
 285                  $this->generator_help();
 286              }                            // switch($command)
 287          }
 288          return;
 289      }
 290  
 291      /**
 292       *  Implement "generate controller" command
 293       *
 294       *  <p>Example:<br><samp>php script/generate.php controller</samp>
 295       *  <i>SomeName</i><br>
 296       *  will generate:</p>
 297       *  <ul>
 298       *    <li>a file
 299       *  <samp>app/controllers/</samp><i>some_name</i><samp>_controller.php</samp><br>
 300       *  containing the class definition<br>
 301       *  <samp>class</samp> <i>SomeName</i><samp>Controller extends
 302       *  ApplicationController {}</samp></li>
 303       *     <li>a file
 304       *  <samp>app/helpers/</samp><i>some_name</i><samp>_helper.php</samp></li>
 305       *     <li>a directory
 306       *  <samp>app/views/</samp><i>some_name</i></li>
 307       *  </ul>
 308       *
 309       *  <p>Optionally, one or more views can be appended to the command:<br>
 310       *  <samp>php script/generate.php controller</samp>
 311       *  <i>SomeName view1 view2</i><br>
 312       *  which will additionally generate files:<br>
 313       *  <samp>app/views/</samp><i>some_name/view1</i><samp>.phtml</samp><br>
 314       *  <samp>app/views/</samp><i>some_name/view2</i><samp>.phtml</samp></p>
 315       *
 316       *  @param string $name Name in CamelCase of the controller to generate.
 317       *                      The value may include '/' which will cause
 318       *                      creation of subdirectories indicated to
 319       *                      hold the controller and view files.
 320       *  @param string $views  Optional list of views to generate
 321       *  @param boolean $scaffolding
 322       *  @uses Inflector::underscore()
 323       *  @uses $controller_class   Set during call
 324       *  @uses $controller_path    Must be set before call.
 325       *  @uses create_controller()
 326       *  @uses create_helper()
 327       *  @uses create_view()
 328       *  @uses $extra_path         Set during call
 329       *  @uses $helper_path        Must be set before call.
 330       *  @uses $layouts_path       Must be set before call.
 331       *  @uses $layout_filename    Set during call
 332       *  @uses $view_path          Must be set before call.
 333       */
 334      function generate_controller($name, $views = "", $scaffolding = false) {
 335  
 336          # Set the View and Controller extra path info
 337          if(stristr($name, "/")) {
 338              $this->extra_path = substr($name,0,strrpos($name, "/"));
 339              $name = Inflector::underscore(substr($name,strrpos($name, "/")+1));
 340              $this->view_path .= "/$this->extra_path/$name";           
 341              $this->layouts_path .= "/$this->extra_path";
 342              $this->controller_path .= "/$this->extra_path";
 343              $this->helper_path .= "/$this->extra_path";
 344          } else {
 345              $name = Inflector::underscore($name);
 346              $this->view_path .= "/$name";
 347          }
 348          $this->layout_filename = $name;
 349          $this->controller_class = Inflector::camelize($name);
 350  
 351          # Create the extra folders for View / Controller
 352          if(file_exists($this->view_path)) {
 353              echo "exists $this->view_path\n";
 354          } else{
 355              $this->exec("$this->mkdir_cmd $this->view_path");
 356              echo "create $this->view_path\n";
 357          }
 358  
 359          if(file_exists($this->controller_path)) {
 360              echo "exists $this->controller_path\n";
 361          } else {
 362              $this->exec("$this->mkdir_cmd $this->controller_path");
 363              echo "create $this->controller_path\n";
 364          }
 365  
 366          if(file_exists($this->helper_path)) {
 367              echo "exists $this->helper_path\n";
 368          } else {
 369              $this->exec("$this->mkdir_cmd $this->helper_path");
 370              echo "create $this->helper_path\n";
 371          }
 372  
 373          # Create the actual controller/helper files
 374          if(!$scaffolding) {
 375              $this->create_controller($name, $views);
 376          } 
 377          $this->create_helper($name);
 378  
 379          if($this->extra_path) {
 380              $name = $this->extra_path."/".$name;
 381          }
 382  
 383          # Create view files if any
 384          if(is_array($views)) {
 385              foreach($views as $view) {
 386                  $this->create_view($view,$name);
 387              }
 388          } elseif(!empty($views)) {
 389              $this->create_view($views,$name);
 390          }
 391      }
 392  
 393      function generate_mailer($name, $views = "") {
 394          if(stristr($name, "_")) {
 395              $model_file = $this->model_path."/".strtolower($name).".php";
 396          } else {
 397              $model_file = $this->model_path."/".Inflector::underscore($name).".php";
 398          }
 399  
 400          $model_class = Inflector::camelize($name);
 401  
 402          if(!file_exists($model_file)) {
 403              if(file_exists($this->mailer_model_template_file)) {
 404                  $template = file_get_contents($this->mailer_model_template_file);
 405                  $template = str_replace('[class_name]',$model_class,$template);
 406                  # Add view methods
 407                  if (!empty($views)) {
 408                      # There are some views, add a method for each
 409                      if(is_array($views)) {
 410                          # Multiple views in an array
 411                          foreach($views as $view) {
 412                              $method  = "\tfunction $view() {\n";
 413                              $method .= "\t\t\$this->subject    = '".$model_class."->".$view."';\n";
 414                              $method .= "\t\t\$this->recipients = '';\n";
 415                              $method .= "\t\t\$this->from       = '';\n";
 416                              $method .= "\t\t\$this->headers    = array();\n";
 417                              $method .= "\t\t\$this->body       = array();\n";
 418                              $method .= "\t}";
 419                              $class_methods[] = $method;
 420                          }
 421                          $class_methods = implode("\n\n",$class_methods);
 422                      } else {
 423                          $class_methods  = "\tfunction $views() {\n";
 424                          $class_methods .= "\t\t\$this->subject    = '".$model_class."->".$views."';\n";
 425                          $class_methods .= "\t\t\$this->recipients = '';\n";
 426                          $class_methods .= "\t\t\$this->from       = '';\n";
 427                          $class_methods .= "\t\t\$this->headers    = array();\n";
 428                          $class_methods .= "\t\t\$this->body       = array();\n";
 429                          $class_methods .= "\t}";                        
 430                      }
 431                      $template = str_replace('[class_methods]', $class_methods, $template);
 432                  } else {
 433                      # No view methods to add, so remove unneeded template
 434                      $template = str_replace('[class_methods]', '', $template);
 435                  }
 436                  # Write the mailer model to disk
 437                  if(!file_put_contents($model_file, $template)) {
 438                      echo "error creating mailer model file: $model_file\n";
 439                  } else {
 440                      echo "create $model_file\n";
 441                  }
 442              } else {
 443                  echo "error mailer model template file doesn't exist: $this->mailer_model_template_file\n";
 444              }
 445          } else {
 446              echo "exists $model_file\n";
 447          }        
 448          
 449          # Now create the view files
 450          $name = Inflector::underscore($name);
 451          $this->view_path .= "/$name"; 
 452          $this->view_template_file = $this->mailer_view_template_file;
 453          $this->controller_class = $model_class;
 454  
 455          # Create the extra folders for View / Controller
 456          if(file_exists($this->view_path)) {
 457              echo "exists $this->view_path\n";
 458          } else{
 459              $this->exec("$this->mkdir_cmd $this->view_path");
 460              echo "create $this->view_path\n";
 461          }
 462  
 463          # Create view files if any
 464          if(is_array($views)) {
 465              foreach($views as $view) {
 466                  $this->create_view($view, $name);
 467              }
 468          } elseif(!empty($views)) {
 469              $this->create_view($views, $name);
 470          }
 471               
 472          
 473      }
 474  
 475      /**
 476       *  Implement the "generate model" command
 477       *
 478       *  <p>Example:<br><samp>php script/generate.php model</samp>
 479       *  <i>SomeName</i><br>
 480       *  will generate a file
 481       *  <samp>app/models/</samp><i>some_name</i><samp>.php</samp><br>
 482       *  containing the class definition<br>
 483       *  <samp>class</samp> <i>SomeName</i> <samp>extends
 484       *  ActiveRecord {}</samp>
 485       *  @param string $name Name of the model.  May be in either
 486       *                under_score or CamelCase.  If no '_' exists in
 487       *                $name it is treated as CamelCase.
 488       *  @uses Inflector::underscore()
 489       *  @uses model_path           Must be set before call.
 490       *                             Not changed during call.
 491       *  @uses model_template_file  Must be set before call.
 492       *                             Not changed during call.
 493       */
 494      function generate_model($name) {
 495  
 496          if(stristr($name, "_")) {
 497              $model_file = $this->model_path."/".strtolower($name).".php";
 498          } else {
 499              $model_file = $this->model_path."/".Inflector::underscore($name).".php";
 500          }
 501  
 502          $model_class = Inflector::camelize($name);
 503  
 504          if(!file_exists($model_file)) {
 505              if(file_exists($this->model_template_file)) {
 506                  $template = file_get_contents($this->model_template_file);
 507                  $template = str_replace('[class_name]',$model_class,$template);
 508                  if(!file_put_contents($model_file,$template)) {
 509                      echo "error creating model file: $model_file\n";
 510                  } else {
 511                      echo "create $model_file\n";
 512                      return true;
 513                  }
 514              } else {
 515                  echo "error model template file doesn't exist: $this->model_template_file\n";
 516              }
 517          } else {
 518              echo "exists $model_file\n";
 519              return true;
 520          }
 521          return false;
 522      }
 523      
 524      /**
 525       *  Implement the "generate scaffold" command
 526       *
 527       *  @param string $model_name
 528       *  @param string $controller_name
 529       *  @param string $views
 530       *  @uses generate_controller()
 531       *  @uses generate_model()
 532       *  @uses Inflector::classify()
 533       *  @uses Inflector::humanize()
 534       *  @uses Inflector::pluralize()
 535       *  @uses Inflector::singularize()
 536       *  @uses Inflector::underscore()
 537       *  @uses $layout_filename          Set as output from
 538       *                                  generate_controller().
 539       *                                  Not changed afterward.
 540       *  @uses fix_php_brackets()
 541       */
 542      function generate_scaffold($model_name, $controller_name, $views="") {
 543          //echo 'generate_scaffold("'.$model_name.'", "'
 544          //          .$controller_name.'", "'.$views.'")'."\n";
 545          if(!$model_exists = $this->generate_model($model_name)) {
 546              echo "Error - Can't create Model: $model_name.\n";    
 547              return;
 548          }
 549  
 550          Trax::$current_controller_object =& $this;
 551          $model_class_name = Inflector::classify($model_name);
 552          $singular_model_name = Inflector::singularize($model_name);
 553          $plural_model_name = Inflector::pluralize($model_name);  
 554          $human_model_name = Inflector::humanize($model_name);      
 555  
 556          try {
 557              $this->{$singular_model_name} = new $model_class_name();
 558          } catch (ActiveRecordError $e) {
 559              echo "Can't create model.\n";
 560              echo $e->getMessage()."\n";
 561              echo "for database '"
 562                  . ActiveRecord::$database_settings[TRAX_ENV]['database']
 563                  . "' on host '"
 564                  . ActiveRecord::$database_settings[TRAX_ENV]['hostspec']
 565                  . "' as user '"
 566                  . ActiveRecord::$database_settings[TRAX_ENV]['username']
 567                  . "'\nDid you configure file "
 568                  . Trax::$config_path
 569                  . "/database.ini correctly?\n";
 570              die();
 571          }
 572          if(empty($controller_name)) {
 573              $controller_name = Inflector::underscore($model_name);   
 574          } else {
 575              $controller_name = Inflector::underscore($controller_name);    
 576          }
 577          Trax::$current_controller_name = $controller_name;
 578          $controller_file = "$this->controller_path/" . $controller_name."_controller.php";
 579          Trax::$current_controller_path = $controller_file;
 580          $non_scaffolded_actions = array();
 581          $illegal_views = array("index","add","edit","show");      
 582          if(is_array($views)) {
 583              foreach($views as $view) {
 584                  if(!in_array($view, $illegal_views)) {
 585                      $non_scaffolded_actions[] = $view;  
 586                  }           
 587              }
 588          }         
 589          $this->generate_controller($controller_name,
 590                                     $non_scaffolded_actions, true); 
 591          if(stristr($controller_name, "/")) {
 592              $controller_class_name =
 593                  Inflector::classify(substr($controller_name,
 594                                             strrpos($controller_name, "/")+1));
 595              $human_controller_name =
 596                  Inflector::humanize(substr($controller_name,
 597                                             strrpos($controller_name, "/")+1));
 598          } else {
 599              $controller_class_name = Inflector::classify($controller_name);
 600              $human_controller_name = Inflector::humanize($controller_name);
 601          }             
 602       
 603          # Generate the controller
 604          ob_start();    
 605          include("$this->scaffold_template_path/controller.php");
 606          $controller_contents = $this->fix_php_brackets(ob_get_contents());
 607          ob_end_clean();
 608          if(!file_exists($controller_file)) {
 609              if(!file_put_contents($controller_file, $controller_contents)) {
 610                  echo "error creating controller class file: $controller_file\n";
 611              } else {
 612                  echo "create $controller_file\n";
 613              }        
 614          } else {
 615              echo "exists $controller_file\n";        
 616          } 
 617                  
 618          # Generate the index.phtml view
 619          $view_file = "$this->view_path/index.".Trax::$views_extension;
 620          ob_start();    
 621          include("$this->scaffold_template_path/view_index.phtml");
 622          $index_contents = $this->fix_php_brackets(ob_get_contents());
 623          ob_end_clean();
 624          if(!file_exists($view_file)) {
 625              if(!file_put_contents($view_file, $index_contents)) {
 626                  echo "error creating view file: $view_file\n";
 627              } else {
 628                  echo "create $view_file\n";
 629              }
 630          } else {
 631              echo "exists $view_file\n";        
 632          } 
 633                 
 634          # Generate the add.phtml view
 635          $view_file = "$this->view_path/add.".Trax::$views_extension;
 636          ob_start();    
 637          include("$this->scaffold_template_path/view_add.phtml");
 638          $add_contents = $this->fix_php_brackets(ob_get_contents());
 639          ob_end_clean();       
 640          if(!file_exists($view_file)) {
 641              if(!file_put_contents($view_file, $add_contents)) {
 642                  echo "error creating view file: $view_file\n";
 643              } else {
 644                  echo "create $view_file\n";
 645              }
 646          } else {
 647              echo "exists $view_file\n";        
 648          } 
 649          
 650          # Generate the edit.phtml view
 651          $view_file = "$this->view_path/edit.".Trax::$views_extension;
 652          ob_start();    
 653          include("$this->scaffold_template_path/view_edit.phtml");
 654          $edit_contents = $this->fix_php_brackets(ob_get_contents());
 655          ob_end_clean(); 
 656          if(!file_exists($view_file)) {
 657              if(!file_put_contents($view_file, $edit_contents)) {
 658                  echo "error creating view file: $view_file\n";
 659              } else {
 660                  echo "create $view_file\n";
 661              }
 662          } else {
 663              echo "exists $view_file\n";        
 664          } 
 665          
 666          # Generate the show.phtml view
 667          $view_file = "$this->view_path/show.".Trax::$views_extension;
 668          ob_start();    
 669          include("$this->scaffold_template_path/view_show.phtml");
 670          $show_contents = $this->fix_php_brackets(ob_get_contents());
 671          ob_end_clean();
 672          if(!file_exists($view_file)) {
 673              if(!file_put_contents($view_file, $show_contents)) {
 674                  echo "error creating view file: $view_file\n";
 675              } else {
 676                  echo "create $view_file\n";
 677              }
 678          } else {
 679              echo "exists $view_file\n";        
 680          } 
 681                 
 682          # Generate the partial containing the form elments from the database
 683          $view_file = "$this->view_path/_form.".Trax::$views_extension;
 684          ob_start();    
 685          require "$this->scaffold_template_path/form_scaffolding.phtml";
 686          $_form_contents = $this->fix_php_brackets(ob_get_contents());
 687          ob_end_clean();  
 688          if(!file_exists($view_file)) {
 689              if(!file_put_contents($view_file, $_form_contents)) {
 690                  echo "error creating view file: $view_file\n";
 691              } else {
 692                  echo "create $view_file\n";
 693              }
 694          } else {
 695              echo "exists $view_file\n";        
 696          } 
 697          
 698          # Generate the layout for the scaffolding
 699          $layout_file = $this->layouts_path."/".$this->layout_filename.".".Trax::$views_extension;
 700          if(!file_exists($this->layouts_path)) {
 701              mkdir($this->layouts_path);        
 702          }
 703          ob_start();    
 704          include("$this->scaffold_template_path/layout.phtml");
 705          $layout_contents = $this->fix_php_brackets(ob_get_contents());
 706          ob_end_clean();  
 707          if(!file_exists($layout_file)) {
 708              if(!file_put_contents($layout_file, $layout_contents)) {
 709                  echo "error creating layout file: $layout_file\n";
 710              } else {
 711                  echo "create $layout_file\n";
 712              }
 713          } else {
 714              echo "exists $layout_file\n";        
 715          }                   
 716      }    
 717  
 718      /**
 719       *  Create a controller file with optional view methods
 720       *
 721       *  @param string $controller Name of the controller
 722       *  @param string[] $views    Name(s) of view(s), if any
 723       *  @uses controller_class    Must be set before call.
 724       *                            Not changed during call.
 725       *  @uses controller_path     Must be set before call.
 726       *                            Not changed during call.
 727       *  @uses controller_template_file Must be set before call.
 728       *                            Not changed during call.
 729       *  @todo Should return succeed/fail indication
 730       */
 731      function create_controller($controller,$views="") {
 732  
 733          $controller_file = "$this->controller_path/"
 734              . $controller . "_controller.php";
 735  
 736          if(!file_exists($controller_file)) {
 737              if(file_exists($this->controller_template_file)) {
 738                  $template = file_get_contents($this->controller_template_file);
 739                  $template = str_replace('[class_name]',
 740                                          $this->controller_class,$template);
 741                  //  Add view methods
 742                  if (!empty($views)) {
 743  
 744                      //  There are some views, add a method for each
 745                      if(is_array($views)) {
 746  
 747                          //  Multiple views in an array
 748                          foreach($views as $view) {
 749                              $class_methods[] = "\tfunction $view() {\n\t}";
 750                          }
 751                          $class_methods = implode("\n\n",$class_methods);
 752                      } else {
 753                          $class_methods = "\tfunction $views() {\n\t}\n\n";
 754                      }
 755                      $template = str_replace('[class_methods]',
 756                                              $class_methods,$template);
 757                  } else {
 758  
 759                      //  No view methods to add, so remove unneeded template
 760                      $template = str_replace('[class_methods]', '',$template);
 761                  }
 762  
 763                  if(!file_put_contents($controller_file,$template)) {
 764                      echo "error creating controller class file: "
 765                          . $controller_file . "\n";
 766                  } else {
 767                      echo "create $controller_file\n";
 768                  }
 769  
 770              } else {
 771                  echo "error controller template file doesn't exist: "
 772                      . $this->controller_template_file . "\n";
 773              }
 774          } else {
 775              echo "exists $controller_file\n";
 776          }
 777      }
 778  
 779      /**
 780       *  Create a helper file for a controller
 781       *
 782       *  @param string $controller Name of the controller
 783       *  @uses controller_class      Must be set before call.
 784       *                              Not changed during call.
 785       *  @uses helper_path           Must be set before call.
 786       *                              Not changed during call.
 787       *  @uses helper_template_file  Must be set before call.
 788       *                              Not changed during call.
 789       *  @todo Should return succeed/fail indication
 790       */
 791      function create_helper($controller) {
 792          $helper_file = "$this->helper_path/".$controller."_helper.php";
 793          if(!file_exists($helper_file)) {
 794              if(file_exists($this->helper_template_file)) {
 795                  $template = file_get_contents($this->helper_template_file);
 796                  $template = str_replace('[class_name]',
 797                                          $this->controller_class,$template);
 798                  if(!file_put_contents($helper_file,$template)) {
 799                      echo "error creating helper file: $helper_file\n";
 800                  } else {
 801                      echo "create $helper_file\n";
 802                  }
 803  
 804              } else {
 805                  echo "error helper template file doesn't exist: "
 806                      . $this->helper_template_file . "\n";
 807              }
 808          } else {
 809              echo "exists $helper_file\n";
 810          }
 811      }
 812  
 813      /**
 814       *  Create a view file if it doesn't exist
 815       *
 816       *  Create a view file in the Trax work area if the required file
 817       *  does not yet exist.  Generate the view file contents by
 818       *  customizing the view template file with information about the
 819       *  controller and view names.
 820       *
 821       *  @param string $view           Name of the view
 822       *  @param string $controller     Name of the controller
 823       *  @uses controller_class        Must be set before call.
 824       *                                Not changed during call.
 825       *  @uses view_path               Must be set before call.
 826       *                                Not changed during call.
 827       *  @uses view_template_file      Must be set before call.
 828       *                                Not changed during call.
 829       *  @todo Should return succeed/fail indication
 830       */
 831      function create_view($view, $controller) {
 832          $view_file = "$this->view_path/".$view.".".Trax::$views_extension;
 833          if(!file_exists($view_file)) {
 834              if(file_exists($this->view_template_file)) {
 835                  $template = file_get_contents($this->view_template_file);
 836                  $template = str_replace('[class_name]',
 837                                          $this->controller_class,$template);
 838                  $template = str_replace('[controller]',$controller,$template);
 839                  $template = str_replace('[view]',$view,$template);
 840                  if(!file_put_contents($view_file,$template)) {
 841                      echo "error creating view file: $view_file\n";
 842                  } else {
 843                      echo "create $view_file\n";
 844                  }
 845              } else {
 846                  echo "error view template file doesn't exist: "
 847                      . $this->view_template_file . "\n";
 848              }
 849          } else {
 850              echo "exists $view_file\n";
 851          }
 852      }
 853  
 854      /**
 855       *  Execute an operating system command
 856       *
 857       *  @param string $cmd  Command to be executed
 858       *  @todo Replace with calls to filesystem methods
 859       */
 860      function exec($cmd) {
 861          if (substr(PHP_OS, 0, 3) == 'WIN') {
 862              exec(str_replace("/","\\",$cmd));
 863          } else {
 864              exec($cmd);
 865          }
 866      }
 867      
 868      /**
 869       *  Replace "< ?php ... ? >" with "<?php ... ?>"
 870       *
 871       *  @param string $string  String to be edited
 872       *  @return string Edited input string
 873       */
 874      function fix_php_brackets($string) {
 875          return str_replace("? >", "?>",
 876                             str_replace("< ?php", "<?php", $string));
 877      }
 878  
 879      /**
 880       *  Output console help message for "generate controller"
 881       */
 882      function controller_help() {
 883          echo "Usage: php generate.php controller ControllerName [view1 view2 ...]\n\n";
 884          echo "Description:\n";
 885          echo "\tThe controller generator creates functions for a new controller and\n";
 886          echo"\tits views.\n\n";
 887          echo "\tThe generator takes a controller name and a list of views as arguments.\n";
 888          echo "\tThe controller name may be given in CamelCase or under_score and should\n";
 889          echo "\tnot be suffixed with 'Controller'.  To create a controller within a\n";
 890          echo "\tmodule, specify the controller name as 'folder/controller'.\n";
 891          echo "\tThe generator creates a controller class in app/controllers with view\n";
 892          echo "\ttemplates in app/views/controller_name.\n\n";
 893          echo "Example:\n";
 894          echo "\tphp script/generate.php controller CreditCard open debit credit close\n\n";
 895          echo "\tCredit card controller with URLs like /credit_card/debit.\n";
 896          echo "\t\tController: app/controllers/credit_card_controller.php\n";
 897          echo "\t\tViews:      app/views/credit_card/debit.phtml [...]\n";
 898          echo "\t\tHelper:     app/helpers/credit_card_helper.php\n\n";
 899          echo "Module/Folders Example:\n";
 900          echo "\tphp script/generate.php controller 'admin/credit_card' suspend late_fee\n\n";
 901          echo "\tCredit card admin controller with URLs /admin/credit_card/suspend.\n";
 902          echo "\t\tController: app/controllers/admin/credit_card_controller.php\n";
 903          echo "\t\tViews:      app/views/admin/credit_card/suspend.phtml [...]\n";
 904          echo "\t\tHelper:     app/helpers/credit_card_helper.php\n\n";
 905      }
 906  
 907      /**
 908       *  Output console help message for "generate model"
 909       */
 910      function model_help() {
 911          echo "Usage: php generate.php model ModelName\n";
 912          echo "Description:\n";
 913          echo "\tThe model generator creates functions for a new model.\n";
 914          echo "\tThe generator takes a model name as its argument.  The model name\n";
 915          echo "\tmay be given in CamelCase or under_score and should not be suffixed\n";
 916          echo "\twith 'Model'. The generator creates a model class in app/models.\n";
 917          echo "Example:\n";
 918          echo "\tphp script/generate.php model Account\n";
 919          echo "\tThis will create an Account model:\n";
 920          echo "\t\tModel:      app/models/account.php\n\n";
 921      }
 922  
 923      /**
 924       *  Output console help message for "generate mailer"
 925       */
 926      function mailer_help() {
 927          echo "Usage: php script/generate.php mailer MailerName [view1 view2 ...]\n\n";
 928          echo "Description:\n";
 929          echo "\tThe mailer generator creates class methods for a new mailer and its views.\n\n";
 930          echo "\tThe generator takes a mailer name and a list of views as arguments.\n";
 931          echo "\tThe mailer name may be given in CamelCase or under_score.\n\n";
 932          echo "\tThe generator creates a mailer class in app/models with view templates\n";
 933          echo "\tin app/views/mailer_name.\n\n";
 934          echo "Example:\n";
 935          echo "\tphp script/generate.php mailer Notifications signup forgot_password invoice\n\n";
 936          echo "\tThis will create a Notifications mailer class:\n";
 937          echo "\t\tMailer:     app/models/notifications.php\n";
 938          echo "\t\tViews:      app/views/notifications/signup.phtml [...]\n\n";  
 939      }
 940      
 941      /**
 942       *  Output console help message for "generate scaffold"
 943       */
 944      function scaffold_help() {
 945          echo "Usage: php script/generate.php scaffold ModelName [ControllerName] [view1 view2 ...]\n\n";
 946          echo "Description:\n";
 947          echo "\tThe scaffold generator creates a controller to interact with a model.\n";
 948          echo "\tIf the model does not exist, it creates the model as well.  The\n";
 949          echo "\tgenerated code is equivalent to the ( public \$scaffold = \"model\"; )\n";
 950          echo "\tdeclaration, making it easy to migrate when you wish to customize\n";
 951          echo "\tyour controller and views.\n\n";
 952          echo "\tThe generator takes a model name, an optional controller name, and a\n";
 953          echo "\tlist of views as arguments.  Scaffolded actions and views are created\n";
 954          echo "\tautomatically.\n\n";
 955          echo "\tThe auto scaffolded actions and views are:\n";
 956          echo "\t\tindex, show, add, edit, delete\n\n";
 957          echo "\tIf a controller name is not given, the plural form of the model name\n";
 958          echo "\twill be used.  The model and controller names may be given in CamelCase\n";
 959          echo "\tor under_score and should not be suffixed with 'Model' or 'Controller'.\n\n";
 960          echo "Example:\n";
 961          echo "\tphp script/generate.php scaffold Account Bank debit credit\n\n";
 962          echo "\tThis will generate an Account model and BankController with a basic\n";
 963          echo "\tuser interface.  Now create the accounts table in your database and\n";
 964          echo "\t browse to http://localhost/bank/.  Voila, you're on Trax!\n\n";
 965          echo "Module/Folders Example:\n";
 966          echo "\tphp script/generate.php scaffold CreditCard 'admin/credit_card' suspend late_fee\n\n";
 967          echo "\tThis will generate a CreditCard model and CreditCardController\n";
 968          echo "\tcontroller in the admin module.\n";            
 969      }
 970  
 971      /**
 972       *  Output console help message for unrecognized command
 973       */
 974      function generator_help() {
 975          echo "Usage:\n";
 976          echo "Generate Controller:\n";
 977          echo "php script/generate.php controller controller_name [view1 view2 ...]\n";
 978          echo "for more controller info php script/generate.php controller\n\n";
 979          echo "Generate Model:\n";
 980          echo "php script/generate.php model ModelName\n";
 981          echo "for more model info php script/generate.php model\n\n";
 982          echo "Generate Mailer:\n";
 983          echo "php script/generate.php mailer MailerName [view1 view2 ...]\n";
 984          echo "for more mailer info php script/generate.php mailer\n\n";
 985          echo "Generate Scaffold:\n";
 986          echo "php script/generate.php scaffold ModelName [controller_name] [view1 view2 ...]\n";
 987          echo "for more scaffold info php script/generate.php scaffold\n\n";        
 988      }
 989  }
 990  
 991  // -- set Emacs parameters --
 992  // Local variables:
 993  // tab-width: 4
 994  // c-basic-offset: 4
 995  // c-hanging-comment-ender-p: nil
 996  // indent-tabs-mode: nil
 997  // End:
 998  ?>


Généré le : Sun Feb 25 20:04:38 2007 par Balluche grâce à PHPXref 0.7