1
0

Added new (clean) yii boilerplate

This commit is contained in:
2014-05-13 12:40:42 +02:00
parent 1d6d975a16
commit 99d29b432b
1983 changed files with 653465 additions and 17 deletions

View File

@@ -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 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';
}
}

View File

@@ -0,0 +1,6 @@
<?php
class ControllerGenerator extends CCodeGenerator
{
public $codeModel='gii.generators.controller.ControllerCode';
}

View File

@@ -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',
),
);
}
*/
}

View File

@@ -0,0 +1,36 @@
<?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"; ?>
/* @var $this <?php echo $this->getControllerClass(); ?> */
<?php
$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>

View File

@@ -0,0 +1,45 @@
<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.
Make sure to write module name in the correct case if it has a camelCase name.
</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(); ?>