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

namespace yii\gii\generators\module;

10 11
use yii\gii\CodeFile;
use yii\helpers\Html;
12 13
use Yii;
use yii\helpers\StringHelper;
14

Qiang Xue committed
15
/**
16
 * This generator will generate the skeleton code needed by a module.
Qiang Xue committed
17
 *
18 19 20
 * @property string $controllerNamespace The controller namespace of the module. This property is read-only.
 * @property boolean $modulePath The directory that contains the module class. This property is read-only.
 *
Qiang Xue committed
21 22 23 24 25
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Generator extends \yii\gii\Generator
{
26 27 28 29
	public $moduleClass;
	public $moduleID;

	/**
Qiang Xue committed
30
	 * @inheritdoc
31
	 */
Qiang Xue committed
32 33 34 35 36
	public function getName()
	{
		return 'Module Generator';
	}

37
	/**
Qiang Xue committed
38
	 * @inheritdoc
39
	 */
Qiang Xue committed
40 41 42 43
	public function getDescription()
	{
		return 'This generator helps you to generate the skeleton code needed by a Yii module.';
	}
44

45
	/**
Qiang Xue committed
46
	 * @inheritdoc
47 48 49
	 */
	public function rules()
	{
Alexander Makarov committed
50
		return array_merge(parent::rules(), [
51 52 53 54 55
			[['moduleID', 'moduleClass'], 'filter', 'filter' => 'trim'],
			[['moduleID', 'moduleClass'], 'required'],
			[['moduleID'], 'match', 'pattern' => '/^[\w\\-]+$/', 'message' => 'Only word characters and dashes are allowed.'],
			[['moduleClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
			[['moduleClass'], 'validateModuleClass'],
Alexander Makarov committed
56
		]);
57 58 59
	}

	/**
Qiang Xue committed
60
	 * @inheritdoc
61 62 63
	 */
	public function attributeLabels()
	{
Alexander Makarov committed
64
		return [
65 66
			'moduleID' => 'Module ID',
			'moduleClass' => 'Module Class',
Alexander Makarov committed
67
		];
68 69 70
	}

	/**
Qiang Xue committed
71
	 * @inheritdoc
72 73 74
	 */
	public function hints()
	{
Alexander Makarov committed
75
		return [
76 77
			'moduleID' => 'This refers to the ID of the module, e.g., <code>admin</code>.',
			'moduleClass' => 'This is the fully qualified class name of the module, e.g., <code>app\modules\admin\Module</code>.',
Alexander Makarov committed
78
		];
79 80 81
	}

	/**
Qiang Xue committed
82
	 * @inheritdoc
83 84 85 86
	 */
	public function successMessage()
	{
		if (Yii::$app->hasModule($this->moduleID)) {
Alexander Makarov committed
87
			$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']);
88 89 90 91 92
			return "The module has been generated successfully. You may $link.";
		}

		$output = <<<EOD
<p>The module has been generated successfully.</p>
93
<p>To access the module, you need to add this to your application configuration:</p>
94 95 96
EOD;
		$code = <<<EOD
<?php
97
	......
Alexander Makarov committed
98 99
	'modules' => [
		'{$this->moduleID}' => [
100
			'class' => '{$this->moduleClass}',
Alexander Makarov committed
101 102
		],
	],
103
	......
104 105 106 107 108 109
EOD;

		return $output . '<pre>' . highlight_string($code, true) . '</pre>';
	}

	/**
Qiang Xue committed
110
	 * @inheritdoc
111 112 113
	 */
	public function requiredTemplates()
	{
Alexander Makarov committed
114
		return ['module.php', 'controller.php', 'view.php'];
115 116
	}

117
	/**
Qiang Xue committed
118
	 * @inheritdoc
119 120 121
	 */
	public function generate()
	{
Alexander Makarov committed
122
		$files = [];
123 124
		$modulePath = $this->getModulePath();
		$files[] = new CodeFile(
125
			$modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php',
Qiang Xue committed
126
			$this->render("module.php")
127 128 129
		);
		$files[] = new CodeFile(
			$modulePath . '/controllers/DefaultController.php',
Qiang Xue committed
130
			$this->render("controller.php")
131 132 133
		);
		$files[] = new CodeFile(
			$modulePath . '/views/default/index.php',
Qiang Xue committed
134
			$this->render("view.php")
135 136 137 138 139 140 141 142 143 144
		);

		return $files;
	}

	/**
	 * Validates [[moduleClass]] to make sure it is a fully qualified class name.
	 */
	public function validateModuleClass()
	{
Qiang Xue committed
145
		if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass), false) === false) {
146 147
			$this->addError('moduleClass', 'Module class must be properly namespaced.');
		}
148 149 150
		if (substr($this->moduleClass, -1, 1) == '\\') {
			$this->addError('moduleClass', 'Module class name must not be empty. Please enter a fully qualified class name. e.g. "app\\modules\\admin\\Module".');
		}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	}

	/**
	 * @return boolean the directory that contains the module class
	 */
	public function getModulePath()
	{
		return Yii::getAlias('@' . str_replace('\\', '/', substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\'))));
	}

	/**
	 * @return string the controller namespace of the module.
	 */
	public function getControllerNamespace()
	{
		return substr($this->moduleClass, 0, strrpos($this->moduleClass, '\\')) . '\controllers';
167
	}
Qiang Xue committed
168
}