CCodeFile
| Package | system.gii | 
|---|---|
| Inheritance | class CCodeFile » CComponent | 
| Since | 1.1.2 | 
| Source Code | framework/gii/CCodeFile.php | 
 CCodeFile represents a code file being generated.
  Public Properties
| Property | Type | Description | Defined By | 
|---|---|---|---|
| content | mixed | the newly generated code. | CCodeFile | 
| error | string | the error occurred when saving the code into a file | CCodeFile | 
| operation | string | the operation to be performed | CCodeFile | 
| path | string | the file path that the new code should be saved to. | CCodeFile | 
| relativePath | string | the code file path relative to the application base path. | CCodeFile | 
| type | string | the code file extension (e.g. php, txt) | CCodeFile | 
Public Methods
| Method | Description | Defined By | 
|---|---|---|
| __call() | Calls the named method which is not a class method. | CComponent | 
| __construct() | Constructor. | CCodeFile | 
| __get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent | 
| __isset() | Checks if a property value is null. | CComponent | 
| __set() | Sets value of a component property. | CComponent | 
| __unset() | Sets a component property to be null. | CComponent | 
| asa() | Returns the named behavior object. | CComponent | 
| attachBehavior() | Attaches a behavior to this component. | CComponent | 
| attachBehaviors() | Attaches a list of behaviors to the component. | CComponent | 
| attachEventHandler() | Attaches an event handler to an event. | CComponent | 
| canGetProperty() | Determines whether a property can be read. | CComponent | 
| canSetProperty() | Determines whether a property can be set. | CComponent | 
| detachBehavior() | Detaches a behavior from the component. | CComponent | 
| detachBehaviors() | Detaches all behaviors from the component. | CComponent | 
| detachEventHandler() | Detaches an existing event handler. | CComponent | 
| disableBehavior() | Disables an attached behavior. | CComponent | 
| disableBehaviors() | Disables all behaviors attached to this component. | CComponent | 
| enableBehavior() | Enables an attached behavior. | CComponent | 
| enableBehaviors() | Enables all behaviors attached to this component. | CComponent | 
| evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent | 
| getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent | 
| getRelativePath() | Returns the code file path relative to the application base path. | CCodeFile | 
| getType() | Returns the code file extension (e.g. php, txt) | CCodeFile | 
| hasEvent() | Determines whether an event is defined. | CComponent | 
| hasEventHandler() | Checks whether the named event has attached handlers. | CComponent | 
| hasProperty() | Determines whether a property is defined. | CComponent | 
| raiseEvent() | Raises an event. | CComponent | 
| save() | Saves the code into the file path. | CCodeFile | 
Property Details
content property
public mixed $content;
the newly generated code. If this is null, it means path should be treated as a directory.
error property
public string $error;
the error occurred when saving the code into a file
operation property
public string $operation;
the operation to be performed
path property
public string $path;
the file path that the new code should be saved to.
relativePath property read-only
public string getRelativePath()
the code file path relative to the application base path.
type property read-only
public string getType()
the code file extension (e.g. php, txt)
Method Details
__construct() method
| public void __construct(string $path, string $content) | ||
| $path | string | the file path that the new code should be saved to. | 
| $content | string | the newly generated code | 
 Source Code: framework/gii/CCodeFile.php#50 (show) 
 public function __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) // is dir
$this->operation=is_dir($path) ? self::OP_SKIP : self::OP_NEW;
else
$this->operation=self::OP_NEW;
}
Constructor.
getRelativePath() method
| public string getRelativePath() | ||
| {return} | string | the code file path relative to the application base path. | 
 Source Code: framework/gii/CCodeFile.php#116 (show) 
  public function getRelativePath()
{
if(strpos($this->path,Yii::app()->basePath)===0)
return substr($this->path,strlen(Yii::app()->basePath)+1);
else
return $this->path;
}
getType() method
| public string getType() | ||
| {return} | string | the code file extension (e.g. php, txt) | 
 Source Code: framework/gii/CCodeFile.php#127 (show) 
  public function getType()
{
if(($pos=strrpos($this->path,'.'))!==false)
return substr($this->path,$pos+1);
else
return 'unknown';
}
save() method
| public void save() | 
 Source Code: framework/gii/CCodeFile.php#65 (show) 
 public function save()
{
$module=Yii::app()->controller->module;
if($this->content===null) // a directory
{
if(!is_dir($this->path))
{
$oldmask=@umask(0);
$result=@mkdir($this->path,$module->newDirMode,true);
@umask($oldmask);
if(!$result)
{
$this->error="Unable to create the directory '{$this->path}'.";
return false;
}
}
return true;
}
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="Unable to create the directory '$dir'.";
return false;
}
}
}
if(@file_put_contents($this->path,$this->content)===false)
{
$this->error="Unable to write the file '{$this->path}'.";
return false;
}
else
{
$oldmask=@umask(0);
@chmod($this->path,$module->newFileMode);
@umask($oldmask);
}
return true;
}
Saves the code into the file path.
    © 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
    http://www.yiiframework.com/doc/api/1.1/CCodeFile