Commit be54f24b by Tobias Munk

updated defaults, UI, validation and templates

parent 01d444f6
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\gii\generators\extension; namespace yii\gii\generators\extension;
...@@ -14,11 +14,9 @@ use yii\helpers\StringHelper; ...@@ -14,11 +14,9 @@ use yii\helpers\StringHelper;
/** /**
* This generator will generate the skeleton files needed by an extension. * This generator will generate the skeleton files needed by an extension.
*
* @property tbd * @property tbd
*
* @author Tobias Munk <schmunk@usrbin.de> * @author Tobias Munk <schmunk@usrbin.de>
* @since 2.0 * @since 2.0
*/ */
class Generator extends \yii\gii\Generator class Generator extends \yii\gii\Generator
{ {
...@@ -26,10 +24,10 @@ class Generator extends \yii\gii\Generator ...@@ -26,10 +24,10 @@ class Generator extends \yii\gii\Generator
public $packageName = "yii2-"; public $packageName = "yii2-";
public $namespace; public $namespace;
public $type = "yii2-extension"; public $type = "yii2-extension";
public $keywords = "yii2,"; public $keywords = "yii2,extension";
public $title; public $title;
public $description; public $description;
public $outputPath = "@app/tmp"; public $outputPath = "@app/runtime/tmp-extensions";
public $license; public $license;
public $authorName; public $authorName;
public $authorEmail; public $authorEmail;
...@@ -55,13 +53,40 @@ class Generator extends \yii\gii\Generator ...@@ -55,13 +53,40 @@ class Generator extends \yii\gii\Generator
*/ */
public function rules() public function rules()
{ {
return array_merge(parent::rules(), [ return array_merge(
[['vendorName', 'packageName'], 'filter', 'filter' => 'trim'], parent::rules(),
[['vendorName', 'packageName', 'namespace', 'type', 'license', 'title','description', 'authorName','authorEmail'], 'required'], [
[['authorEmail'], 'email'], [['vendorName', 'packageName'], 'filter', 'filter' => 'trim'],
[['packageName'], 'match', 'pattern' => '/^[a-z0-9-\.]+$/', 'message' => 'Only lowercase word characters, dashes and dots are allowed.'], [
[['vendorName'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'], [
]); 'vendorName',
'packageName',
'namespace',
'type',
'license',
'title',
'description',
'authorName',
'authorEmail',
'outputPath'
],
'required'
],
[['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.'
],
]
);
} }
/** /**
...@@ -70,9 +95,9 @@ class Generator extends \yii\gii\Generator ...@@ -70,9 +95,9 @@ class Generator extends \yii\gii\Generator
public function attributeLabels() public function attributeLabels()
{ {
return [ return [
'vendorName' => 'Vendor Name', 'vendorName' => 'Vendor Name',
'packageName' => 'Package Name', 'packageName' => 'Package Name',
'license' => 'License', 'license' => 'License',
]; ];
} }
...@@ -82,11 +107,12 @@ class Generator extends \yii\gii\Generator ...@@ -82,11 +107,12 @@ class Generator extends \yii\gii\Generator
public function hints() public function hints()
{ {
return [ return [
'vendorName' => 'This refers to the name of the publisher, often i.e. your GitHub user name.', '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.', 'packageName' => 'This is the name of the extension on packagist, eg. <code>yii2-foobar</code>',
'namespace' => 'This will be added to your autoloader by composer. Do not use yii or yii2 in the namespace', '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.',
'outputPath' => 'The temporary location of the generated files.', 'keywords' => 'Comma separated keywords for this extension.',
'title' => 'A more descriptive name of your application for the README file.', 'outputPath' => 'The temporary location of the generated files.',
'title' => 'A more descriptive name of your application for the README file.',
'description' => 'A sentence or subline describing the main purpose of the extension.', 'description' => 'A sentence or subline describing the main purpose of the extension.',
]; ];
} }
...@@ -96,7 +122,7 @@ class Generator extends \yii\gii\Generator ...@@ -96,7 +122,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function stickyAttributes() public function stickyAttributes()
{ {
return ['vendorName','outputPath','authorName','authorEmail']; return ['vendorName', 'outputPath', 'authorName', 'authorEmail'];
} }
/** /**
...@@ -108,46 +134,47 @@ class Generator extends \yii\gii\Generator ...@@ -108,46 +134,47 @@ class Generator extends \yii\gii\Generator
# $link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']); # $link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']);
# return "The module has been generated successfully. You may $link."; # return "The module has been generated successfully. You may $link.";
#} #}
$outputPath = realpath(\Yii::getAlias($this->outputPath));
$output1 = <<<EOD $output1 = <<<EOD
<p>The extension has been generated successfully.</p> <p><em>The extension has been generated successfully.</em></p>
<p>To enable it in your application, you need to create a git repository <p>To enable it in your application, you need to create a git repository
and require via composer.</p> and require via composer.</p>
EOD; EOD;
$code1 = <<<EOD $code1 = <<<EOD
cd tmp/{$this->packageName} cd {$outputPath}/{$this->packageName}
git init git init
git add -A git add -A
git commit git commit
git remote add origin https://path.to/your/repo
git push -u origin master
EOD; EOD;
$output2 = <<<EOD $output2 = <<<EOD
<p>The next step is just for <em>local testing</em>, skip it if you directly publish the extension on e.g. packagist.org</p> <p>The next step is just for <em>local testing</em>, skip it if you directly publish the extension on e.g. packagist.org</p>
<p>Add the newly created repo to your composer.json.</p> <p>Add the newly created repo to your composer.json.</p>
EOD; EOD;
$code2 = <<<EOD $code2 = <<<EOD
"repositories":[ "repositories":[
{ {
"type": "git", "type": "git",
"url": "file://./tmp/{$this->packageName}" "url": "https://path.to/your/repo"
} }
] ]
EOD; EOD;
$output3 = <<<EOD $output3 = <<<EOD
<p>Note: Make sure to remove the above lines after testing.</p> <p class="well">Note: You may use the url <code>file://{$outputPath}/{$this->packageName}</code> for testing.</p>
<p>Require the package with composer</p> <p>Require the package with composer</p>
EOD; EOD;
$code3 = <<<EOD $code3 = <<<EOD
composer.phar require {$this->vendorName}/yii2-{$this->packageName}:* composer.phar require {$this->vendorName}/{$this->packageName}:dev-master
EOD; EOD;
$output4 = <<<EOD $output4 = <<<EOD
<p>And use it in your application.</p> <p>And use it in your application.</p>
EOD; EOD;
$code4 = <<<EOD $code4 = <<<EOD
\$x = new \\{$this->vendorName}\\{$this->packageName}\AutoloadExample::widget(); \\{$this->namespace}AutoloadExample::widget();
echo \$x->run();
EOD; EOD;
$return = $output1 . '<pre>' . highlight_string($code1, true) . '</pre>'; $return = $output1 . '<pre>' . highlight_string($code1, true) . '</pre>';
$return .= $output2 . '<pre>' . highlight_string($code2, true) . '</pre>'; $return .= $output2 . '<pre>' . highlight_string($code2, true) . '</pre>';
$return .= $output3 . '<pre>' . highlight_string($code3, true) . '</pre>'; $return .= $output3 . '<pre>' . highlight_string($code3, true) . '</pre>';
$return .= $output4 . '<pre>' . highlight_string($code4, true) . '</pre>'; $return .= $output4 . '<pre>' . highlight_string($code4, true) . '</pre>';
...@@ -190,7 +217,14 @@ EOD; ...@@ -190,7 +217,14 @@ EOD;
public function getOutputPath() public function getOutputPath()
{ {
return Yii::getAlias($this->outputPath); return Yii::getAlias($this->outputPath);
#return Yii::getAlias('@' . str_replace('\\', '/', substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\')))); }
/**
* @return a json encoded array with the given keywords
*/
public function getKeywordsArrayJson()
{
return json_encode(explode(',', $this->keywords));
} }
/** /**
......
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
* @var yii\gii\generators\module\Generator $generator * @var yii\gii\generators\module\Generator $generator
*/ */
?> ?>
<div class="alert alert-info">
Please read the
<?= \yii\helpers\Html::a('Extension Guidelines', 'https://github.com/yiisoft/yii2/blob/master/docs/guide/extensions.md', ['target'=>'new']) ?>
before creating an extension.
</div>
<div class="module-form"> <div class="module-form">
<?php <?php
echo $form->field($generator, 'vendorName'); echo $form->field($generator, 'vendorName');
...@@ -12,7 +17,7 @@ ...@@ -12,7 +17,7 @@
echo $form->field($generator, 'namespace'); echo $form->field($generator, 'namespace');
echo $form->field($generator, 'type')->dropDownList($generator->optsType()); echo $form->field($generator, 'type')->dropDownList($generator->optsType());
echo $form->field($generator, 'keywords'); echo $form->field($generator, 'keywords');
echo $form->field($generator, 'license')->dropDownList($generator->optsLicense()); echo $form->field($generator, 'license')->dropDownList($generator->optsLicense(), ['prompt'=>'Choose...']);
echo $form->field($generator, 'title'); echo $form->field($generator, 'title');
echo $form->field($generator, 'description'); echo $form->field($generator, 'description');
echo $form->field($generator, 'authorName'); echo $form->field($generator, 'authorName');
...@@ -20,11 +25,3 @@ ...@@ -20,11 +25,3 @@
echo $form->field($generator, 'outputPath'); echo $form->field($generator, 'outputPath');
?> ?>
</div> </div>
<?php
$js = <<< EOS
$('#generator-packagename').keyup(function(){
$('#generator-namespace').val($('#generator-vendorname').val()+'\\\'+$('#generator-packagename').val());
});
EOS;
$this->registerJs($js);
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
echo "<?php\n"; echo "<?php\n";
?> ?>
namespace <?= $generator->namespace ?>; namespace <?= substr($generator->namespace, 0, -1) ?>;
class AutoloadExample extends \yii\base\widget { class AutoloadExample extends \yii\base\widget {
function run() { function run() {
......
{ {
"name": "<?= $generator->vendorName ?>/yii2-<?= $generator->packageName ?>", "name": "<?= $generator->vendorName ?>/<?= $generator->packageName ?>",
"description": "<?= $generator->description ?>", "description": "<?= $generator->description ?>",
"type": "<?= $generator->type ?>", "type": "<?= $generator->type ?>",
"keywords": ["TODO","ARRAY INPUT"], "keywords": <?= $generator->keywordsArrayJson ?>,
"license": "<?= $generator->license ?>", "license": "<?= $generator->license ?>",
"authors": [ "authors": [
{ {
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
], ],
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"<?= $generator->namespace ?>": "" "<?= str_replace('\\','\\\\',$generator->namespace) ?>": ""
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment