Generator.php 6.06 KB
Newer Older
1 2
<?php
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6 7 8 9 10
 */

namespace yii\gii\generators\extension;

use Yii;
11
use yii\gii\CodeFile;
12 13 14

/**
 * This generator will generate the skeleton files needed by an extension.
Tobias Munk committed
15
 *
16
 * @author Tobias Munk <schmunk@usrbin.de>
17
 * @since 2.0
18 19 20 21
 */
class Generator extends \yii\gii\Generator
{
	public $vendorName;
22
	public $packageName = "yii2-";
23
	public $namespace;
24
	public $type = "yii2-extension";
25
	public $keywords = "yii2,extension";
26 27
	public $title;
	public $description;
28
	public $outputPath = "@app/runtime/tmp-extensions";
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	public $license;
	public $authorName;
	public $authorEmail;

	/**
	 * @inheritdoc
	 */
	public function getName()
	{
		return 'Extension Generator';
	}

	/**
	 * @inheritdoc
	 */
	public function getDescription()
	{
		return 'This generator helps you to generate the files needed by a Yii extension.';
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		return array_merge(
			parent::rules(),
			[
				[['vendorName', 'packageName'], 'filter', 'filter' => 'trim'],
				[
					[
						'vendorName',
						'packageName',
						'namespace',
						'type',
						'license',
						'title',
						'description',
						'authorName',
						'authorEmail',
						'outputPath'
					],
					'required'
				],
73
				[['keywords'], 'safe'],
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
				[['authorEmail'], 'email'],
				[
					['vendorName', 'packageName'],
					'match',
					'pattern' => '/^[a-z0-9\-\.]+$/',
					'message' => 'Only lowercase word characters, dashes and dots are allowed.'
				],
				[
					['namespace'],
					'match',
					'pattern' => '/^[a-zA-Z0-9\\\]+\\\$/',
					'message' => 'Only letters, numbers and backslashes are allowed. PSR-4 namespaces must end with a namespace separator.'
				],
			]
		);
89 90 91 92 93 94 95 96
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
97
			'vendorName'  => 'Vendor Name',
98
			'packageName' => 'Package Name',
99
			'license'     => 'License',
100 101 102 103 104 105 106 107 108
		];
	}

	/**
	 * @inheritdoc
	 */
	public function hints()
	{
		return [
Tobias Munk committed
109 110 111
			'vendorName'  => 'This refers to the name of the publisher, your GitHub user name is usually a good choice, eg. <code>myself</code>.',
			'packageName' => 'This is the name of the extension on packagist, eg. <code>yii2-foobar</code>.',
			'namespace'   => 'PSR-4, eg. <code>myself\foobar\</code> This will be added to your autoloading by composer. Do not use yii or yii2 in the namespace.',
112 113 114
			'keywords'    => 'Comma separated keywords for this extension.',
			'outputPath'  => 'The temporary location of the generated files.',
			'title'       => 'A more descriptive name of your application for the README file.',
115 116 117 118 119 120 121 122 123
			'description' => 'A sentence or subline describing the main purpose of the extension.',
		];
	}

	/**
	 * @inheritdoc
	 */
	public function stickyAttributes()
	{
124
		return ['vendorName', 'outputPath', 'authorName', 'authorEmail'];
125 126 127 128 129 130 131
	}

	/**
	 * @inheritdoc
	 */
	public function successMessage()
	{
132
		$outputPath = realpath(\Yii::getAlias($this->outputPath));
133
		$output1 = <<<EOD
134
<p><em>The extension has been generated successfully.</em></p>
135
<p>To enable it in your application, you need to create a git repository
Tobias Munk committed
136
and require it via composer.</p>
137
EOD;
138
		$code1 = <<<EOD
139
cd {$outputPath}/{$this->packageName}
140 141 142 143

git init
git add -A
git commit
144 145
git remote add origin https://path.to/your/repo
git push -u origin master
146
EOD;
147
		$output2 = <<<EOD
Tobias Munk committed
148
<p>The next step is just for <em>initial development</em>, skip it if you directly publish the extension on packagist.org</p>
149 150
<p>Add the newly created repo to your composer.json.</p>
EOD;
151
		$code2 = <<<EOD
152 153 154
"repositories":[
	{
		"type": "git",
155
		"url": "https://path.to/your/repo"
156 157 158
	}
]
EOD;
159
		$output3 = <<<EOD
160
<p class="well">Note: You may use the url <code>file://{$outputPath}/{$this->packageName}</code> for testing.</p>
161 162
<p>Require the package with composer</p>
EOD;
163
		$code3 = <<<EOD
164
composer.phar require {$this->vendorName}/{$this->packageName}:dev-master
165
EOD;
166
		$output4 = <<<EOD
167 168
<p>And use it in your application.</p>
EOD;
169
		$code4 = <<<EOD
170
\\{$this->namespace}AutoloadExample::widget();
171
EOD;
172
		$output5 = <<<EOD
Tobias Munk committed
173 174 175
<p>When you have finished development register your extension at <a href='https://packagist.org/' target='_blank'>packagist.org</a>.</p>
EOD;

176
		$return = $output1 . '<pre>' . highlight_string($code1, true) . '</pre>';
177 178 179
		$return .= $output2 . '<pre>' . highlight_string($code2, true) . '</pre>';
		$return .= $output3 . '<pre>' . highlight_string($code3, true) . '</pre>';
		$return .= $output4 . '<pre>' . highlight_string($code4, true) . '</pre>';
Tobias Munk committed
180
		$return .= $output5;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		return $return;
	}

	/**
	 * @inheritdoc
	 */
	public function requiredTemplates()
	{
		return ['composer.json', 'AutoloadExample.php', 'README.md'];
	}

	/**
	 * @inheritdoc
	 */
	public function generate()
	{
197
		$files = [];
198
		$modulePath = $this->getOutputPath();
199
		$files[] = new CodeFile(
200 201 202
			$modulePath . '/' . $this->packageName . '/composer.json',
			$this->render("composer.json")
		);
203
		$files[] = new CodeFile(
204 205 206
			$modulePath . '/' . $this->packageName . '/AutoloadExample.php',
			$this->render("AutoloadExample.php")
		);
207
		$files[] = new CodeFile(
208 209 210 211 212 213 214 215 216 217 218 219
			$modulePath . '/' . $this->packageName . '/README.md',
			$this->render("README.md")
		);
		return $files;
	}

	/**
	 * @return boolean the directory that contains the module class
	 */
	public function getOutputPath()
	{
		return Yii::getAlias($this->outputPath);
220 221 222
	}

	/**
223
	 * @return string a json encoded array with the given keywords
224 225 226 227
	 */
	public function getKeywordsArrayJson()
	{
		return json_encode(explode(',', $this->keywords));
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	}

	/**
	 * @return array options for type drop-down
	 */
	public function optsType()
	{
		$licenses = [
			'yii2-extension',
			'library',
		];
		return array_combine($licenses, $licenses);
	}

	/**
	 * @return array options for license drop-down
	 */
	public function optsLicense()
	{
		$licenses = [
			'Apache-2.0',
			'BSD-2-Clause',
			'BSD-3-Clause',
			'BSD-4-Clause',
			'GPL-2.0',
			'GPL-2.0+',
			'GPL-3.0',
			'GPL-3.0+',
			'LGPL-2.1',
			'LGPL-2.1+',
			'LGPL-3.0',
			'LGPL-3.0+',
			'MIT'
		];
		return array_combine($licenses, $licenses);
	}
}