Generator.php 3.89 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 21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Generator extends \yii\gii\Generator
{
23 24 25 26
	public $moduleClass;
	public $moduleID;

	/**
Qiang Xue committed
27
	 * @inheritdoc
28
	 */
Qiang Xue committed
29 30 31 32 33
	public function getName()
	{
		return 'Module Generator';
	}

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

42
	/**
Qiang Xue committed
43
	 * @inheritdoc
44 45 46
	 */
	public function rules()
	{
Alexander Makarov committed
47
		return array_merge(parent::rules(), [
48 49 50 51 52
			[['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
53
		]);
54 55 56
	}

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

	/**
Qiang Xue committed
68
	 * @inheritdoc
69 70 71
	 */
	public function hints()
	{
Alexander Makarov committed
72
		return [
73 74
			'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
75
		];
76 77 78
	}

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

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

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

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

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

		return $files;
	}

	/**
	 * Validates [[moduleClass]] to make sure it is a fully qualified class name.
	 */
	public function validateModuleClass()
	{
Qiang Xue committed
142
		if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass), false) === false) {
143 144
			$this->addError('moduleClass', 'Module class must be properly namespaced.');
		}
145 146 147
		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".');
		}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	}

	/**
	 * @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';
164
	}
Qiang Xue committed
165
}