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.

Generator.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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\extension;
  8. use Yii;
  9. use yii\gii\CodeFile;
  10. /**
  11. * This generator will generate the skeleton files needed by an extension.
  12. *
  13. * @property string $keywordsArrayJson A json encoded array with the given keywords. This property is
  14. * read-only.
  15. * @property boolean $outputPath The directory that contains the module class. This property is read-only.
  16. *
  17. * @author Tobias Munk <schmunk@usrbin.de>
  18. * @since 2.0
  19. */
  20. class Generator extends \yii\gii\Generator
  21. {
  22. public $vendorName;
  23. public $packageName = "yii2-";
  24. public $namespace;
  25. public $type = "yii2-extension";
  26. public $keywords = "yii2,extension";
  27. public $title;
  28. public $description;
  29. public $outputPath = "@app/runtime/tmp-extensions";
  30. public $license;
  31. public $authorName;
  32. public $authorEmail;
  33. /**
  34. * @inheritdoc
  35. */
  36. public function getName()
  37. {
  38. return 'Extension Generator';
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function getDescription()
  44. {
  45. return 'This generator helps you to generate the files needed by a Yii extension.';
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function rules()
  51. {
  52. return array_merge(
  53. parent::rules(),
  54. [
  55. [['vendorName', 'packageName'], 'filter', 'filter' => 'trim'],
  56. [
  57. [
  58. 'vendorName',
  59. 'packageName',
  60. 'namespace',
  61. 'type',
  62. 'license',
  63. 'title',
  64. 'description',
  65. 'authorName',
  66. 'authorEmail',
  67. 'outputPath'
  68. ],
  69. 'required'
  70. ],
  71. [['keywords'], 'safe'],
  72. [['authorEmail'], 'email'],
  73. [
  74. ['vendorName', 'packageName'],
  75. 'match',
  76. 'pattern' => '/^[a-z0-9\-\.]+$/',
  77. 'message' => 'Only lowercase word characters, dashes and dots are allowed.'
  78. ],
  79. [
  80. ['namespace'],
  81. 'match',
  82. 'pattern' => '/^[a-zA-Z0-9\\\]+\\\$/',
  83. 'message' => 'Only letters, numbers and backslashes are allowed. PSR-4 namespaces must end with a namespace separator.'
  84. ],
  85. ]
  86. );
  87. }
  88. /**
  89. * @inheritdoc
  90. */
  91. public function attributeLabels()
  92. {
  93. return [
  94. 'vendorName' => 'Vendor Name',
  95. 'packageName' => 'Package Name',
  96. 'license' => 'License',
  97. ];
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function hints()
  103. {
  104. return [
  105. 'vendorName' => 'This refers to the name of the publisher, your GitHub user name is usually a good choice, eg. <code>myself</code>.',
  106. 'packageName' => 'This is the name of the extension on packagist, eg. <code>yii2-foobar</code>.',
  107. 'namespace' => 'PSR-4, eg. <code>myself\foobar\</code> This will be added to your autoloading by composer. Do not use yii, yii2 or yiisoft in the namespace.',
  108. 'keywords' => 'Comma separated keywords for this extension.',
  109. 'outputPath' => 'The temporary location of the generated files.',
  110. 'title' => 'A more descriptive name of your application for the README file.',
  111. 'description' => 'A sentence or subline describing the main purpose of the extension.',
  112. ];
  113. }
  114. /**
  115. * @inheritdoc
  116. */
  117. public function stickyAttributes()
  118. {
  119. return ['vendorName', 'outputPath', 'authorName', 'authorEmail'];
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function successMessage()
  125. {
  126. $outputPath = realpath(\Yii::getAlias($this->outputPath));
  127. $output1 = <<<EOD
  128. <p><em>The extension has been generated successfully.</em></p>
  129. <p>To enable it in your application, you need to create a git repository
  130. and require it via composer.</p>
  131. EOD;
  132. $code1 = <<<EOD
  133. cd {$outputPath}/{$this->packageName}
  134. git init
  135. git add -A
  136. git commit
  137. git remote add origin https://path.to/your/repo
  138. git push -u origin master
  139. EOD;
  140. $output2 = <<<EOD
  141. <p>The next step is just for <em>initial development</em>, skip it if you directly publish the extension on packagist.org</p>
  142. <p>Add the newly created repo to your composer.json.</p>
  143. EOD;
  144. $code2 = <<<EOD
  145. "repositories":[
  146. {
  147. "type": "git",
  148. "url": "https://path.to/your/repo"
  149. }
  150. ]
  151. EOD;
  152. $output3 = <<<EOD
  153. <p class="well">Note: You may use the url <code>file://{$outputPath}/{$this->packageName}</code> for testing.</p>
  154. <p>Require the package with composer</p>
  155. EOD;
  156. $code3 = <<<EOD
  157. composer.phar require {$this->vendorName}/{$this->packageName}:dev-master
  158. EOD;
  159. $output4 = <<<EOD
  160. <p>And use it in your application.</p>
  161. EOD;
  162. $code4 = <<<EOD
  163. \\{$this->namespace}AutoloadExample::widget();
  164. EOD;
  165. $output5 = <<<EOD
  166. <p>When you have finished development register your extension at <a href='https://packagist.org/' target='_blank'>packagist.org</a>.</p>
  167. EOD;
  168. $return = $output1 . '<pre>' . highlight_string($code1, true) . '</pre>';
  169. $return .= $output2 . '<pre>' . highlight_string($code2, true) . '</pre>';
  170. $return .= $output3 . '<pre>' . highlight_string($code3, true) . '</pre>';
  171. $return .= $output4 . '<pre>' . highlight_string($code4, true) . '</pre>';
  172. $return .= $output5;
  173. return $return;
  174. }
  175. /**
  176. * @inheritdoc
  177. */
  178. public function requiredTemplates()
  179. {
  180. return ['composer.json', 'AutoloadExample.php', 'README.md'];
  181. }
  182. /**
  183. * @inheritdoc
  184. */
  185. public function generate()
  186. {
  187. $files = [];
  188. $modulePath = $this->getOutputPath();
  189. $files[] = new CodeFile(
  190. $modulePath . '/' . $this->packageName . '/composer.json',
  191. $this->render("composer.json")
  192. );
  193. $files[] = new CodeFile(
  194. $modulePath . '/' . $this->packageName . '/AutoloadExample.php',
  195. $this->render("AutoloadExample.php")
  196. );
  197. $files[] = new CodeFile(
  198. $modulePath . '/' . $this->packageName . '/README.md',
  199. $this->render("README.md")
  200. );
  201. return $files;
  202. }
  203. /**
  204. * @return boolean the directory that contains the module class
  205. */
  206. public function getOutputPath()
  207. {
  208. return Yii::getAlias($this->outputPath);
  209. }
  210. /**
  211. * @return string a json encoded array with the given keywords
  212. */
  213. public function getKeywordsArrayJson()
  214. {
  215. return json_encode(explode(',', $this->keywords), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  216. }
  217. /**
  218. * @return array options for type drop-down
  219. */
  220. public function optsType()
  221. {
  222. $licenses = [
  223. 'yii2-extension',
  224. 'library',
  225. ];
  226. return array_combine($licenses, $licenses);
  227. }
  228. /**
  229. * @return array options for license drop-down
  230. */
  231. public function optsLicense()
  232. {
  233. $licenses = [
  234. 'Apache-2.0',
  235. 'BSD-2-Clause',
  236. 'BSD-3-Clause',
  237. 'BSD-4-Clause',
  238. 'GPL-2.0',
  239. 'GPL-2.0+',
  240. 'GPL-3.0',
  241. 'GPL-3.0+',
  242. 'LGPL-2.1',
  243. 'LGPL-2.1+',
  244. 'LGPL-3.0',
  245. 'LGPL-3.0+',
  246. 'MIT'
  247. ];
  248. return array_combine($licenses, $licenses);
  249. }
  250. }