Fixes #149
[akelos.git] / lib / utils / generators / clone / clone_generator.php
blobf6dbd0882347ebb8035ee0f377056c4fbf817916
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>
19 class CloneGenerator extends AkelosGenerator
21 var $command_values = array('class_to_clone','class_name');
23 function _setupCloner()
25 $this->clone_setup_done = true;
26 $this->class_to_clone = AkInflector::underscore($this->class_to_clone);
27 $this->class_name = AkInflector::underscore($this->class_name);
29 $this->clone_replacements = array(
31 AkInflector::camelize($this->class_to_clone).'Controller' => AkInflector::camelize($this->class_name).'Controller',
33 AkInflector::humanize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::humanize(AkInflector::pluralize($this->class_name)),
34 AkInflector::titleize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::titleize(AkInflector::pluralize($this->class_name)),
36 AkInflector::humanize($this->class_to_clone) => AkInflector::humanize($this->class_name),
37 AkInflector::titleize($this->class_to_clone) => AkInflector::titleize($this->class_name),
39 AkInflector::camelize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::camelize(AkInflector::pluralize($this->class_name)),
40 AkInflector::pluralize($this->class_to_clone) => AkInflector::pluralize($this->class_name),
42 AkInflector::camelize($this->class_to_clone) => AkInflector::camelize($this->class_name),
43 $this->class_to_clone => $this->class_name,
48 //AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name).DS.$action.'.tpl'
50 $this->files_to_clone = array(
51 AkInflector::toModelFilename($this->class_to_clone) => AkInflector::toModelFilename($this->class_name),
52 AK_TEST_DIR.DS.'unit'.DS.'test_'.$this->class_to_clone.'.php' => AK_TEST_DIR.DS.'unit'.DS.'test_'.$this->class_name.'.php',
53 AK_TEST_DIR.DS.'fixtures'.DS.AkInflector::tableize($this->class_to_clone).'.yml' => AK_TEST_DIR.DS.'fixtures'.DS.AkInflector::tableize($this->class_name).'.yml',
54 AkInflector::toControllerFilename($this->class_to_clone) => AkInflector::toControllerFilename($this->class_name),
55 AK_TEST_DIR.DS.'functional'.DS.'test_'.AkInflector::camelize($this->class_to_clone.'_controller').'.php' => AK_TEST_DIR.DS.'functional'.DS.'test_'.AkInflector::camelize($this->class_name.'_controller').'.php',
56 AK_HELPERS_DIR.DS.AkInflector::underscore("{$this->class_to_clone}_helper").'.php' => AK_HELPERS_DIR.DS.AkInflector::underscore("{$this->class_name}_helper").'.php'
59 foreach ($this->_getControllerViews() as $view_file){
60 $this->files_to_clone[AK_VIEWS_DIR.DS.$this->class_to_clone.DS.$view_file.'.tpl'] = AK_VIEWS_DIR.DS.$this->class_name.DS.$view_file.'.tpl';
63 $this->files_to_clone[AK_VIEWS_DIR.DS.'layouts'.DS.$this->class_to_clone.'.tpl'] = AK_VIEWS_DIR.DS.'layouts'.DS.$this->class_name.'.tpl';
65 foreach (Ak::dir(AK_APP_DIR.DS.'locales'.DS.$this->class_to_clone, array('dirs'=>false)) as $locale_file) {
66 $this->files_to_clone[AK_APP_DIR.DS.'locales'.DS.$this->class_to_clone.DS.$locale_file] = AK_APP_DIR.DS.'locales'.DS.$this->class_name.DS.$locale_file;
70 function _getControllerViews()
72 $view_files = Ak::dir(AK_VIEWS_DIR.DS.$this->class_to_clone, array('dirs'=>false));
73 foreach ($view_files as $k=>$view_file){
74 $view_files[$k] = str_replace('.tpl','',$view_file);
76 return $view_files;
79 function hasCollisions()
81 $this->_setupCloner();
83 $this->collisions = array();
84 foreach ($this->files_to_clone as $origin=>$destination){
85 if(file_exists($destination)){
86 $this->collisions[] = Ak::t('%file_name file already exists',array('%file_name'=>$destination));
89 return count($this->collisions) > 0;
92 function generate()
94 if (empty($this->clone_setup_done)) {
95 $this->_setupCloner();
97 foreach ($this->files_to_clone as $origin=>$destination){
98 if(file_exists($origin)){
99 $origin_code = Ak::file_get_contents($origin);
100 $destination_code = str_replace(array_keys($this->clone_replacements), array_values($this->clone_replacements), $origin_code);
101 $this->save($destination, $destination_code);