ModuleCode.php 2.1 KB

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