Commit 94e4c076 by Qiang Xue

gii WIP

parent a0a900fd
<?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',
);
}
<?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',
),
);
}
}
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);
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;
}
<?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");
}
}
}
<?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';
}
}
<?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>
<?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.';
}
}
<?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.';
}
}
<?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.';
}
}
<?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.';
}
}
<?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>
<?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>
<?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(); ?>
<?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(); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment