Scaffold generator when using plural controller names created plural helpers, while...
[akelos.git] / lib / utils / generators / scaffold / scaffold_generator.php
blob0c1189f4046241daf5a1838ecd3ca7efaab14d9f
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
18 ak_define('ACTIVE_RECORD_VALIDATE_TABLE_NAMES', false);
20 class ScaffoldGenerator extends AkelosGenerator
22 var $command_values = array('model_name','controller_name','(array)actions');
24 function cast()
26 $this->model_name = AkInflector::camelize($this->model_name);
27 $this->model_file_path = AkInflector::toModelFilename($this->model_name);
28 $this->controller_name = empty($this->controller_name) ? $this->model_name : (AkInflector::camelize($this->controller_name));
29 $this->controller_file_path = AkInflector::toControllerFilename($this->controller_name);
30 $this->controller_class_name = str_replace(array('/','::'),'_', $this->controller_name.'Controller');
31 $this->controller_name = AkInflector::demodulize($this->controller_name);
32 $this->controller_human_name = AkInflector::humanize($this->controller_name);
33 $this->helper_name = (AkInflector::is_plural($this->controller_name)?AkInflector::singularize($this->controller_name):$this->controller_name).'Helper';
34 $this->helper_var_name = '$'.AkInflector::underscore($this->helper_name);
36 $this->singular_name = AkInflector::underscore($this->model_name);
37 $this->plural_name = AkInflector::pluralize($this->singular_name);
38 $this->singular_controller_name = AkInflector::underscore($this->controller_name);
40 $this->module_preffix = AkInflector::underscore(substr($this->controller_class_name, 0, strrpos($this->controller_class_name, '_')));
41 $this->module_preffix = empty($this->module_preffix) ? '' : DS.$this->module_preffix;
44 $this->files = array(
45 'controller.php' => $this->controller_file_path,
46 /**
47 * @todo Implement generic functional tests
49 // 'functional_test.php' => AK_TEST_DIR.DS.'functional'.DS.'test_'.$this->controller_class_name.'.php',
50 'helper.php' => AK_HELPERS_DIR.$this->module_preffix.DS.trim($this->helper_var_name,'$').'.php',
51 'layout' => AK_VIEWS_DIR.DS.'layouts'.DS.$this->singular_controller_name.'.tpl',
52 'view_add' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'add.tpl',
53 'view_destroy' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'destroy.tpl',
54 'view_edit' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'edit.tpl',
55 'view_listing' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'listing.tpl',
56 'view_show' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'show.tpl',
57 'form' => AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.'_form.tpl',
60 $this->user_actions = array();
61 foreach ((array)@$this->actions as $action){
62 $this->user_actions[$action] = AK_VIEWS_DIR.$this->module_preffix.DS.$this->singular_controller_name.DS.$action.'.tpl';
67 function hasCollisions()
69 $this->collisions = array();
70 foreach (array_merge(array_values($this->files),array_values($this->user_actions)) as $file_name){
71 if(file_exists($file_name)){
72 $this->collisions[] = Ak::t('%file_name file already exists',array('%file_name'=>$file_name));
75 return count($this->collisions) > 0;
78 function generate()
80 //Generate models if they don't exist
81 $model_files = array(
82 'model'=>$this->model_file_path,
83 'installer'=>AK_APP_DIR.DS.'installers'.DS.$this->singular_name.'_installer.php',
84 'model_unit_test'=>AK_TEST_DIR.DS.'unit'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
85 'model_fixture'=> AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'models'.DS.$this->singular_name.'.php',
86 'installer_fixture'=>AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'installers'.DS.$this->singular_name.'_installer.php'
89 $this->_template_vars = (array)$this;
90 foreach ($model_files as $template=>$file_path){
91 if(!file_exists($file_path)){
92 $this->save($file_path, $this->render($template, !empty($this->sintags)));
96 // We check for common testing files
97 $common_testing_files = array(
98 'fixtures'.DS.'config'.DS.'config.php',
99 'fixtures'.DS.'app'.DS.'shared_model.php'
101 foreach ($common_testing_files as $common_testing_file){
102 if(!file_exists(AK_TEST_DIR.DS.$common_testing_file)){
103 $this->save(AK_TEST_DIR.DS.$common_testing_file,
104 file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.$common_testing_file));
108 if(file_exists($this->model_file_path)){
109 require_once(AK_APP_DIR.DS.'shared_model.php');
110 require_once($this->model_file_path);
111 if(class_exists($this->model_name)){
112 $ModelInstance =& new $this->model_name;
113 $table_name = $ModelInstance->getTableName();
114 if(!empty($table_name)){
115 $this->content_columns = $ModelInstance->getContentColumns();
116 unset(
117 $this->content_columns['updated_at'],
118 $this->content_columns['updated_on'],
119 $this->content_columns['created_at'],
120 $this->content_columns['created_on']
123 $internationalized_columns = $ModelInstance->getInternationalizedColumns();
124 foreach ($internationalized_columns as $column_name=>$languages){
125 foreach ($languages as $lang){
126 $this->content_columns[$column_name] = $this->content_columns[$lang.'_'.$column_name];
127 $this->content_columns[$column_name]['name'] = $column_name;
128 unset($this->content_columns[$lang.'_'.$column_name]);
136 $this->_template_vars = (array)$this;
137 foreach ($this->files as $template=>$file_path){
138 $this->save($file_path, $this->render($template, !empty($this->sintags)));
140 foreach ($this->user_actions as $action=>$file_path){
141 $this->assignVarToTemplate('action',$action);
142 $this->save($file_path, $this->render('view', !empty($this->sintags)));
145 $unit_test_runner = AK_TEST_DIR.DS.'unit.php';
146 if(!file_exists($unit_test_runner)){
147 Ak::file_put_contents($unit_test_runner, file_get_contents(AK_FRAMEWORK_DIR.DS.'test'.DS.'app.php'));