Generator.php 7.61 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 17 18 19
 * @property string $keywordsArrayJson A json encoded array with the given keywords. This property is
 * read-only.
 * @property boolean $outputPath The directory that contains the module class. This property is read-only.
 *
20
 * @author Tobias Munk <schmunk@usrbin.de>
21
 * @since 2.0
22 23 24
 */
class Generator extends \yii\gii\Generator
{
25 26 27 28 29 30 31 32 33 34 35
    public $vendorName;
    public $packageName = "yii2-";
    public $namespace;
    public $type = "yii2-extension";
    public $keywords = "yii2,extension";
    public $title;
    public $description;
    public $outputPath = "@app/runtime/tmp-extensions";
    public $license;
    public $authorName;
    public $authorEmail;
36

37

38 39 40 41 42 43 44
    /**
     * @inheritdoc
     */
    public function getName()
    {
        return 'Extension Generator';
    }
45

46 47 48 49 50 51 52
    /**
     * @inheritdoc
     */
    public function getDescription()
    {
        return 'This generator helps you to generate the files needed by a Yii extension.';
    }
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return array_merge(
            parent::rules(),
            [
                [['vendorName', 'packageName'], 'filter', 'filter' => 'trim'],
                [
                    [
                        'vendorName',
                        'packageName',
                        'namespace',
                        'type',
                        'license',
                        'title',
                        'description',
                        'authorName',
                        'authorEmail',
                        'outputPath'
                    ],
                    'required'
                ],
                [['keywords'], 'safe'],
                [['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.'
                ],
            ]
        );
    }
95

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

108 109 110 111 112 113 114 115
    /**
     * @inheritdoc
     */
    public function hints()
    {
        return [
            '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>.',
tsvetann committed
116
            '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.',
117 118 119 120 121 122
            '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.',
            'description' => 'A sentence or subline describing the main purpose of the extension.',
        ];
    }
123

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

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

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

181 182 183 184 185
        $return = $output1 . '<pre>' . highlight_string($code1, true) . '</pre>';
        $return .= $output2 . '<pre>' . highlight_string($code2, true) . '</pre>';
        $return .= $output3 . '<pre>' . highlight_string($code3, true) . '</pre>';
        $return .= $output4 . '<pre>' . highlight_string($code4, true) . '</pre>';
        $return .= $output5;
186

187 188
        return $return;
    }
189

190 191 192 193 194 195 196
    /**
     * @inheritdoc
     */
    public function requiredTemplates()
    {
        return ['composer.json', 'AutoloadExample.php', 'README.md'];
    }
197

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

218 219
        return $files;
    }
220

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

229 230 231 232 233
    /**
     * @return string a json encoded array with the given keywords
     */
    public function getKeywordsArrayJson()
    {
234
        return json_encode(explode(',', $this->keywords), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
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 265 266 267 268 269 270 271 272
    }

    /**
     * @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);
    }
273
}