Generator.php 3.45 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\crud;

Qiang Xue committed
10 11 12 13
use yii\db\ActiveRecord;
use yii\gii\CodeFile;
use yii\web\Controller;

Qiang Xue committed
14 15 16 17 18 19 20
/**
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Generator extends \yii\gii\Generator
{
Qiang Xue committed
21 22 23 24
	public $modelClass;
	public $controllerID;
	public $baseControllerClass = 'yii\web\Controller';

Qiang Xue committed
25 26 27 28 29 30 31 32 33 34
	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.';
	}
35

Qiang Xue committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	public function rules()
	{
		return array_merge(parent::rules(), array(
			array('modelClass, controllerID, baseControllerClass', 'filter', 'filter' => 'trim'),
			array('modelClass, controllerID, baseControllerClass', 'required'),
			array('modelClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
			array('modelClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())),
			array('controllerID', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'),
			array('baseControllerClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
			array('baseControllerClass', 'validateClass', 'params' => array('extends' => Controller::className())),
		));
	}

	public function attributeLabels()
	{
		return array_merge(parent::attributeLabels(), array(
			'modelClass' => 'Model Class',
			'controllerID' => 'Controller ID',
			'baseControllerClass' => 'Base Controller Class',
		));
	}

	/**
	 * @inheritdoc
	 */
	public function hints()
	{
		return array(
			'modelClass' => 'This is the ActiveRecord class associated with the table that CRUD will be built upon.
				You should provide a fully qualified class name, e.g., <code>app\models\Post</code>.',
			'controllerID' => 'CRUD controllers are often named after the model class name that they are dealing with.
				Controller ID should be in lower case and may contain module ID(s) separated by slashes. 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>',
			'baseControllerClass' => 'This is the class that the new CRUD controller class will extend from.
				You should provide a fully qualified class name, e.g., <code>yii\web\Controller</code>.',
		);
	}

	public function requiredTemplates()
	{
		return array(
			'controller.php',
		);
	}

	/**
	 * @inheritdoc
	 */
	public function stickyAttributes()
	{
		return array('baseControllerClass');
	}

93 94 95 96 97
	/**
	 * @inheritdoc
	 */
	public function generate()
	{
Qiang Xue committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		$files = array();
		$files[] = new CodeFile(
			$this->controllerFile,
			$this->render('controller.php')
		);

		$files = scandir($this->getTemplatePath());
		foreach ($files as $file) {
			if (is_file($templatePath . '/' . $file) && CFileHelper::getExtension($file) === 'php' && $file !== 'controller.php') {
				$files[] = new CodeFile(
					$this->viewPath . DIRECTORY_SEPARATOR . $file,
					$this->render($templatePath . '/' . $file)
				);
			}
		}

		return $files;
115
	}
Qiang Xue committed
116
}