(adapted from CloneGenerator, @author Bermi Ferrer * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org * @license GNU Lesser General Public License * * @todo Prune empty directories * @todo Remove compiled templates from views and layouts * @todo Provide more robust support for scaffolded classes with different model and controller names */ class DeleteGenerator extends AkelosGenerator { var $command_values = array('class_to_delete','controller_to_delete'); // TODO: add optional controller name (if absent, assume same as class) function _setupDeleter() { // DEBUG echo "setting up deleter ... \n"; $this->delete_setup_done = true; $this->underscored_class_to_delete = AkInflector::underscore($this->class_to_delete); if (empty($this->controller_to_delete)) { $this->controller_to_delete = $this->class_to_delete; } $this->underscored_controller_to_delete = AkInflector::underscore($this->controller_to_delete); //AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_to_delete).DS.$action.'.tpl' // Add model and its installer and test suite, plus controller and its helper and test suite $this->files_to_delete = array( AkInflector::toModelFilename($this->class_to_delete), AK_APP_DIR.DS.'installers'.DS.$this->underscored_class_to_delete.'_installer.php', // TODO: (this is not currently created, use line below) AK_TEST_DIR.DS.'unit'.DS.'test_'.$this->class_to_delete.'.php', AK_TEST_DIR.DS.'unit'.DS.'app'.DS.'models'.DS.$this->underscored_class_to_delete.'.php', // TODO: (this is not currently created, use two lines below) AK_TEST_DIR.DS.'fixtures'.DS.AkInflector::tableize($this->class_to_delete).'.yml', AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'models'.DS.$this->underscored_class_to_delete.'.php', AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'installers'.DS.$this->underscored_class_to_delete.'_installer.php', AkInflector::toControllerFilename($this->controller_to_delete), // TODO: Replace this with following inflector mothod when that method is fixed AK_HELPERS_DIR.DS.$this->underscored_controller_to_delete.'_helper.php', // FIXME: (returned filename does not follow _helper convention) AkInflector::toHelperFilename($this->controller_to_delete), // TODO: (this is not currently created, use two lines below) AK_TEST_DIR.DS.'functional'.DS.'test_'.AkInflector::camelize($this->class_to_delete.'_controller').'.php', AK_TEST_DIR.DS.'functional'.DS.'controllers'.DS.$this->underscored_controller_to_delete.'_controller'.'.php', AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'controllers'.DS.$this->underscored_controller_to_delete.'_controller'.'.php', AK_TEST_DIR.DS.'fixtures'.DS.'app'.DS.'helpers'.DS.$this->underscored_controller_to_delete.'_helper'.'.php', ); foreach ($this->_getControllerViews() as $view_file){ $this->files_to_delete[] = AK_VIEWS_DIR.DS.$this->underscored_controller_to_delete.DS.$view_file.'.tpl'; } $this->files_to_delete[] = AK_VIEWS_DIR.DS.'layouts'.DS.$this->underscored_controller_to_delete.'.tpl'; foreach (Ak::dir(AK_APP_DIR.DS.'locales'.DS.$this->underscored_class_to_delete, array('dirs'=>false)) as $locale_file) { $this->files_to_delete[] = AK_APP_DIR.DS.'locales'.DS.$this->underscored_class_to_delete.DS.$locale_file; } // DEBUG: echo "files list: \n"; // DEBUG: print_r($this->files_to_delete); } function _getControllerViews() { // FIXME: 1) This will not work for scaffolded classes that specified a different name for the controller unless the user supplies the controller name. Should the user // be notified if no controller was specified and the default controller is not found? // 2) The only way to include the controller and views when a controller with name different from the model was created, is to scan the app/controllers directory // for "var $models =" set to underscored_class_to_delete. This could lead to unintended results if there are multiple controllers that reference the same model. // Does RAILS permit this? If yes, should we let the user choose which one (or all) to delete? $view_files = Ak::dir(AK_VIEWS_DIR.DS.$this->underscored_controller_to_delete, array('dirs'=>false)); foreach ($view_files as $k=>$view_file){ $view_files[$k] = str_replace('.tpl','',$view_file); } return $view_files; } function generate() { if (empty($this->delete_setup_done)) { $this->_setupDeleter(); } foreach ($this->files_to_delete as $target){ if(file_exists($target)){ $this->remove($target); } else { // TODO: what abour directories? $this->log[] = 'WARNING: '.$target.' not found!'; } } } function remove($file_path) { $this->log[] = $file_path; Ak::file_delete($file_path); } function printLog() { if(!empty($this->log)){ echo "\n".Ak::t('The following files were submitted for removal:')."\n"; echo join("\n",$this->log)."\n"; } $this->log = array(); } } ?>