You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\generators\module;
  8. use yii\gii\CodeFile;
  9. use yii\helpers\Html;
  10. use Yii;
  11. use yii\helpers\StringHelper;
  12. /**
  13. * This generator will generate the skeleton code needed by a module.
  14. *
  15. * @property string $controllerNamespace The controller namespace of the module. This property is read-only.
  16. * @property boolean $modulePath The directory that contains the module class. This property is read-only.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class Generator extends \yii\gii\Generator
  22. {
  23. public $moduleClass;
  24. public $moduleID;
  25. /**
  26. * @inheritdoc
  27. */
  28. public function getName()
  29. {
  30. return 'Module Generator';
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function getDescription()
  36. {
  37. return 'This generator helps you to generate the skeleton code needed by a Yii module.';
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return array_merge(parent::rules(), [
  45. [['moduleID', 'moduleClass'], 'filter', 'filter' => 'trim'],
  46. [['moduleID', 'moduleClass'], 'required'],
  47. [['moduleID'], 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
  48. [['moduleClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
  49. [['moduleClass'], 'validateModuleClass'],
  50. ]);
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'moduleID' => 'Module ID',
  59. 'moduleClass' => 'Module Class',
  60. ];
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function hints()
  66. {
  67. return [
  68. 'moduleID' => 'This refers to the ID of the module, e.g., <code>admin</code>.',
  69. 'moduleClass' => 'This is the fully qualified class name of the module, e.g., <code>app\modules\admin\Module</code>.',
  70. ];
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function successMessage()
  76. {
  77. if (Yii::$app->hasModule($this->moduleID)) {
  78. $link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']);
  79. return "The module has been generated successfully. You may $link.";
  80. }
  81. $output = <<<EOD
  82. <p>The module has been generated successfully.</p>
  83. <p>To access the module, you need to add this to your application configuration:</p>
  84. EOD;
  85. $code = <<<EOD
  86. <?php
  87. ......
  88. 'modules' => [
  89. '{$this->moduleID}' => [
  90. 'class' => '{$this->moduleClass}',
  91. ],
  92. ],
  93. ......
  94. EOD;
  95. return $output . '<pre>' . highlight_string($code, true) . '</pre>';
  96. }
  97. /**
  98. * @inheritdoc
  99. */
  100. public function requiredTemplates()
  101. {
  102. return ['module.php', 'controller.php', 'view.php'];
  103. }
  104. /**
  105. * @inheritdoc
  106. */
  107. public function generate()
  108. {
  109. $files = [];
  110. $modulePath = $this->getModulePath();
  111. $files[] = new CodeFile(
  112. $modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php',
  113. $this->render("module.php")
  114. );
  115. $files[] = new CodeFile(
  116. $modulePath . '/controllers/DefaultController.php',
  117. $this->render("controller.php")
  118. );
  119. $files[] = new CodeFile(
  120. $modulePath . '/views/default/index.php',
  121. $this->render("view.php")
  122. );
  123. return $files;
  124. }
  125. /**
  126. * Validates [[moduleClass]] to make sure it is a fully qualified class name.
  127. */
  128. public function validateModuleClass()
  129. {
  130. if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass), false) === false) {
  131. $this->addError('moduleClass', 'Module class must be properly namespaced.');
  132. }
  133. if (empty($this->moduleClass) || substr_compare($this->moduleClass, '\\', -1, 1) === 0) {
  134. $this->addError('moduleClass', 'Module class name must not be empty. Please enter a fully qualified class name. e.g. "app\\modules\\admin\\Module".');
  135. }
  136. }
  137. /**
  138. * @return boolean the directory that contains the module class
  139. */
  140. public function getModulePath()
  141. {
  142. return Yii::getAlias('@' . str_replace('\\', '/', substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\'))));
  143. }
  144. /**
  145. * @return string the controller namespace of the module.
  146. */
  147. public function getControllerNamespace()
  148. {
  149. return substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\')) . '\controllers';
  150. }
  151. }