ControllerCode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class ControllerCode extends CCodeModel
  3. {
  4. public $controller;
  5. public $baseClass='Controller';
  6. public $actions='index';
  7. public function rules()
  8. {
  9. return array_merge(parent::rules(), array(
  10. array('controller, actions, baseClass', 'filter', 'filter'=>'trim'),
  11. array('controller, baseClass', 'required'),
  12. array('controller', 'match', 'pattern'=>'/^\w+[\w+\\/]*$/', 'message'=>'{attribute} should only contain word characters and slashes.'),
  13. array('actions', 'match', 'pattern'=>'/^\w+[\w\s,]*$/', 'message'=>'{attribute} should only contain word characters, spaces and commas.'),
  14. array('baseClass', 'match', 'pattern'=>'/^[a-zA-Z_\\\\][\w\\\\]*$/', 'message'=>'{attribute} should only contain word characters and backslashes.'),
  15. array('baseClass', 'validateReservedWord', 'skipOnError'=>true),
  16. array('baseClass, actions', 'sticky'),
  17. ));
  18. }
  19. public function attributeLabels()
  20. {
  21. return array_merge(parent::attributeLabels(), array(
  22. 'baseClass'=>'Base Class',
  23. 'controller'=>'Controller ID',
  24. 'actions'=>'Action IDs',
  25. ));
  26. }
  27. public function requiredTemplates()
  28. {
  29. return array(
  30. 'controller.php',
  31. 'view.php',
  32. );
  33. }
  34. public function successMessage()
  35. {
  36. $link=CHtml::link('try it now', Yii::app()->createUrl($this->controller), array('target'=>'_blank'));
  37. return "The controller has been generated successfully. You may $link.";
  38. }
  39. public function prepare()
  40. {
  41. $this->files=array();
  42. $templatePath=$this->templatePath;
  43. $this->files[]=new CCodeFile(
  44. $this->controllerFile,
  45. $this->render($templatePath.'/controller.php')
  46. );
  47. foreach($this->getActionIDs() as $action)
  48. {
  49. $this->files[]=new CCodeFile(
  50. $this->getViewFile($action),
  51. $this->render($templatePath.'/view.php', array('action'=>$action))
  52. );
  53. }
  54. }
  55. public function getActionIDs()
  56. {
  57. $actions=preg_split('/[\s,]+/',$this->actions,-1,PREG_SPLIT_NO_EMPTY);
  58. $actions=array_unique($actions);
  59. sort($actions);
  60. return $actions;
  61. }
  62. public function getControllerClass()
  63. {
  64. if(($pos=strrpos($this->controller,'/'))!==false)
  65. return ucfirst(substr($this->controller,$pos+1)).'Controller';
  66. else
  67. return ucfirst($this->controller).'Controller';
  68. }
  69. public function getModule()
  70. {
  71. if(($pos=strpos($this->controller,'/'))!==false)
  72. {
  73. $id=substr($this->controller,0,$pos);
  74. if(($module=Yii::app()->getModule($id))!==null)
  75. return $module;
  76. }
  77. return Yii::app();
  78. }
  79. public function getControllerID()
  80. {
  81. if($this->getModule()!==Yii::app())
  82. $id=substr($this->controller,strpos($this->controller,'/')+1);
  83. else
  84. $id=$this->controller;
  85. if(($pos=strrpos($id,'/'))!==false)
  86. $id[$pos+1]=strtolower($id[$pos+1]);
  87. else
  88. $id[0]=strtolower($id[0]);
  89. return $id;
  90. }
  91. public function getUniqueControllerID()
  92. {
  93. $id=$this->controller;
  94. if(($pos=strrpos($id,'/'))!==false)
  95. $id[$pos+1]=strtolower($id[$pos+1]);
  96. else
  97. $id[0]=strtolower($id[0]);
  98. return $id;
  99. }
  100. public function getControllerFile()
  101. {
  102. $module=$this->getModule();
  103. $id=$this->getControllerID();
  104. if(($pos=strrpos($id,'/'))!==false)
  105. $id[$pos+1]=strtoupper($id[$pos+1]);
  106. else
  107. $id[0]=strtoupper($id[0]);
  108. return $module->getControllerPath().'/'.$id.'Controller.php';
  109. }
  110. public function getViewFile($action)
  111. {
  112. $module=$this->getModule();
  113. return $module->getViewPath().'/'.$this->getControllerID().'/'.$action.'.php';
  114. }
  115. }