You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.9 KiB
166 lines
3.9 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\gii\generators\module;
|
||
|
|
||
11 years ago
|
use yii\gii\CodeFile;
|
||
|
use yii\helpers\Html;
|
||
11 years ago
|
use Yii;
|
||
|
use yii\helpers\StringHelper;
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* This generator will generate the skeleton code needed by a module.
|
||
11 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Generator extends \yii\gii\Generator
|
||
|
{
|
||
11 years ago
|
public $moduleClass;
|
||
|
public $moduleID;
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function getName()
|
||
|
{
|
||
|
return 'Module Generator';
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function getDescription()
|
||
|
{
|
||
|
return 'This generator helps you to generate the skeleton code needed by a Yii module.';
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function rules()
|
||
|
{
|
||
11 years ago
|
return array_merge(parent::rules(), [
|
||
11 years ago
|
[['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'],
|
||
11 years ago
|
]);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function attributeLabels()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
'moduleID' => 'Module ID',
|
||
|
'moduleClass' => 'Module Class',
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function hints()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
'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>.',
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function successMessage()
|
||
|
{
|
||
|
if (Yii::$app->hasModule($this->moduleID)) {
|
||
11 years ago
|
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($this->moduleID), ['target' => '_blank']);
|
||
11 years ago
|
return "The module has been generated successfully. You may $link.";
|
||
|
}
|
||
|
|
||
|
$output = <<<EOD
|
||
|
<p>The module has been generated successfully.</p>
|
||
11 years ago
|
<p>To access the module, you need to add this to your application configuration:</p>
|
||
11 years ago
|
EOD;
|
||
|
$code = <<<EOD
|
||
|
<?php
|
||
11 years ago
|
......
|
||
11 years ago
|
'modules' => [
|
||
|
'{$this->moduleID}' => [
|
||
11 years ago
|
'class' => '{$this->moduleClass}',
|
||
11 years ago
|
],
|
||
|
],
|
||
11 years ago
|
......
|
||
11 years ago
|
EOD;
|
||
|
|
||
|
return $output . '<pre>' . highlight_string($code, true) . '</pre>';
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function requiredTemplates()
|
||
|
{
|
||
11 years ago
|
return ['module.php', 'controller.php', 'view.php'];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function generate()
|
||
|
{
|
||
11 years ago
|
$files = [];
|
||
11 years ago
|
$modulePath = $this->getModulePath();
|
||
|
$files[] = new CodeFile(
|
||
11 years ago
|
$modulePath . '/' . StringHelper::basename($this->moduleClass) . '.php',
|
||
11 years ago
|
$this->render("module.php")
|
||
11 years ago
|
);
|
||
|
$files[] = new CodeFile(
|
||
|
$modulePath . '/controllers/DefaultController.php',
|
||
11 years ago
|
$this->render("controller.php")
|
||
11 years ago
|
);
|
||
|
$files[] = new CodeFile(
|
||
|
$modulePath . '/views/default/index.php',
|
||
11 years ago
|
$this->render("view.php")
|
||
11 years ago
|
);
|
||
|
|
||
|
return $files;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Validates [[moduleClass]] to make sure it is a fully qualified class name.
|
||
|
*/
|
||
|
public function validateModuleClass()
|
||
|
{
|
||
11 years ago
|
if (strpos($this->moduleClass, '\\') === false || Yii::getAlias('@' . str_replace('\\', '/', $this->moduleClass), false) === false) {
|
||
11 years ago
|
$this->addError('moduleClass', 'Module class must be properly namespaced.');
|
||
|
}
|
||
11 years ago
|
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".');
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @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';
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|