1<?php 2 3class ModuleCode extends CCodeModel 4{ 5 public $moduleID; 6 7 public function rules() 8 { 9 return array_merge(parent::rules(), array( 10 array('moduleID', 'filter', 'filter'=>'trim'), 11 array('moduleID', 'required'), 12 array('moduleID', 'match', 'pattern'=>'/^\w+$/', 'message'=>'{attribute} should only contain word characters.'), 13 )); 14 } 15 16 public function attributeLabels() 17 { 18 return array_merge(parent::attributeLabels(), array( 19 'moduleID'=>'Module ID', 20 )); 21 } 22 23 public function successMessage() 24 { 25 if(Yii::app()->hasModule($this->moduleID)) 26 return 'The module has been generated successfully. You may '.CHtml::link('try it now', Yii::app()->createUrl($this->moduleID), array('target'=>'_blank')).'.'; 27 28 $output=<<<EOD 29<p>The module has been generated successfully.</p> 30<p>To access the module, you need to modify the application configuration as follows:</p> 31EOD; 32 $code=<<<EOD 33<?php 34return array( 35 'modules'=>array( 36 '{$this->moduleID}', 37 ), 38 ...... 39); 40EOD; 41 42 return $output.highlight_string($code,true); 43 } 44 45 public function prepare() 46 { 47 $this->files=array(); 48 $templatePath=$this->templatePath; 49 $modulePath=$this->modulePath; 50 $moduleTemplateFile=$templatePath.DIRECTORY_SEPARATOR.'module.php'; 51 52 $this->files[]=new CCodeFile( 53 $modulePath.'/'.$this->moduleClass.'.php', 54 $this->render($moduleTemplateFile) 55 ); 56 57 $files=CFileHelper::findFiles($templatePath,array( 58 'exclude'=>array('.svn'), 59 )); 60 61 foreach($files as $file) 62 { 63 if($file!==$moduleTemplateFile) 64 { 65 if(CFileHelper::getExtension($file)==='php') 66 $content=$this->render($file); 67 else if(basename($file)==='.yii') // an empty directory 68 { 69 $file=dirname($file); 70 $content=null; 71 } 72 else 73 $content=file_get_contents($file); 74 $this->files[]=new CCodeFile( 75 $modulePath.substr($file,strlen($templatePath)), 76 $content 77 ); 78 } 79 } 80 } 81 82 public function getModuleClass() 83 { 84 return ucfirst($this->moduleID).'Module'; 85 } 86 87 public function getModulePath() 88 { 89 return Yii::app()->modulePath.DIRECTORY_SEPARATOR.$this->moduleID; 90 } 91}