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.
237 lines
5.8 KiB
237 lines
5.8 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\controller;
|
||
|
|
||
|
use Yii;
|
||
11 years ago
|
use yii\gii\CodeFile;
|
||
|
use yii\helpers\Html;
|
||
11 years ago
|
use yii\helpers\Inflector;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* This generator will generate a controller and one or a few action view files.
|
||
11 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Generator extends \yii\gii\Generator
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* @var string the controller ID
|
||
|
*/
|
||
11 years ago
|
public $controller;
|
||
11 years ago
|
/**
|
||
|
* @var string the base class of the controller
|
||
|
*/
|
||
11 years ago
|
public $baseClass = 'yii\web\Controller';
|
||
11 years ago
|
/**
|
||
|
* @var string the namespace of the controller class
|
||
|
*/
|
||
11 years ago
|
public $ns;
|
||
11 years ago
|
/**
|
||
|
* @var string list of action IDs separated by commas or spaces
|
||
|
*/
|
||
11 years ago
|
public $actions = 'index';
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
$this->ns = \Yii::$app->controllerNamespace;
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function getName()
|
||
|
{
|
||
|
return 'Controller Generator';
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function getDescription()
|
||
|
{
|
||
|
return 'This generator helps you to quickly generate a new controller class,
|
||
|
one or several controller actions and their corresponding views.';
|
||
|
}
|
||
|
|
||
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
|
[['controller', 'actions', 'baseClass', 'ns'], 'filter', 'filter' => 'trim'],
|
||
|
[['controller', 'baseClass'], 'required'],
|
||
|
[['controller'], 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'],
|
||
|
[['actions'], 'match', 'pattern' => '/^[a-z\\-,\\s]*$/', 'message' => 'Only a-z, dashes (-), spaces and commas are allowed.'],
|
||
|
[['baseClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
|
||
|
[['ns'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
|
||
11 years ago
|
]);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function attributeLabels()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
'baseClass' => 'Base Class',
|
||
|
'controller' => 'Controller ID',
|
||
|
'actions' => 'Action IDs',
|
||
11 years ago
|
'ns' => 'Controller Namespace',
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function requiredTemplates()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
'controller.php',
|
||
|
'view.php',
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function stickyAttributes()
|
||
|
{
|
||
11 years ago
|
return ['ns', 'baseClass'];
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function hints()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
'controller' => 'Controller ID should be in lower case and may contain module ID(s) separated by slashes. For example:
|
||
11 years ago
|
<ul>
|
||
|
<li><code>order</code> generates <code>OrderController.php</code></li>
|
||
|
<li><code>order-item</code> generates <code>OrderItemController.php</code></li>
|
||
|
<li><code>admin/user</code> generates <code>UserController.php</code> within the <code>admin</code> module.</li>
|
||
|
</ul>',
|
||
11 years ago
|
'actions' => 'Provide one or multiple action IDs to generate empty action method(s) in the controller. Separate multiple action IDs with commas or spaces.
|
||
|
Action IDs should be in lower case. For example:
|
||
|
<ul>
|
||
|
<li><code>index</code> generates <code>actionIndex()</code></li>
|
||
|
<li><code>create-order</code> generates <code>actionCreateOrder()</code></li>
|
||
|
</ul>',
|
||
11 years ago
|
'ns' => 'This is the namespace that the new controller class will use.',
|
||
11 years ago
|
'baseClass' => 'This is the class that the new controller class will extend from. Please make sure the class exists and can be autoloaded.',
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
11 years ago
|
public function successMessage()
|
||
|
{
|
||
11 years ago
|
$actions = $this->getActionIDs();
|
||
|
if (in_array('index', $actions)) {
|
||
|
$route = $this->controller . '/index';
|
||
|
} else {
|
||
|
$route = $this->controller . '/' . reset($actions);
|
||
|
}
|
||
11 years ago
|
$link = Html::a('try it now', Yii::$app->getUrlManager()->createUrl($route), ['target' => '_blank']);
|
||
11 years ago
|
return "The controller has been generated successfully. You may $link.";
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function generate()
|
||
11 years ago
|
{
|
||
11 years ago
|
$files = [];
|
||
11 years ago
|
|
||
11 years ago
|
$files[] = new CodeFile(
|
||
|
$this->getControllerFile(),
|
||
11 years ago
|
$this->render('controller.php')
|
||
11 years ago
|
);
|
||
|
|
||
|
foreach ($this->getActionIDs() as $action) {
|
||
11 years ago
|
$files[] = new CodeFile(
|
||
11 years ago
|
$this->getViewFile($action),
|
||
11 years ago
|
$this->render('view.php', ['action' => $action])
|
||
11 years ago
|
);
|
||
|
}
|
||
11 years ago
|
|
||
|
return $files;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Normalizes [[actions]] into an array of action IDs.
|
||
|
* @return array an array of action IDs entered by the user
|
||
|
*/
|
||
11 years ago
|
public function getActionIDs()
|
||
|
{
|
||
11 years ago
|
$actions = array_unique(preg_split('/[\s,]+/', $this->actions, -1, PREG_SPLIT_NO_EMPTY));
|
||
11 years ago
|
sort($actions);
|
||
|
return $actions;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @return string the controller class name without the namespace part.
|
||
|
*/
|
||
11 years ago
|
public function getControllerClass()
|
||
|
{
|
||
11 years ago
|
return Inflector::id2camel($this->getControllerID()) . 'Controller';
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @return string the controller ID (without the module ID prefix)
|
||
|
*/
|
||
11 years ago
|
public function getControllerID()
|
||
|
{
|
||
11 years ago
|
if (($pos = strrpos($this->controller, '/')) !== false) {
|
||
11 years ago
|
return substr($this->controller, $pos + 1);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
return $this->controller;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @return \yii\base\Module the module that the new controller belongs to
|
||
|
*/
|
||
11 years ago
|
public function getModule()
|
||
|
{
|
||
11 years ago
|
if (($pos = strrpos($this->controller, '/')) !== false) {
|
||
11 years ago
|
$id = substr($this->controller, 0, $pos);
|
||
11 years ago
|
if (($module = Yii::$app->getModule($id)) !== null) {
|
||
11 years ago
|
return $module;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
return Yii::$app;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* @return string the controller class file path
|
||
|
*/
|
||
11 years ago
|
public function getControllerFile()
|
||
|
{
|
||
|
$module = $this->getModule();
|
||
11 years ago
|
return $module->getControllerPath() . '/' . $this->getControllerClass() . '.php';
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* @param string $action the action ID
|
||
|
* @return string the action view file path
|
||
|
*/
|
||
11 years ago
|
public function getViewFile($action)
|
||
|
{
|
||
11 years ago
|
$module = $this->getModule();
|
||
|
return $module->getViewPath() . '/' . $this->getControllerID() . '/' . $action . '.php';
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|