ModuleCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * ModuleCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. * @version $Id: ModuleCommand.php 433 2008-12-30 22:59:17Z qiang.xue $
  10. */
  11. /**
  12. * ModuleCommand generates a controller class.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @version $Id: ModuleCommand.php 433 2008-12-30 22:59:17Z qiang.xue $
  16. * @package system.cli.commands.shell
  17. */
  18. class ModuleCommand extends CConsoleCommand
  19. {
  20. /**
  21. * @var string the directory that contains templates for the module command.
  22. * Defaults to null, meaning using 'framework/cli/views/shell/module'.
  23. * If you set this path and some views are missing in the directory,
  24. * the default views will be used.
  25. */
  26. public $templatePath;
  27. public function getHelp()
  28. {
  29. return <<<EOD
  30. USAGE
  31. module <module-ID>
  32. DESCRIPTION
  33. This command generates an application module.
  34. PARAMETERS
  35. * module-ID: required, module ID. It is case-sensitive.
  36. EOD;
  37. }
  38. /**
  39. * Execute the action.
  40. * @param array $args command line parameters specific for this command
  41. * @return integer|null non zero application exit code for help or null on success
  42. */
  43. public function run($args)
  44. {
  45. if(!isset($args[0]))
  46. {
  47. echo "Error: module ID is required.\n";
  48. echo $this->getHelp();
  49. return 1;
  50. }
  51. $moduleID=$args[0];
  52. $moduleClass=ucfirst($moduleID).'Module';
  53. $modulePath=Yii::app()->getModulePath().DIRECTORY_SEPARATOR.$moduleID;
  54. $sourceDir=$this->templatePath===null?YII_PATH.'/cli/views/shell/module':$this->templatePath;
  55. $list=$this->buildFileList($sourceDir,$modulePath);
  56. $list['module.php']['target']=$modulePath.DIRECTORY_SEPARATOR.$moduleClass.'.php';
  57. $list['module.php']['callback']=array($this,'generateModuleClass');
  58. $list['module.php']['params']=array(
  59. 'moduleClass'=>$moduleClass,
  60. 'moduleID'=>$moduleID,
  61. );
  62. $list[$moduleClass.'.php']=$list['module.php'];
  63. unset($list['module.php']);
  64. $this->copyFiles($list);
  65. echo <<<EOD
  66. Module '{$moduleID}' has been created under the following folder:
  67. $modulePath
  68. You may access it in the browser using the following URL:
  69. http://hostname/path/to/index.php?r=$moduleID
  70. Note, the module needs to be installed first by adding '{$moduleID}'
  71. to the 'modules' property in the application configuration.
  72. EOD;
  73. }
  74. public function generateModuleClass($source,$params)
  75. {
  76. return $this->renderFile($source,$params,true);
  77. }
  78. }