summaryrefslogtreecommitdiff
path: root/framework/gii/generators/controller
diff options
context:
space:
mode:
Diffstat (limited to 'framework/gii/generators/controller')
-rw-r--r--framework/gii/generators/controller/ControllerCode.php130
-rw-r--r--framework/gii/generators/controller/ControllerGenerator.php6
-rw-r--r--framework/gii/generators/controller/templates/default/controller.php45
-rw-r--r--framework/gii/generators/controller/templates/default/view.php33
-rw-r--r--framework/gii/generators/controller/views/index.php44
5 files changed, 258 insertions, 0 deletions
diff --git a/framework/gii/generators/controller/ControllerCode.php b/framework/gii/generators/controller/ControllerCode.php
new file mode 100644
index 0000000..6cdc9fd
--- /dev/null
+++ b/framework/gii/generators/controller/ControllerCode.php
@@ -0,0 +1,130 @@
+<?php
+
+class ControllerCode extends CCodeModel
+{
+ public $controller;
+ public $baseClass='Controller';
+ public $actions='index';
+
+ 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+[\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.'),
+ 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';
+ }
+} \ No newline at end of file
diff --git a/framework/gii/generators/controller/ControllerGenerator.php b/framework/gii/generators/controller/ControllerGenerator.php
new file mode 100644
index 0000000..91ffaaa
--- /dev/null
+++ b/framework/gii/generators/controller/ControllerGenerator.php
@@ -0,0 +1,6 @@
+<?php
+
+class ControllerGenerator extends CCodeGenerator
+{
+ public $codeModel='gii.generators.controller.ControllerCode';
+} \ No newline at end of file
diff --git a/framework/gii/generators/controller/templates/default/controller.php b/framework/gii/generators/controller/templates/default/controller.php
new file mode 100644
index 0000000..ba0b627
--- /dev/null
+++ b/framework/gii/generators/controller/templates/default/controller.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * This is the template for generating a controller class file.
+ * The following variables are available in this template:
+ * - $this: the ControllerCode object
+ */
+?>
+<?php echo "<?php\n"; ?>
+
+class <?php echo $this->controllerClass; ?> extends <?php echo $this->baseClass."\n"; ?>
+{
+<?php foreach($this->getActionIDs() as $action): ?>
+ public function action<?php echo ucfirst($action); ?>()
+ {
+ $this->render('<?php echo $action; ?>');
+ }
+
+<?php endforeach; ?>
+ // Uncomment the following methods and override them if needed
+ /*
+ public function filters()
+ {
+ // return the filter configuration for this controller, e.g.:
+ return array(
+ 'inlineFilterName',
+ array(
+ 'class'=>'path.to.FilterClass',
+ 'propertyName'=>'propertyValue',
+ ),
+ );
+ }
+
+ public function actions()
+ {
+ // return external action classes, e.g.:
+ return array(
+ 'action1'=>'path.to.ActionClass',
+ 'action2'=>array(
+ 'class'=>'path.to.AnotherActionClass',
+ 'propertyName'=>'propertyValue',
+ ),
+ );
+ }
+ */
+} \ No newline at end of file
diff --git a/framework/gii/generators/controller/templates/default/view.php b/framework/gii/generators/controller/templates/default/view.php
new file mode 100644
index 0000000..26fb36a
--- /dev/null
+++ b/framework/gii/generators/controller/templates/default/view.php
@@ -0,0 +1,33 @@
+<?php
+/**
+ * This is the template for generating an action view file.
+ * The following variables are available in this template:
+ * - $this: the ControllerCode object
+ * - $action: the action ID
+ */
+?>
+<?php
+echo "<?php\n";
+$label=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', basename($this->getControllerID()))))));
+if($action==='index')
+{
+ echo "\$this->breadcrumbs=array(
+ '$label',
+);";
+}
+else
+{
+ $action=ucfirst($action);
+ echo "\$this->breadcrumbs=array(
+ '$label'=>array('/{$this->uniqueControllerID}'),
+ '$action',
+);";
+}
+?>
+?>
+<h1><?php echo '<?php'; ?> echo $this->id . '/' . $this->action->id; ?></h1>
+
+<p>
+ You may change the content of this page by modifying
+ the file <tt><?php echo '<?php'; ?> echo __FILE__; ?></tt>.
+</p>
diff --git a/framework/gii/generators/controller/views/index.php b/framework/gii/generators/controller/views/index.php
new file mode 100644
index 0000000..6224e7e
--- /dev/null
+++ b/framework/gii/generators/controller/views/index.php
@@ -0,0 +1,44 @@
+<h1>Controller Generator</h1>
+
+<p>This generator helps you to quickly generate a new controller class,
+one or several controller actions and their corresponding views.</p>
+
+<?php $form=$this->beginWidget('CCodeForm', array('model'=>$model)); ?>
+
+ <div class="row">
+ <?php echo $form->labelEx($model,'controller'); ?>
+ <?php echo $form->textField($model,'controller',array('size'=>65)); ?>
+ <div class="tooltip">
+ Controller ID is case-sensitive. Below are some examples:
+ <ul>
+ <li><code>post</code> generates <code>PostController.php</code></li>
+ <li><code>postTag</code> generates <code>PostTagController.php</code></li>
+ <li><code>admin/user</code> generates <code>admin/UserController.php</code>.
+ If the application has an <code>admin</code> module enabled,
+ it will generate <code>UserController</code> within the module instead.
+ </li>
+ </ul>
+ </div>
+ <?php echo $form->error($model,'controller'); ?>
+ </div>
+
+ <div class="row sticky">
+ <?php echo $form->labelEx($model,'baseClass'); ?>
+ <?php echo $form->textField($model,'baseClass',array('size'=>65)); ?>
+ <div class="tooltip">
+ This is the class that the new controller class will extend from.
+ Please make sure the class exists and can be autoloaded.
+ </div>
+ <?php echo $form->error($model,'baseClass'); ?>
+ </div>
+
+ <div class="row">
+ <?php echo $form->labelEx($model,'actions'); ?>
+ <?php echo $form->textField($model,'actions',array('size'=>65)); ?>
+ <div class="tooltip">
+ Action IDs are case-insensitive. Separate multiple action IDs with commas or spaces.
+ </div>
+ <?php echo $form->error($model,'actions'); ?>
+ </div>
+
+<?php $this->endWidget(); ?>