CCodeModel
| 鍖 | system.gii |
|---|---|
| 缁ф壙 | abstract class CCodeModel » CFormModel » CModel » CComponent |
| 瀹炵幇 | IteratorAggregate, Traversable, ArrayAccess |
| 婧愯嚜 | 1.1.2 |
| 鐗堟湰 | $Id: CCodeModel.php 3547 2012-01-24 10:07:28Z mdomba $ |
| 婧愮爜 | framework/gii/CCodeModel.php |
姣忎竴涓唬鐮佺敓鎴愬櫒鑷冲皯闇瑕佹湁涓涓粠姝ょ被鎵╁睍鑰屾潵鐨勪唬鐮佹ā鍨嬬被銆 涓涓唬鐮佹ā鍨嬬殑浣滅敤鏄〃绀虹敤鎴锋彁渚涚殑鍙傛暟锛屽苟浣跨敤瀹冧滑鏉ョ敓鎴愯嚜 瀹氫箟鐨勪唬鐮併
娲剧敓绫婚渶瑕佸疄鐜prepare鏂规硶锛屾鏂规硶鐨勪富瑕佷换鍔℃槸 鍩轰簬鐢ㄦ埛鍙傛暟濉厖files灞炴с
files灞炴у皢琚嫢骞CCodeFile瀹炰緥缁勬垚鐨勯泦鍚堝~鍏咃紝 姣忎釜瀹炰緥琛ㄧず灏嗙敓鎴愮殑涓涓崟涓鐨勪唬鐮佹枃浠躲
CCodeModel瀹炵幇浜嗙矘杩炲睘鎬э紙sticky attributes锛夌殑鍔熻兘銆備竴涓矘杩 灞炴у彲浠ヨ蹇嗗畠鐨勬渶鍚庣殑鏈夋晥鍊硷紝鍗充娇鐢ㄦ埛宸茬粡鍏抽棴鐒跺悗鍐嶆墦寮娴忚鍣ㄣ 瑕佸畾涔変竴涓睘鎬т负绮樿繛灞炴э紝鍙渶瑕佺畝鍗曞湴灏嗗畠鍔犲叆鍒板悕涓衡渟ticky鈥 鐨勯獙璇佸櫒鐨勯獙璇佽鍒欏垪琛ㄤ腑銆
鍏叡灞炴
| 灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|---|
| answers | array | 鐢ㄦ埛瀵规槸鍚︿互鏂扮敓鎴愮殑浠g爜鏂囦欢瑕嗙洊鍘熸湁鐨勪唬鐮佹枃浠剁殑纭淇℃伅銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆 | CCodeModel |
| attributes | array | Returns all attribute values. | CModel |
| errors | array | Returns the errors for all attribute or a single attribute. | CModel |
| files | array | CCodeFile瀵硅薄鐨勫垪琛紝琛ㄧず灏嗚鐢熸垚鐨勪唬鐮佹枃浠躲 prepare()鏂规硶璐熻矗濉厖姝ゅ睘鎬с | CCodeModel |
| iterator | CMapIterator | Returns an iterator for traversing the attributes in the model. | CModel |
| keywords | CCodeModel | ||
| safeAttributeNames | array | Returns the attribute names that are safe to be massively assigned. | CModel |
| scenario | string | Returns the scenario that this model is used in. | CModel |
| status | integer | 姝ゆā鍨嬬殑鐘舵併 姝ゅ睘鎬у奸氳繃CCodeGenerator鍐呴儴绠$悊銆 | CCodeModel |
| stickyFile | string | 瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆 | CCodeModel |
| template | string | 鐢ㄦ埛閫夋嫨鐨勪唬鐮佹ā鏉跨殑鍚嶅瓧銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆 | CCodeModel |
| templatePath | string | 鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘 | CCodeModel |
| templates | array | 杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 | CCodeModel |
| validatorList | CList | Returns all the validators declared in the model. | CModel |
| validators | array | Returns the validators applicable to the current scenario. | CModel |
鍏叡鏂规硶
| 鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|
| __call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
| __construct() | 鏋勯犳柟娉曘 | CFormModel |
| __get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
| __isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
| __set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
| __unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
| addError() | Adds a new error to the specified attribute. | CModel |
| addErrors() | Adds a list of errors. | CModel |
| asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
| attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
| attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
| attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
| attributeLabels() | 澹版槑姝ゆā鍨嬪睘鎬х殑鏍囩銆 | CCodeModel |
| attributeNames() | 杩斿洖灞炴у悕鍒楄〃銆 | CFormModel |
| behaviors() | Returns a list of behaviors that this model should behave as. | CModel |
| canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
| canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
| class2id() | 灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓 HTML ID銆 | CCodeModel |
| class2name() | 灏嗕竴涓被鐨勫悕瀛楄浆鎹负浠ョ┖鐧藉垎闅旂殑鍗曡瘝銆 | CCodeModel |
| class2var() | 灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓瀛楁瘝灏忓啓鐨勫彉閲忓悕銆 | CCodeModel |
| classExists() | 妫娴嬬粰瀹氬悕瀛楃殑绫绘槸鍚﹀瓨鍦紙瀵瑰ぇ灏忓啓鏁忔劅锛夈 | CCodeModel |
| clearErrors() | Removes errors for all attributes or a single attribute. | CModel |
| confirmed() | CCodeModel | |
| createValidators() | Creates validator objects based on the specification in rules. | CModel |
| detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
| detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
| detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
| disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
| disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
| enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
| enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
| errorMessage() | 杩斿洖褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆 | CCodeModel |
| evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
| generateAttributeLabel() | Generates a user friendly attribute label. | CModel |
| getAttributeLabel() | Returns the text label for the specified attribute. | CModel |
| getAttributes() | Returns all attribute values. | CModel |
| getError() | Returns the first error of the specified attribute. | CModel |
| getErrors() | Returns the errors for all attribute or a single attribute. | CModel |
| getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
| getIterator() | Returns an iterator for traversing the attributes in the model. | CModel |
| getSafeAttributeNames() | Returns the attribute names that are safe to be massively assigned. | CModel |
| getScenario() | Returns the scenario that this model is used in. | CModel |
| getStickyFile() | 杩斿洖瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆 | CCodeModel |
| getTemplatePath() | 杩斿洖鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘 | CCodeModel |
| getTemplates() | 杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 | CCodeModel |
| getValidatorList() | Returns all the validators declared in the model. | CModel |
| getValidators() | Returns the validators applicable to the current scenario. | CModel |
| hasErrors() | Returns a value indicating whether there is any validation error. | CModel |
| hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
| hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
| hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
| init() | 鍒濆鍖栨ā鍨嬨 | CFormModel |
| isAttributeRequired() | Returns a value indicating whether the attribute is required. | CModel |
| isAttributeSafe() | Returns a value indicating whether the attribute is safe for massive assignments. | CModel |
| loadStickyAttributes() | 浠庢枃浠朵腑杞藉叆绮樿繛灞炴у肩劧鍚庡皢瀹冧滑濉厖鍒版ā鍨嬩腑銆 | CCodeModel |
| offsetExists() | Returns whether there is an element at the specified offset. | CModel |
| offsetGet() | Returns the element at the specified offset. | CModel |
| offsetSet() | Sets the element at the specified offset. | CModel |
| offsetUnset() | Unsets the element at the specified offset. | CModel |
| onAfterConstruct() | This event is raised after the model instance is created by new operator. | CModel |
| onAfterValidate() | This event is raised after the validation is performed. | CModel |
| onBeforeValidate() | This event is raised before the validation is performed. | CModel |
| onUnsafeAttribute() | This method is invoked when an unsafe attribute is being massively assigned. | CModel |
| pluralize() | 灏嗕竴涓崟璇嶈浆鎹负澶嶆暟褰㈠紡銆 | CCodeModel |
| prepare() | 鍑嗗灏嗙敓鎴愮殑浠g爜鏂囦欢銆 | CCodeModel |
| raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
| render() | 浣跨敤鎸囧畾鐨勪唬鐮佹ā鏉挎枃浠剁敓鎴愪唬鐮併 | CCodeModel |
| renderResults() | CCodeModel | |
| requiredTemplates() | 杩斿洖蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃銆 | CCodeModel |
| rules() | 澹版槑姝ゆā鍨嬬殑楠岃瘉瑙勫垯銆 | CCodeModel |
| save() | 灏嗙敓鎴愮殑浠g爜淇濆瓨鍒拌嫢骞叉枃浠朵腑銆 | CCodeModel |
| saveStickyAttributes() | 淇濆瓨绮樿繛灞炴у煎埌涓涓枃浠朵腑銆 | CCodeModel |
| setAttributes() | Sets the attribute values in a massive way. | CModel |
| setScenario() | Sets the scenario for the model. | CModel |
| sticky() | 绮樿繛楠岃瘉鍣紙The "sticky" validator锛夈 | CCodeModel |
| successMessage() | 杩斿洖褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆 | CCodeModel |
| unsetAttributes() | Sets the attributes to be null. | CModel |
| validate() | Performs the validation. | CModel |
| validateReservedWord() | 楠岃瘉涓涓睘鎬ф病鏈変娇鐢≒HP鐨勪繚鐣欏叧閿瓧銆 | CCodeModel |
| validateTemplate() | 楠岃瘉妯℃澘鐨勯夋嫨銆 | CCodeModel |
鍙椾繚鎶ゆ柟娉
| 鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|
| afterConstruct() | This method is invoked after a model instance is created by new operator. | CModel |
| afterValidate() | This method is invoked after validation ends. | CModel |
| beforeValidate() | This method is invoked before validation starts. | CModel |
浜嬩欢
| 浜嬩欢 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|
| onAfterConstruct | This event is raised after the model instance is created by new operator. | CModel |
| onBeforeValidate | This event is raised before the validation is performed. | CModel |
| onAfterValidate | This event is raised after the validation is performed. | CModel |
| onUnsafeAttribute | This method is invoked when an unsafe attribute is being massively assigned. | CModel |
灞炴ц缁
鐢ㄦ埛瀵规槸鍚︿互鏂扮敓鎴愮殑浠g爜鏂囦欢瑕嗙洊鍘熸湁鐨勪唬鐮佹枃浠剁殑纭淇℃伅銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆
CCodeFile瀵硅薄鐨勫垪琛紝琛ㄧず灏嗚鐢熸垚鐨勪唬鐮佹枃浠躲 prepare()鏂规硶璐熻矗濉厖姝ゅ睘鎬с
姝ゆā鍨嬬殑鐘舵併 姝ゅ睘鎬у奸氳繃CCodeGenerator鍐呴儴绠$悊銆
瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆
鐢ㄦ埛閫夋嫨鐨勪唬鐮佹ā鏉跨殑鍚嶅瓧銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆
鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘
杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 姝ゆ柟娉曠畝鍗曞湴杩斿洖CCodeGenerator::templates灞炴у笺
鏂规硶璇︾粏
|
public array attributeLabels()
| ||
| {return} | array | 灞炴х殑鏍囩 |
public function attributeLabels()
{
return array(
'template'=>'Code Template',
);
}
澹版槑姝ゆā鍨嬪睘鎬х殑鏍囩銆 瀛愮被蹇呴』鎸変笅鍒楁牸寮忚鐩栨鏂规硶锛
return array_merge(parent::attributeLabels(), array(
...labels for the child class attributes...
));
|
public string class2id(string $name)
| ||
| $name | string | 闇瑕佽浆鎹㈢殑瀛楃涓 |
| {return} | string | 杞崲鐨勭粨鏋 |
public function class2id($name)
{
return trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/', '-\0', $name))),'-');
}
灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓 HTML ID銆 渚嬪锛'PostTag'灏嗚浆鎹负'post-tag'銆
|
public string class2name(string $name, boolean $ucwords=true)
| ||
| $name | string | 闇瑕佹暟鐨勭殑瀛楃涓层 |
| $ucwords | boolean | 鏄惁灏嗘瘡涓崟璇嶇殑棣栧瓧姣嶈浆涓哄ぇ鍐 |
| {return} | string | 杞崲鐨勭粨鏋 |
public function class2name($name,$ucwords=true)
{
$result=trim(strtolower(str_replace('_',' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($result) : $result;
}
灏嗕竴涓被鐨勫悕瀛楄浆鎹负浠ョ┖鐧藉垎闅旂殑鍗曡瘝銆 渚嬪锛'PostTag'灏嗚浆鎹负'Post Tag'銆
|
public string class2var(string $name)
| ||
| $name | string | 绫荤殑鍚嶅瓧 |
| {return} | string | 浠庣被鐨勫悕瀛楄浆鎹㈣屾潵鐨勫彉閲忓悕瀛 |
public function class2var($name)
{
$name[0]=strtolower($name[0]);
return $name;
}
灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓瀛楁瘝灏忓啓鐨勫彉閲忓悕銆 涔嬫墍浠ユ彁渚涙鏂规硶锛屾槸鍥犱负PHP鍑芥暟lcfirst()鍙湪 PHP5.3+ 鍙敤銆
|
public boolean classExists(string $name)
| ||
| $name | string | 闇瑕佹娴嬬殑绫荤殑鍚嶅瓧 |
| {return} | boolean | 姝ょ被鏄惁瀛樺湪 |
public function classExists($name)
{
return class_exists($name,false) && in_array($name, get_declared_classes());
}
妫娴嬬粰瀹氬悕瀛楃殑绫绘槸鍚﹀瓨鍦紙瀵瑰ぇ灏忓啓鏁忔劅锛夈
|
public void confirmed(CCodeFile $file)
| ||
| $file | CCodeFile | 灏嗚淇濆瓨鐨勪唬鐮佹枃浠 |
public function confirmed($file)
{
return $this->answers===null && $file->operation===CCodeFile::OP_NEW
|| is_array($this->answers) && isset($this->answers[md5($file->path)]);
}
|
public string errorMessage()
| ||
| {return} | string | 褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆 |
public function errorMessage()
{
return 'There was some error when generating the code. Please check the following messages.';
}
杩斿洖褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆 濡傛灉瀛愮被闇瑕佽嚜瀹氫箟閿欒鎻愮ず淇℃伅灏辫鐩栨鏂规硶銆
|
public string getStickyFile()
| ||
| {return} | string | 瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆 |
public function getStickyFile()
{
return Yii::app()->runtimePath.'/gii-'.Yii::getVersion().'/'.get_class($this).'.php';
}
|
public string getTemplatePath()
| ||
| {return} | string | 鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘 |
public function getTemplatePath()
{
$templates=$this->getTemplates();
if(isset($templates[$this->template]))
return $templates[$this->template];
else if(empty($templates))
throw new CHttpException(500,'No templates are available.');
else
throw new CHttpException(500,'Invalid template selection.');
}
|
public array getTemplates()
| ||
| {return} | array | 鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 |
public function getTemplates()
{
return Yii::app()->controller->templates;
}
杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 姝ゆ柟娉曠畝鍗曞湴杩斿洖CCodeGenerator::templates灞炴у笺
|
public void loadStickyAttributes()
|
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;
}
}
}
}
浠庢枃浠朵腑杞藉叆绮樿繛灞炴у肩劧鍚庡皢瀹冧滑濉厖鍒版ā鍨嬩腑銆
|
public string pluralize(string $name)
| ||
| $name | string | 闇瑕佽浆涓哄鏁扮殑鍗曡瘝 |
| {return} | string | 鍗曡瘝鐨勫鏁 |
public function pluralize($name)
{
$rules=array(
'/move$/i' => 'moves',
'/foot$/i' => 'feet',
'/child$/i' => 'children',
'/human$/i' => 'humans',
'/man$/i' => 'men',
'/tooth$/i' => 'teeth',
'/person$/i' => 'people',
'/([m|l])ouse$/i' => '\1ice',
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/(shea|lea|loa|thie)f$/i' => '\1ves',
'/([ti])um$/i' => '\1a',
'/(tomat|potat|ech|her|vet)o$/i' => '\1oes',
'/(bu)s$/i' => '\1ses',
'/(ax|test)is$/i' => '\1es',
'/s$/' => 's',
);
foreach($rules as $rule=>$replacement)
{
if(preg_match($rule,$name))
return preg_replace($rule,$replacement,$name);
}
return $name.'s';
}
灏嗕竴涓崟璇嶈浆鎹负澶嶆暟褰㈠紡銆 娉ㄦ剰锛氭鏂规硶浠呴拡瀵硅嫳璇紒 渚嬪锛'apple'灏嗚浆鎹负'apples'锛岃'child'灏嗚浆鎹负'children'銆
|
abstract public void prepare()
|
鍑嗗灏嗙敓鎴愮殑浠g爜鏂囦欢銆 杩欐槸瀛愮被闇瑕佸疄鐜扮殑涓昏鏂规硶锛屽畠鍖呭惈浜嗕娇鐢ㄥ皢瑕佺敓鎴 鐨勪唬鐮佹枃浠剁殑鍒楄〃濉厖files灞炴х殑閫昏緫銆
|
public string render(string $templateFile, array $_params_=NULL)
| ||
| $templateFile | string | 浠g爜妯℃澘鏂囦欢璺緞 |
| $_params_ | array | 琚彁鍙栧埌浠g爜妯℃澘涓娇鐢ㄧ殑鍙傛暟鐨勯泦鍚 |
| {return} | string | 鐢熸垚鐨勪唬鐮 |
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();
}
浣跨敤鎸囧畾鐨勪唬鐮佹ā鏉挎枃浠剁敓鎴愪唬鐮併 姝ゆ柟娉曚富瑕佸湪generate涓鐢ㄤ簬鐢熸垚浠g爜銆
|
public string renderResults()
| ||
| {return} | string | 浠g爜鐢熸垚鐨勭粨鏋滄棩蹇椼 |
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";
else if($file->operation===CCodeFile::OP_NEW && $this->confirmed($file))
$output.=' generated '.$file->relativePath."\n";
else if($file->operation===CCodeFile::OP_OVERWRITE && $this->confirmed($file))
$output.=' overwrote '.$file->relativePath."\n";
else
$output.=' skipped '.$file->relativePath."\n";
}
$output.="done!\n";
return $output;
}
|
public array requiredTemplates()
| ||
| {return} | array | 蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃锛屽畠浠槸涓 templatePath鐩稿叧鑱旂殑鏂囦欢璺緞銆 |
杩斿洖蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃銆 娲剧敓绫婚氬父闇瑕佽鐩栨鏂规硶銆
|
public array rules()
| ||
| {return} | array | 楠岃瘉瑙勫垯 |
public function rules()
{
return array(
array('template', 'required'),
array('template', 'validateTemplate', 'skipOnError'=>true),
array('template', 'sticky'),
);
}
澹版槑姝ゆā鍨嬬殑楠岃瘉瑙勫垯銆 瀛愮被蹇呴』鎸変笅鍒楁牸寮忚鐩栨鏂规硶锛
return array_merge(parent::rules(), array(
...rules for the child class...
));
|
public void save()
|
public function save()
{
$result=true;
foreach($this->files as $file)
{
if($this->confirmed($file))
$result=$file->save() && $result;
}
return $result;
}
灏嗙敓鎴愮殑浠g爜淇濆瓨鍒拌嫢骞叉枃浠朵腑銆
|
public void saveStickyAttributes()
|
public function saveStickyAttributes()
{
$path=$this->getStickyFile();
@mkdir(dirname($path),0755,true);
file_put_contents($path,"<?php\nreturn ".var_export($this->_stickyAttributes,true).";\n");
}
淇濆瓨绮樿繛灞炴у煎埌涓涓枃浠朵腑銆
|
public void sticky(string $attribute, array $params)
| ||
| $attribute | string | 闇瑕侀獙璇佺殑灞炴 |
| $params | array | 楠岃瘉鍙傛暟 |
public function sticky($attribute,$params)
{
if(!$this->hasErrors())
$this->_stickyAttributes[$attribute]=$this->$attribute;
}
绮樿繛楠岃瘉鍣紙The "sticky" validator锛夈 姝ら獙璇佸櫒骞朵笉鐪熺殑瀵瑰睘鎬ц繘琛岄獙璇侊紝 瀹冨疄闄呬笂鏄皢姝ゅ睘鎬х殑鍊间繚瀛樺湪涓涓枃浠朵腑浠ヤ娇灞炴ф敮鎸佺矘杩炲姛鑳姐
|
public string successMessage()
| ||
| {return} | string | 褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆 |
public function successMessage()
{
return 'The code has been generated successfully.';
}
杩斿洖褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆 濡傛灉瀛愮被闇瑕佽嚜瀹氫箟鎻愮ず淇℃伅灏辫鐩栨鏂规硶銆
|
public void validateReservedWord(string $attribute, array $params)
| ||
| $attribute | string | 闇瑕侀獙璇佺殑灞炴 |
| $params | array | 楠岃瘉鍙傛暟 |
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.');
}
楠岃瘉涓涓睘鎬ф病鏈変娇鐢≒HP鐨勪繚鐣欏叧閿瓧銆
|
public void validateTemplate(string $attribute, array $params)
| ||
| $attribute | string | 闇瑕侀獙璇佺殑灞炴 |
| $params | array | 楠岃瘉鍙傛暟 |
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'.");
}
}
}
楠岃瘉妯℃澘鐨勯夋嫨銆 姝ゆ柟娉曢獙璇佺敤鎴锋槸鍚﹂夋嫨浜嗕竴涓瓨鍦ㄧ殑妯℃澘锛屼笖姝ゆā鏉垮寘鍚湁 鎵鏈夊湪requiredTemplates涓寚瀹氱殑蹇呴渶鐨勬ā鏉挎枃浠躲