Qiang Xue
11 years ago
17 changed files with 1117 additions and 0 deletions
@ -0,0 +1,428 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii; |
||||
|
||||
use Yii; |
||||
use yii\base\Model; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends Model |
||||
{ |
||||
/** |
||||
* @var string |
||||
*/ |
||||
public $id; |
||||
|
||||
/** |
||||
* @return string name of the code generator |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return 'unknown'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return ''; |
||||
} |
||||
|
||||
public function getUrl() |
||||
{ |
||||
return Yii::$app->controller->createUrl('default/view', array('id' => $this->id)); |
||||
} |
||||
|
||||
public function renderForm() |
||||
{ |
||||
return ''; |
||||
} |
||||
|
||||
public function renderFileList() |
||||
{ |
||||
return ''; |
||||
} |
||||
|
||||
const STATUS_NEW = 1; |
||||
const STATUS_PREVIEW = 2; |
||||
const STATUS_SUCCESS = 3; |
||||
const STATUS_ERROR = 4; |
||||
|
||||
static $keywords = array( |
||||
'__class__', |
||||
'__dir__', |
||||
'__file__', |
||||
'__function__', |
||||
'__line__', |
||||
'__method__', |
||||
'__namespace__', |
||||
'abstract', |
||||
'and', |
||||
'array', |
||||
'as', |
||||
'break', |
||||
'case', |
||||
'catch', |
||||
'cfunction', |
||||
'class', |
||||
'clone', |
||||
'const', |
||||
'continue', |
||||
'declare', |
||||
'default', |
||||
'die', |
||||
'do', |
||||
'echo', |
||||
'else', |
||||
'elseif', |
||||
'empty', |
||||
'enddeclare', |
||||
'endfor', |
||||
'endforeach', |
||||
'endif', |
||||
'endswitch', |
||||
'endwhile', |
||||
'eval', |
||||
'exception', |
||||
'exit', |
||||
'extends', |
||||
'final', |
||||
'final', |
||||
'for', |
||||
'foreach', |
||||
'function', |
||||
'global', |
||||
'goto', |
||||
'if', |
||||
'implements', |
||||
'include', |
||||
'include_once', |
||||
'instanceof', |
||||
'interface', |
||||
'isset', |
||||
'list', |
||||
'namespace', |
||||
'new', |
||||
'old_function', |
||||
'or', |
||||
'parent', |
||||
'php_user_filter', |
||||
'print', |
||||
'private', |
||||
'protected', |
||||
'public', |
||||
'require', |
||||
'require_once', |
||||
'return', |
||||
'static', |
||||
'switch', |
||||
'this', |
||||
'throw', |
||||
'try', |
||||
'unset', |
||||
'use', |
||||
'var', |
||||
'while', |
||||
'xor', |
||||
); |
||||
|
||||
/** |
||||
* @var array user confirmations on whether to overwrite existing code files with the newly generated ones. |
||||
* The value of this property is internally managed by this class and {@link CCodeGenerator}. |
||||
*/ |
||||
public $answers; |
||||
/** |
||||
* @var string the name of the code template that the user has selected. |
||||
* The value of this property is internally managed by this class and {@link CCodeGenerator}. |
||||
*/ |
||||
public $template; |
||||
/** |
||||
* @var array a list of {@link CCodeFile} objects that represent the code files to be generated. |
||||
* The {@link prepare()} method is responsible to populate this property. |
||||
*/ |
||||
public $files = array(); |
||||
/** |
||||
* @var integer the status of this model. T |
||||
* The value of this property is internally managed by {@link CCodeGenerator}. |
||||
*/ |
||||
public $status = self::STATUS_NEW; |
||||
|
||||
private $_stickyAttributes = array(); |
||||
|
||||
/** |
||||
* Prepares the code files to be generated. |
||||
* This is the main method that child classes should implement. It should contain the logic |
||||
* that populates the {@link files} property with a list of code files to be generated. |
||||
*/ |
||||
public function prepare() |
||||
{ |
||||
|
||||
} |
||||
|
||||
/** |
||||
* Declares the model validation rules. |
||||
* Child classes must override this method in the following format: |
||||
* <pre> |
||||
* return array_merge(parent::rules(), array( |
||||
* ...rules for the child class... |
||||
* )); |
||||
* </pre> |
||||
* @return array validation rules |
||||
*/ |
||||
public function rules() |
||||
{ |
||||
return array( |
||||
array('template', 'required'), |
||||
array('template', 'validateTemplate', 'skipOnError' => true), |
||||
array('template', 'sticky'), |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Validates the template selection. |
||||
* This method validates whether the user selects an existing template |
||||
* and the template contains all required template files as specified in {@link requiredTemplates}. |
||||
* @param string $attribute the attribute to be validated |
||||
* @param array $params validation parameters |
||||
*/ |
||||
public function validateTemplate($attribute, $params) |
||||
{ |
||||
$templates = $this->templates; |
||||
if (!isset($templates[$this->template])) { |
||||
$this->addError('template', 'Invalid template selection.'); |
||||
} else { |
||||
$templatePath = $this->templatePath; |
||||
foreach ($this->requiredTemplates() as $template) { |
||||
if (!is_file($templatePath . '/' . $template)) { |
||||
$this->addError('template', "Unable to find the required code template file '$template'."); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Checks if the named class exists (in a case sensitive manner). |
||||
* @param string $name class name to be checked |
||||
* @return boolean whether the class exists |
||||
*/ |
||||
public function classExists($name) |
||||
{ |
||||
return class_exists($name, false) && in_array($name, get_declared_classes()); |
||||
} |
||||
|
||||
/** |
||||
* Declares the model attribute labels. |
||||
* Child classes must override this method in the following format: |
||||
* <pre> |
||||
* return array_merge(parent::attributeLabels(), array( |
||||
* ...labels for the child class attributes... |
||||
* )); |
||||
* </pre> |
||||
* @return array the attribute labels |
||||
*/ |
||||
public function attributeLabels() |
||||
{ |
||||
return array( |
||||
'template' => 'Code Template', |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Returns a list of code templates that are required. |
||||
* Derived classes usually should override this method. |
||||
* @return array list of code templates that are required. They should be file paths |
||||
* relative to {@link templatePath}. |
||||
*/ |
||||
public function requiredTemplates() |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
/** |
||||
* Saves the generated code into files. |
||||
*/ |
||||
public function save() |
||||
{ |
||||
$result = true; |
||||
foreach ($this->files as $file) { |
||||
if ($this->confirmed($file)) { |
||||
$result = $file->save() && $result; |
||||
} |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* Returns the message to be displayed when the newly generated code is saved successfully. |
||||
* Child classes should override this method if the message needs to be customized. |
||||
* @return string the message to be displayed when the newly generated code is saved successfully. |
||||
*/ |
||||
public function successMessage() |
||||
{ |
||||
return 'The code has been generated successfully.'; |
||||
} |
||||
|
||||
/** |
||||
* Returns the message to be displayed when some error occurred during code file saving. |
||||
* Child classes should override this method if the message needs to be customized. |
||||
* @return string the message to be displayed when some error occurred during code file saving. |
||||
*/ |
||||
public function errorMessage() |
||||
{ |
||||
return 'There was some error when generating the code. Please check the following messages.'; |
||||
} |
||||
|
||||
/** |
||||
* Returns a list of available code templates (name=>directory). |
||||
* This method simply returns the {@link CCodeGenerator::templates} property value. |
||||
* @return array a list of available code templates (name=>directory). |
||||
*/ |
||||
public function getTemplates() |
||||
{ |
||||
return Yii::app()->controller->templates; |
||||
} |
||||
|
||||
/** |
||||
* @return string the directory that contains the template files. |
||||
* @throws CHttpException if {@link templates} is empty or template selection is invalid |
||||
*/ |
||||
public function getTemplatePath() |
||||
{ |
||||
$templates = $this->getTemplates(); |
||||
if (isset($templates[$this->template])) { |
||||
return $templates[$this->template]; |
||||
} elseif (empty($templates)) { |
||||
throw new CHttpException(500, 'No templates are available.'); |
||||
} else { |
||||
throw new CHttpException(500, 'Invalid template selection.'); |
||||
} |
||||
|
||||
} |
||||
|
||||
/** |
||||
* @param CCodeFile $file whether the code file should be saved |
||||
* @return bool whether the confirmation is found in {@link answers} with appropriate {@link operation} |
||||
*/ |
||||
public function confirmed($file) |
||||
{ |
||||
return $this->answers === null && $file->operation === CCodeFile::OP_NEW |
||||
|| is_array($this->answers) && isset($this->answers[md5($file->path)]); |
||||
} |
||||
|
||||
/** |
||||
* Generates the code using the specified code template file. |
||||
* This method is manly used in {@link generate} to generate code. |
||||
* @param string $templateFile the code template file path |
||||
* @param array $_params_ a set of parameters to be extracted and made available in the code template |
||||
* @throws CException is template file does not exist |
||||
* @return string the generated code |
||||
*/ |
||||
public function render($templateFile, $_params_ = null) |
||||
{ |
||||
if (!is_file($templateFile)) { |
||||
throw new CException("The template file '$templateFile' does not exist."); |
||||
} |
||||
|
||||
if (is_array($_params_)) { |
||||
extract($_params_, EXTR_PREFIX_SAME, 'params'); |
||||
} else { |
||||
$params = $_params_; |
||||
} |
||||
ob_start(); |
||||
ob_implicit_flush(false); |
||||
require($templateFile); |
||||
return ob_get_clean(); |
||||
} |
||||
|
||||
/** |
||||
* @return string the code generation result log. |
||||
*/ |
||||
public function renderResults() |
||||
{ |
||||
$output = 'Generating code using template "' . $this->templatePath . "\"...\n"; |
||||
foreach ($this->files as $file) { |
||||
if ($file->error !== null) { |
||||
$output .= "<span class=\"error\">generating {$file->relativePath}<br/> {$file->error}</span>\n"; |
||||
} elseif ($file->operation === CCodeFile::OP_NEW && $this->confirmed($file)) { |
||||
$output .= ' generated ' . $file->relativePath . "\n"; |
||||
} elseif ($file->operation === CCodeFile::OP_OVERWRITE && $this->confirmed($file)) { |
||||
$output .= ' overwrote ' . $file->relativePath . "\n"; |
||||
} else { |
||||
$output .= ' skipped ' . $file->relativePath . "\n"; |
||||
} |
||||
} |
||||
$output .= "done!\n"; |
||||
return $output; |
||||
} |
||||
|
||||
/** |
||||
* The "sticky" validator. |
||||
* This validator does not really validate the attributes. |
||||
* It actually saves the attribute value in a file to make it sticky. |
||||
* @param string $attribute the attribute to be validated |
||||
* @param array $params the validation parameters |
||||
*/ |
||||
public function sticky($attribute, $params) |
||||
{ |
||||
if (!$this->hasErrors()) { |
||||
$this->_stickyAttributes[$attribute] = $this->$attribute; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Loads sticky attributes from a file and populates them into the model. |
||||
*/ |
||||
public function loadStickyAttributes() |
||||
{ |
||||
$this->_stickyAttributes = array(); |
||||
$path = $this->getStickyFile(); |
||||
if (is_file($path)) { |
||||
$result = @include($path); |
||||
if (is_array($result)) { |
||||
$this->_stickyAttributes = $result; |
||||
foreach ($this->_stickyAttributes as $name => $value) { |
||||
if (property_exists($this, $name) || $this->canSetProperty($name)) { |
||||
$this->$name = $value; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Saves sticky attributes into a file. |
||||
*/ |
||||
public function saveStickyAttributes() |
||||
{ |
||||
$path = $this->getStickyFile(); |
||||
@mkdir(dirname($path), 0755, true); |
||||
file_put_contents($path, "<?php\nreturn " . var_export($this->_stickyAttributes, true) . ";\n");
|
||||
} |
||||
|
||||
/** |
||||
* @return string the file path that stores the sticky attribute values. |
||||
*/ |
||||
public function getStickyFile() |
||||
{ |
||||
return Yii::app()->runtimePath . '/gii-' . Yii::getVersion() . '/' . get_class($this) . '.php'; |
||||
} |
||||
|
||||
/** |
||||
* Validates an attribute to make sure it is not taking a PHP reserved keyword. |
||||
* @param string $attribute the attribute to be validated |
||||
* @param array $params validation parameters |
||||
*/ |
||||
public function validateReservedWord($attribute, $params) |
||||
{ |
||||
$value = $this->$attribute; |
||||
if (in_array(strtolower($value), self::$keywords)) { |
||||
$this->addError($attribute, $this->getAttributeLabel($attribute) . ' cannot take a reserved PHP keyword.'); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii; |
||||
|
||||
use yii\web\AssetBundle; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class GiiAsset extends AssetBundle |
||||
{ |
||||
public $sourcePath = '@yii/gii/assets'; |
||||
public $css = array( |
||||
'main.css', |
||||
); |
||||
public $js = array( |
||||
'gii.js', |
||||
); |
||||
public $depends = array( |
||||
'yii\web\YiiAsset', |
||||
'yii\bootstrap\BootstrapAsset', |
||||
'yii\bootstrap\BootstrapPluginAsset', |
||||
); |
||||
} |
@ -0,0 +1,101 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii; |
||||
|
||||
use Yii; |
||||
use yii\web\HttpException; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Module extends \yii\base\Module |
||||
{ |
||||
public $controllerNamespace = 'yii\gii\controllers'; |
||||
/** |
||||
* @var array the list of IPs that are allowed to access this module. |
||||
* Each array element represents a single IP filter which can be either an IP address |
||||
* or an address with wildcard (e.g. 192.168.0.*) to represent a network segment. |
||||
* The default value is `array('127.0.0.1', '::1')`, which means the module can only be accessed |
||||
* by localhost. |
||||
*/ |
||||
public $allowedIPs = array('127.0.0.1', '::1'); |
||||
/** |
||||
* @var array a list of path aliases that refer to the directories containing code generators. |
||||
* The directory referred by a single path alias may contain multiple code generators, each stored |
||||
* under a sub-directory whose name is the generator name. |
||||
*/ |
||||
public $generators = array(); |
||||
/** |
||||
* @var integer the permission to be set for newly generated code files. |
||||
* This value will be used by PHP chmod function. |
||||
* Defaults to 0666, meaning the file is read-writable by all users. |
||||
*/ |
||||
public $newFileMode = 0666; |
||||
/** |
||||
* @var integer the permission to be set for newly generated directories. |
||||
* This value will be used by PHP chmod function. |
||||
* Defaults to 0777, meaning the directory can be read, written and executed by all users. |
||||
*/ |
||||
public $newDirMode = 0777; |
||||
public $enabled = true; |
||||
|
||||
|
||||
/** |
||||
* Initializes the gii module. |
||||
*/ |
||||
public function init() |
||||
{ |
||||
parent::init(); |
||||
foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) { |
||||
$config['id'] = $id; |
||||
$this->generators[$id] = Yii::createObject($config); |
||||
} |
||||
} |
||||
|
||||
public function beforeAction($action) |
||||
{ |
||||
if ($this->checkAccess()) { |
||||
return parent::beforeAction($action); |
||||
} else { |
||||
throw new HttpException(403, 'You are not allowed to access this page.'); |
||||
} |
||||
} |
||||
|
||||
protected function checkAccess() |
||||
{ |
||||
$ip = Yii::$app->getRequest()->getUserIP(); |
||||
foreach ($this->allowedIPs as $filter) { |
||||
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
protected function coreGenerators() |
||||
{ |
||||
return array( |
||||
'model' => array( |
||||
'class' => 'yii\gii\generators\model\Generator', |
||||
), |
||||
'crud' => array( |
||||
'class' => 'yii\gii\generators\crud\Generator', |
||||
), |
||||
'controller' => array( |
||||
'class' => 'yii\gii\generators\controller\Generator', |
||||
), |
||||
'form' => array( |
||||
'class' => 'yii\gii\generators\form\Generator', |
||||
), |
||||
'module' => array( |
||||
'class' => 'yii\gii\generators\module\Generator', |
||||
), |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,15 @@
|
||||
yii.gii = (function ($) { |
||||
return { |
||||
init: function () { |
||||
$('.hint-block').each(function() { |
||||
var $hint = $(this); |
||||
$hint.parent().find('input,select,textarea').popover({ |
||||
html: true, |
||||
trigger: 'focus', |
||||
placement: 'right', |
||||
content: $hint.html() |
||||
}); |
||||
}); |
||||
} |
||||
}; |
||||
})(jQuery); |
After Width: | Height: | Size: 7.0 KiB |
@ -0,0 +1,43 @@
|
||||
body { |
||||
padding-top: 70px; |
||||
} |
||||
|
||||
.footer { |
||||
border-top: 1px solid #ddd; |
||||
margin-top: 30px; |
||||
padding-top: 15px; |
||||
padding-bottom: 30px; |
||||
} |
||||
|
||||
.jumbotron { |
||||
text-align: center; |
||||
background-color: transparent; |
||||
} |
||||
|
||||
.jumbotron .btn { |
||||
font-size: 21px; |
||||
padding: 14px 24px; |
||||
} |
||||
|
||||
.navbar-brand { |
||||
padding: 0 15px; |
||||
} |
||||
|
||||
.default-index .generator { |
||||
min-height: 200px; |
||||
margin-bottom: 20px; |
||||
} |
||||
|
||||
.list-group .glyphicon { |
||||
float: right; |
||||
margin-right: -15px; |
||||
} |
||||
|
||||
.popover { |
||||
max-width: 400px; |
||||
width: 400px; |
||||
} |
||||
|
||||
.hint-block { |
||||
display: none; |
||||
} |
@ -0,0 +1,54 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii\controllers; |
||||
|
||||
use yii\web\Controller; |
||||
use yii\web\HttpException; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class DefaultController extends Controller |
||||
{ |
||||
public $layout = 'generator'; |
||||
public $generator; |
||||
|
||||
public function actionIndex() |
||||
{ |
||||
$this->layout = 'main'; |
||||
return $this->render('index'); |
||||
} |
||||
|
||||
public function actionView($id) |
||||
{ |
||||
$generator = $this->loadGenerator($id); |
||||
return $this->render('view', array( |
||||
'generator' => $generator |
||||
)); |
||||
} |
||||
|
||||
public function actionCode($file) |
||||
{ |
||||
|
||||
} |
||||
|
||||
public function actionDiff($file1, $file2) |
||||
{ |
||||
|
||||
} |
||||
|
||||
protected function loadGenerator($id) |
||||
{ |
||||
if (isset($this->module->generators[$id])) { |
||||
return $this->generator = $this->module->generators[$id]; |
||||
} else { |
||||
throw new HttpException(404, "Code generator not found: $id"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,166 @@
|
||||
<?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; |
||||
|
||||
/** |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends \yii\gii\Generator |
||||
{ |
||||
public $controller; |
||||
public $baseClass = 'yii\web\Controller'; |
||||
public $actions = 'index'; |
||||
|
||||
public function getName() |
||||
{ |
||||
return 'Controller Generator'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return 'This generator helps you to quickly generate a new controller class, |
||||
one or several controller actions and their corresponding views.'; |
||||
} |
||||
|
||||
public function renderForm() |
||||
{ |
||||
return Yii::$app->getView()->renderFile(__DIR__ . '/views/form.php', array( |
||||
'model' => $this, |
||||
)); |
||||
} |
||||
|
||||
public function rules() |
||||
{ |
||||
return array_merge(parent::rules(), array( |
||||
array('controller, actions, baseClass', 'filter', 'filter' => 'trim'), |
||||
array('controller, baseClass', 'required'), |
||||
array('controller', 'match', 'pattern' => '/^[\w+\\/]*$/', 'message' => '{attribute} should only contain word characters and slashes.'), |
||||
array('actions', 'match', 'pattern' => '/^\w+[\w\s,]*$/', 'message' => '{attribute} should only contain word characters, spaces and commas.'), |
||||
array('baseClass', 'match', 'pattern' => '/^[a-zA-Z_][\w\\\\]*$/', 'message' => '{attribute} should only contain word characters and backslashes.'), |
||||
array('baseClass', 'validateReservedWord', 'skipOnError' => true), |
||||
array('baseClass, actions', 'sticky'), |
||||
)); |
||||
} |
||||
|
||||
public function attributeLabels() |
||||
{ |
||||
return array_merge(parent::attributeLabels(), array( |
||||
'baseClass' => 'Base Class', |
||||
'controller' => 'Controller ID', |
||||
'actions' => 'Action IDs', |
||||
)); |
||||
} |
||||
|
||||
public function requiredTemplates() |
||||
{ |
||||
return array( |
||||
'controller.php', |
||||
'view.php', |
||||
); |
||||
} |
||||
|
||||
public function successMessage() |
||||
{ |
||||
$link = CHtml::link('try it now', Yii::app()->createUrl($this->controller), array('target' => '_blank')); |
||||
return "The controller has been generated successfully. You may $link."; |
||||
} |
||||
|
||||
public function prepare() |
||||
{ |
||||
$this->files = array(); |
||||
$templatePath = $this->templatePath; |
||||
|
||||
$this->files[] = new CCodeFile( |
||||
$this->controllerFile, |
||||
$this->render($templatePath . '/controller.php') |
||||
); |
||||
|
||||
foreach ($this->getActionIDs() as $action) { |
||||
$this->files[] = new CCodeFile( |
||||
$this->getViewFile($action), |
||||
$this->render($templatePath . '/view.php', array('action' => $action)) |
||||
); |
||||
} |
||||
} |
||||
|
||||
public function getActionIDs() |
||||
{ |
||||
$actions = preg_split('/[\s,]+/', $this->actions, -1, PREG_SPLIT_NO_EMPTY); |
||||
$actions = array_unique($actions); |
||||
sort($actions); |
||||
return $actions; |
||||
} |
||||
|
||||
public function getControllerClass() |
||||
{ |
||||
if (($pos = strrpos($this->controller, '/')) !== false) { |
||||
return ucfirst(substr($this->controller, $pos + 1)) . 'Controller'; |
||||
} else { |
||||
return ucfirst($this->controller) . 'Controller'; |
||||
} |
||||
} |
||||
|
||||
public function getModule() |
||||
{ |
||||
if (($pos = strpos($this->controller, '/')) !== false) { |
||||
$id = substr($this->controller, 0, $pos); |
||||
if (($module = Yii::app()->getModule($id)) !== null) { |
||||
return $module; |
||||
} |
||||
} |
||||
return Yii::app(); |
||||
} |
||||
|
||||
public function getControllerID() |
||||
{ |
||||
if ($this->getModule() !== Yii::app()) { |
||||
$id = substr($this->controller, strpos($this->controller, '/') + 1); |
||||
} else { |
||||
$id = $this->controller; |
||||
} |
||||
if (($pos = strrpos($id, '/')) !== false) { |
||||
$id[$pos + 1] = strtolower($id[$pos + 1]); |
||||
} else { |
||||
$id[0] = strtolower($id[0]); |
||||
} |
||||
return $id; |
||||
} |
||||
|
||||
public function getUniqueControllerID() |
||||
{ |
||||
$id = $this->controller; |
||||
if (($pos = strrpos($id, '/')) !== false) { |
||||
$id[$pos + 1] = strtolower($id[$pos + 1]); |
||||
} else { |
||||
$id[0] = strtolower($id[0]); |
||||
} |
||||
return $id; |
||||
} |
||||
|
||||
public function getControllerFile() |
||||
{ |
||||
$module = $this->getModule(); |
||||
$id = $this->getControllerID(); |
||||
if (($pos = strrpos($id, '/')) !== false) { |
||||
$id[$pos + 1] = strtoupper($id[$pos + 1]); |
||||
} else { |
||||
$id[0] = strtoupper($id[0]); |
||||
} |
||||
return $module->getControllerPath() . '/' . $id . 'Controller.php'; |
||||
} |
||||
|
||||
public function getViewFile($action) |
||||
{ |
||||
$module = $this->getModule(); |
||||
return $module->getViewPath() . '/' . $this->getControllerID() . '/' . $action . '.php'; |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
use yii\helpers\Html; |
||||
use yii\widgets\ActiveForm; |
||||
|
||||
/** |
||||
* @var yii\base\View $this |
||||
* @var yii\widgets\ActiveForm $form |
||||
*/ |
||||
?> |
||||
<div class="controller-form"> |
||||
<div class="row"> |
||||
<div class="col-lg-6"> |
||||
<?php $form = ActiveForm::begin(array('id' => 'login-form')); ?> |
||||
<?php echo $form->field($model, 'controller')->hint(' |
||||
Controller ID is case-sensitive and can contain module ID(s). For example: |
||||
<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> |
||||
'); ?> |
||||
<?php echo $form->field($model, 'baseClass')->hint(' |
||||
This is the class that the new controller class will extend from. |
||||
Please make sure the class exists and can be autoloaded. |
||||
'); ?> |
||||
<?php echo $form->field($model, 'actions')->hint(' |
||||
Provide one or multiple action IDs to generate empty action method(s) in the controller. |
||||
Separate multiple action IDs with commas or spaces. |
||||
'); ?> |
||||
<div class="form-actions"> |
||||
<?php echo Html::submitButton('Preview', array('class' => 'btn btn-primary')); ?> |
||||
</div> |
||||
<?php ActiveForm::end(); ?> |
||||
</div> |
||||
</div> |
||||
</div> |
@ -0,0 +1,27 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii\generators\crud; |
||||
|
||||
/** |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends \yii\gii\Generator |
||||
{ |
||||
public function getName() |
||||
{ |
||||
return 'CRUD Generator'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return 'This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) |
||||
operations for the specified data model.'; |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii\generators\form; |
||||
|
||||
/** |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends \yii\gii\Generator |
||||
{ |
||||
public function getName() |
||||
{ |
||||
return 'Form Generator'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return 'This generator generates a view script file that displays a form to collect input for the specified model class.'; |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii\generators\model; |
||||
|
||||
/** |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends \yii\gii\Generator |
||||
{ |
||||
public function getName() |
||||
{ |
||||
return 'Model Generator'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return 'This generator generates a model class for the specified database table.'; |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\gii\generators\module; |
||||
|
||||
/** |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Generator extends \yii\gii\Generator |
||||
{ |
||||
public function getName() |
||||
{ |
||||
return 'Module Generator'; |
||||
} |
||||
|
||||
public function getDescription() |
||||
{ |
||||
return 'This generator helps you to generate the skeleton code needed by a Yii module.'; |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
<?php |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var $this \yii\base\View |
||||
* @var $content string |
||||
* @var yii\gii\Generator[] $generators |
||||
* @var yii\gii\Generator $activeGenerator |
||||
*/ |
||||
$generators = Yii::$app->controller->module->generators; |
||||
$activeGenerator = Yii::$app->controller->generator; |
||||
$this->title = 'Welcome to Gii'; |
||||
?> |
||||
<div class="default-index"> |
||||
<div class="page-header"> |
||||
<h1>Welcome to Gii <small>a magic tool that can build up an application for you</small></h1> |
||||
</div> |
||||
|
||||
<p class="lead">Start the fun with the following code generators:</p> |
||||
|
||||
<div class="row"> |
||||
<?php foreach (array_values($generators) as $i => $generator): ?> |
||||
<div class="generator col-lg-4"> |
||||
<h3><?php echo Html::encode($generator->getName()); ?></h3>
|
||||
<p><?php echo $generator->getDescription(); ?></p>
|
||||
<p><?php echo Html::a('Start »', $generator->getUrl(), array('class' => 'btn btn-default')); ?></p>
|
||||
</div> |
||||
<?php endforeach; ?> |
||||
</div> |
||||
|
||||
|
||||
<p><a class="btn btn-success" href="http://www.yiiframework.com/extensions/?tag=gii">Get More Generators</a></p> |
||||
|
||||
</div> |
@ -0,0 +1,19 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var $this \yii\base\View |
||||
* @var $generator \yii\gii\Generator |
||||
*/ |
||||
|
||||
$this->title = $generator->getName(); |
||||
?> |
||||
<div class="default-view"> |
||||
<h1><?php echo Html::encode($generator->getName()); ?></h1>
|
||||
<p><?php echo $generator->getDescription(); ?></p>
|
||||
|
||||
<?php echo $generator->renderForm(); ?> |
||||
|
||||
<?php echo $generator->renderFileList(); ?> |
||||
</div> |
@ -0,0 +1,31 @@
|
||||
<?php |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var $this \yii\base\View |
||||
* @var $content string |
||||
* @var yii\gii\Generator[] $generators |
||||
* @var yii\gii\Generator $activeGenerator |
||||
*/ |
||||
$generators = Yii::$app->controller->module->generators; |
||||
$activeGenerator = Yii::$app->controller->generator; |
||||
?> |
||||
<?php $this->beginContent('@yii/gii/views/layouts/main.php'); ?> |
||||
<div class="row"> |
||||
<div class="col-lg-3"> |
||||
<div class="list-group"> |
||||
<?php |
||||
foreach ($generators as $id => $generator) { |
||||
$label = '<i class="glyphicon glyphicon-chevron-right"></i>' . Html::encode($generator->getName()); |
||||
echo Html::a($label, array('default/view', 'id' => $id), array( |
||||
'class' => $generator === $activeGenerator ? 'list-group-item active' : 'list-group-item', |
||||
)); |
||||
} |
||||
?> |
||||
</div> |
||||
</div> |
||||
<div class="col-lg-9"> |
||||
<?php echo $content; ?> |
||||
</div> |
||||
</div> |
||||
<?php $this->endContent(); ?> |
@ -0,0 +1,55 @@
|
||||
<?php |
||||
use yii\bootstrap\NavBar; |
||||
use yii\bootstrap\Nav; |
||||
use yii\helpers\Html; |
||||
|
||||
/** |
||||
* @var $this \yii\base\View |
||||
* @var $content string |
||||
*/ |
||||
$asset = yii\gii\GiiAsset::register($this); |
||||
?> |
||||
<?php $this->beginPage(); ?> |
||||
<!DOCTYPE html> |
||||
<html lang="en"> |
||||
<head> |
||||
<meta charset="utf-8"/> |
||||
<title><?php echo Html::encode($this->title); ?></title>
|
||||
<?php $this->head(); ?> |
||||
</head> |
||||
<body> |
||||
<?php $this->beginBody(); ?> |
||||
<?php |
||||
NavBar::begin(array( |
||||
'brandLabel' => Html::img($asset->baseUrl . '/logo.png'), |
||||
'brandUrl' => array('default/index'), |
||||
'options' => array( |
||||
'class' => 'navbar-inverse navbar-fixed-top', |
||||
), |
||||
)); |
||||
echo Nav::widget(array( |
||||
'options' => array('class' => 'nav navbar-nav pull-right'), |
||||
'items' => array( |
||||
array('label' => 'Home', 'url' => array('default/index')), |
||||
array('label' => 'Help', 'url' => 'http://www.yiiframework.com/doc/guide/topics.gii'), |
||||
array('label' => 'Application', 'url' => Yii::$app->homeUrl), |
||||
), |
||||
)); |
||||
NavBar::end(); |
||||
?> |
||||
|
||||
<div class="container"> |
||||
<?php echo $content; ?> |
||||
</div> |
||||
|
||||
<footer class="footer"> |
||||
<div class="container"> |
||||
<p class="pull-left">A Product of <a href="http://www.yiisoft.com/">Yii Software LLC</a></p> |
||||
<p class="pull-right"><?php echo Yii::powered(); ?></p>
|
||||
</div> |
||||
</footer> |
||||
|
||||
<?php $this->endBody(); ?> |
||||
</body> |
||||
</html> |
||||
<?php $this->endPage(); ?> |
Loading…
Reference in new issue