CCodeFile

优质
小牛编辑
126浏览
2023-12-01
所有包 | 属性 | 方法
system.gii
继承class CCodeFile » CComponent
源自1.1.2
版本$Id: CCodeFile.php 3426 2011-10-25 00:01:09Z alexander.makarow $
源码framework/gii/CCodeFile.php
CCodeFile表示生成的一个代码文件。

公共属性

隐藏继承属性

属性类型描述定义在
contentmixed新生成的代码。如果此值为null,表示path 将被当作是一个目录。CCodeFile
errorstring将代码保存到文件时发生的错误CCodeFile
operationstring将执行的操作CCodeFile
pathstring生成的代码存放的文件路径。CCodeFile
relativePathstring此代码文件的相对路径(基于应用的基本路径)。CCodeFile
typestring此代码文件的扩展名(例如:php,txt)。CCodeFile

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造方法。CCodeFile
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getRelativePath()返回此代码文件的相对路径(基于应用的基本路径)。CCodeFile
getType()返回此代码文件的扩展名(例如:php,txt)。CCodeFile
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
raiseEvent()发起一个事件。CComponent
save()将生成的代码保存到文件path。CCodeFile

属性详细

content 属性 public mixed $content;

新生成的代码。如果此值为null,表示path 将被当作是一个目录。

error 属性 public string $error;

将代码保存到文件时发生的错误

operation 属性 public string $operation;

将执行的操作

path 属性 public string $path;

生成的代码存放的文件路径。

relativePath 属性 只读 public string getRelativePath()

此代码文件的相对路径(基于应用的基本路径)。

type 属性 只读 public string getType()

此代码文件的扩展名(例如:php,txt)。

方法详细

__construct() 方法
public void __construct(string $path, string $content)
$pathstring生成的代码存放的文件路径。
$contentstring新生成的代码
源码: framework/gii/CCodeFile.php#51 (显示) publicfunction__construct($path,$content)
{
$this->path=strtr($path,array('/'=>DIRECTORY_SEPARATOR,'\'=>DIRECTORY_SEPARATOR));
$this->content=$content;
if(is_file($path))
$this->operation=file_get_contents($path)===$content?self::OP_SKIP:self::OP_OVERWRITE;
elseif($content===null)//isdir
$this->operation=is_dir($path)?self::OP_SKIP:self::OP_NEW;
else
$this->operation=self::OP_NEW;
}

构造方法。

getRelativePath() 方法
public string getRelativePath()
{return}string此代码文件的相对路径(基于应用的基本路径)。
源码: framework/gii/CCodeFile.php#117 (显示) publicfunctiongetRelativePath()
{
if(strpos($this->path,Yii::app()->basePath)===0)
returnsubstr($this->path,strlen(Yii::app()->basePath)+1);
else
return$this->path;
}
getType() 方法
public string getType()
{return}string此代码文件的扩展名(例如:php,txt)。
源码: framework/gii/CCodeFile.php#128 (显示) publicfunctiongetType()
{
if(($pos=strrpos($this->path,'.'))!==false)
returnsubstr($this->path,$pos+1);
else
return'unknown';
}
save() 方法
public void save()
源码: framework/gii/CCodeFile.php#66 (显示) publicfunctionsave()
{
$module=Yii::app()->controller->module;
if($this->content===null)//adirectory
{
if(!is_dir($this->path))
{
$oldmask=@umask(0);
$result=@mkdir($this->path,$module->newDirMode,true);
@umask($oldmask);
if(!$result)
{
$this->error="Unabletocreatethedirectory'{$this->path}'.";
returnfalse;
}
}
returntrue;
}

if($this->operation===self::OP_NEW)
{
$dir=dirname($this->path);
if(!is_dir($dir))
{
$oldmask=@umask(0);
$result=@mkdir($dir,$module->newDirMode,true);
@umask($oldmask);
if(!$result)
{
$this->error="Unabletocreatethedirectory'$dir'.";
returnfalse;
}
}
}
if(@file_put_contents($this->path,$this->content)===false)
{
$this->error="Unabletowritethefile'{$this->path}'.";
returnfalse;
}
else
{
$oldmask=@umask(0);
@chmod($this->path,$module->newFileMode);
@umask($oldmask);
}
returntrue;
}

将生成的代码保存到文件path。