[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/utils/generators/scaffold/ -> scaffold_generator.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActiveSupport
  13   * @subpackage Generators
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  ak_define('ACTIVE_RECORD_VALIDATE_TABLE_NAMES', false);
  19  
  20  include_once (AK_LIB_DIR.DS.'AkInstaller.php');
  21  
  22  class ScaffoldGenerator extends  AkelosGenerator
  23  {
  24      var $_skip_files = array();
  25      var $command_values = array('model_name','controller_name','(array)actions');
  26  
  27      function cast()
  28      {
  29          $this->model_name = AkInflector::camelize($this->model_name);
  30          $this->model_file_path = AkInflector::toModelFilename($this->model_name);
  31          $this->controller_name = empty($this->controller_name) ? $this->model_name : (AkInflector::camelize($this->controller_name));
  32          $this->controller_file_path = AkInflector::toControllerFilename($this->controller_name);
  33          $this->controller_class_name = str_replace(array('/','::'),'_', $this->controller_name.'Controller');
  34          $this->controller_name = AkInflector::demodulize($this->controller_name);
  35          $this->controller_human_name = AkInflector::humanize($this->controller_name);
  36          $this->helper_name = (AkInflector::is_plural($this->controller_name)?AkInflector::singularize($this->controller_name):$this->controller_name).'Helper';
  37          $this->helper_var_name = '$'.AkInflector::underscore($this->helper_name);
  38  
  39          $this->singular_name = AkInflector::underscore($this->model_name);
  40          $this->plural_name = AkInflector::pluralize($this->singular_name);
  41          $this->singular_controller_name = AkInflector::underscore($this->controller_name);
  42  
  43          $this->module_preffix = AkInflector::underscore(substr($this->controller_class_name, 0, strrpos($this->controller_class_name, '_')));
  44          $this->module_preffix = empty($this->module_preffix) ? '' : DS.$this->module_preffix;
  45  
  46  
  47          $this->files = array(
  48          'controller.php' => $this->controller_file_path,
  49          /**
  50           * @todo Implement generic functional tests
  51           */
  52          // 'functional_test.php' => AK_TEST_DIR.DS.'functional'.DS.'test_'.$this->controller_class_name.'.php',
  53          'helper.php' => AK_HELPERS_DIR.$this->module_preffix.DS.trim($this->helper_var_name,'$').'.php',
  54          'layout' => AK_VIEWS_DIR.DS.'layouts'.DS.$this->singular_controller_name.'.tpl',
  55          'view_add' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'add.tpl',
  56          'view_destroy' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'destroy.tpl',
  57          'view_edit' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'edit.tpl',
  58          'view_listing' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'listing.tpl',
  59          'view_show' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'show.tpl',
  60          'form' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'_form.tpl',
  61          );
  62  
  63          $this->user_actions = array();
  64          foreach ((array)@$this->actions as $action){
  65              $this->user_actions[$action] = AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.$action.'.tpl';
  66          }
  67  
  68      }
  69  
  70      function hasCollisions()
  71      {
  72          $this->collisions = array();
  73          foreach (array_merge(array_values($this->files),array_values($this->user_actions)) as $file_name){
  74              $user_answer = 5;
  75              if($user_answer != 3 && file_exists($file_name)){
  76                  $message = Ak::t('%file_name file already exists',array('%file_name'=>$file_name));
  77                  $user_answer = AkInstaller::promptUserVar($message."\n".
  78                  "Would you like to:\n".
  79                  " 1) overwrite file\n".
  80                  " 2) keep existing file\n".
  81                  " 3) overwrite all\n".
  82                  " 4) keep all\n".
  83                  " 5) abort\n", array('default' => 5));
  84                  
  85                  if($user_answer == 2 || $user_answer == 4){
  86                      $this->_skip_files[] = $file_name;
  87                  }elseif($user_answer == 5){
  88                      $this->collisions[] = $message;
  89                  }
  90              }
  91          }
  92          return count($this->collisions) > 0;
  93      }
  94  
  95      function generate()
  96      {
  97          //Generate models if they don't exist
  98          $model_files = array(
  99          'model'=>$this->model_file_path,
 100          'installer'=>AK_APP_DIR.DS.'installers'.DS.$this->singular_name.'_installer.php',
 101          'model_unit_test'=>AK_TEST_DIR.DS.'unit'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
 102          'model_fixture'=>    AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
 103          'installer_fixture'=>AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'installers'.DS.$this->singular_name.'_installer.php'
 104          );
 105  
 106          $this->_template_vars = (array)$this;
 107          foreach ($model_files as $template=>$file_path){
 108              if(!file_exists($file_path)){
 109                  $this->save($file_path, $this->render($template, !empty($this->sintags)));
 110              }
 111          }
 112  
 113          // We check for common testing files
 114          $common_testing_files = array(
 115          'fixtures'.DS.'config'.DS.'config.php',
 116          'fixtures'.DS.'app'.DS.'shared_model.php'
 117          );
 118          foreach ($common_testing_files as $common_testing_file){
 119              if(!file_exists(AK_TEST_DIR.DS.$common_testing_file)){
 120                  $this->save(AK_TEST_DIR.DS.$common_testing_file,
 121                  file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.$common_testing_file));
 122              }
 123          }
 124  
 125          if(file_exists($this->model_file_path)){
 126              require_once(AK_APP_DIR.DS.'shared_model.php');
 127              require_once($this->model_file_path);
 128              if(class_exists($this->model_name)){
 129                  $ModelInstance =& new $this->model_name;
 130                  $table_name = $ModelInstance->getTableName();
 131                  if(!empty($table_name)){
 132                      $this->content_columns = $ModelInstance->getContentColumns();
 133                      unset(
 134                      $this->content_columns['updated_at'],
 135                      $this->content_columns['updated_on'],
 136                      $this->content_columns['created_at'],
 137                      $this->content_columns['created_on']
 138                      );
 139                  }
 140                  $internationalized_columns = $ModelInstance->getInternationalizedColumns();
 141                  foreach ($internationalized_columns as $column_name=>$languages){
 142                      foreach ($languages as $lang){
 143                          $this->content_columns[$column_name] = $this->content_columns[$lang.'_'.$column_name];
 144                          $this->content_columns[$column_name]['name'] = $column_name;
 145                          unset($this->content_columns[$lang.'_'.$column_name]);
 146                      }
 147                  }
 148  
 149  
 150              }
 151          }
 152  
 153          $this->_template_vars = (array)$this;
 154          foreach ($this->files as $template=>$file_path){
 155              if(!in_array($file_path,$this->_skip_files)){
 156                  $this->save($file_path, $this->render($template, !empty($this->sintags)));
 157              }
 158          }
 159          foreach ($this->user_actions as $action=>$file_path){
 160              $this->assignVarToTemplate('action',$action);
 161              $this->save($file_path, $this->render('view', !empty($this->sintags)));
 162          }
 163  
 164          $unit_test_runner = AK_TEST_DIR.DS.'unit.php';
 165          if(!file_exists($unit_test_runner)){
 166              Ak::file_put_contents($unit_test_runner, file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.'app.php'));
 167          }
 168  
 169      }
 170  }
 171  
 172  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6