Added Yii framework files

1 parent 09968d6e
Showing with 4881 additions and 0 deletions

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

deny from all
<?php
/**
* This file contains the base application component class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CApplicationComponent is the base class for application component classes.
*
* CApplicationComponent implements the basic methods required by {@link IApplicationComponent}.
*
* When developing an application component, try to put application component initialization code in
* the {@link init()} method instead of the constructor. This has the advantage that
* the application component can be customized through application configuration.
*
* @property boolean $isInitialized Whether this application component has been initialized (ie, {@link init()} is invoked).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
abstract class CApplicationComponent extends CComponent implements IApplicationComponent
{
/**
* @var array the behaviors that should be attached to this component.
* The behaviors will be attached to the component when {@link init} is called.
* Please refer to {@link CModel::behaviors} on how to specify the value of this property.
*/
public $behaviors=array();
private $_initialized=false;
/**
* Initializes the application component.
* This method is required by {@link IApplicationComponent} and is invoked by application.
* If you override this method, make sure to call the parent implementation
* so that the application component can be marked as initialized.
*/
public function init()
{
$this->attachBehaviors($this->behaviors);
$this->_initialized=true;
}
/**
* Checks if this application component bas been initialized.
* @return boolean whether this application component has been initialized (ie, {@link init()} is invoked).
*/
public function getIsInitialized()
{
return $this->_initialized;
}
}
<?php
/**
* CBehavior class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CBehavior is a convenient base class for behavior classes.
*
* @property CComponent $owner The owner component that this behavior is attached to.
* @property boolean $enabled Whether this behavior is enabled.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
*/
class CBehavior extends CComponent implements IBehavior
{
private $_enabled;
private $_owner;
/**
* Declares events and the corresponding event handler methods.
* The events are defined by the {@link owner} component, while the handler
* methods by the behavior class. The handlers will be attached to the corresponding
* events when the behavior is attached to the {@link owner} component; and they
* will be detached from the events when the behavior is detached from the component.
* @return array events (array keys) and the corresponding event handler methods (array values).
*/
public function events()
{
return array();
}
/**
* Attaches the behavior object to the component.
* The default implementation will set the {@link owner} property
* and attach event handlers as declared in {@link events}.
* Make sure you call the parent implementation if you override this method.
* @param CComponent $owner the component that this behavior is to be attached to.
*/
public function attach($owner)
{
$this->_owner=$owner;
foreach($this->events() as $event=>$handler)
$owner->attachEventHandler($event,array($this,$handler));
}
/**
* Detaches the behavior object from the component.
* The default implementation will unset the {@link owner} property
* and detach event handlers declared in {@link events}.
* Make sure you call the parent implementation if you override this method.
* @param CComponent $owner the component that this behavior is to be detached from.
*/
public function detach($owner)
{
foreach($this->events() as $event=>$handler)
$owner->detachEventHandler($event,array($this,$handler));
$this->_owner=null;
}
/**
* @return CComponent the owner component that this behavior is attached to.
*/
public function getOwner()
{
return $this->_owner;
}
/**
* @return boolean whether this behavior is enabled
*/
public function getEnabled()
{
return $this->_enabled;
}
/**
* @param boolean $value whether this behavior is enabled
*/
public function setEnabled($value)
{
if($this->_enabled!=$value && $this->_owner)
{
if($value)
{
foreach($this->events() as $event=>$handler)
$this->_owner->attachEventHandler($event,array($this,$handler));
}
else
{
foreach($this->events() as $event=>$handler)
$this->_owner->detachEventHandler($event,array($this,$handler));
}
}
$this->_enabled=$value;
}
}
<?php
/**
* CErrorEvent class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CErrorEvent represents the parameter for the {@link CApplication::onError onError} event.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CErrorEvent extends CEvent
{
/**
* @var string error code
*/
public $code;
/**
* @var string error message
*/
public $message;
/**
* @var string error message
*/
public $file;
/**
* @var string error file
*/
public $line;
/**
* Constructor.
* @param mixed $sender sender of the event
* @param string $code error code
* @param string $message error message
* @param string $file error file
* @param integer $line error line
*/
public function __construct($sender,$code,$message,$file,$line)
{
$this->code=$code;
$this->message=$message;
$this->file=$file;
$this->line=$line;
parent::__construct($sender);
}
}
<?php
/**
* CException class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CException represents a generic exception for all purposes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CException extends Exception
{
}
<?php
/**
* CExceptionEvent class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CExceptionEvent represents the parameter for the {@link CApplication::onException onException} event.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CExceptionEvent extends CEvent
{
/**
* @var CException the exception that this event is about.
*/
public $exception;
/**
* Constructor.
* @param mixed $sender sender of the event
* @param CException $exception the exception
*/
public function __construct($sender,$exception)
{
$this->exception=$exception;
parent::__construct($sender);
}
}
\ No newline at end of file
<?php
/**
* CHttpException class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CHttpException represents an exception caused by invalid operations of end-users.
*
* The HTTP error code can be obtained via {@link statusCode}.
* Error handlers may use this status code to decide how to format the error page.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CHttpException extends CException
{
/**
* @var integer HTTP status code, such as 403, 404, 500, etc.
*/
public $statusCode;
/**
* Constructor.
* @param integer $status HTTP status code, such as 404, 500, etc.
* @param string $message error message
* @param integer $code error code
*/
public function __construct($status,$message=null,$code=0)
{
$this->statusCode=$status;
parent::__construct($message,$code);
}
}
<?php
/**
* CModelBehavior class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CModelBehavior is a base class for behaviors that are attached to a model component.
* The model should extend from {@link CModel} or its child classes.
*
* @property CModel $owner The owner model that this behavior is attached to.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
*/
class CModelBehavior extends CBehavior
{
/**
* Declares events and the corresponding event handler methods.
* The default implementation returns 'onAfterConstruct', 'onBeforeValidate' and 'onAfterValidate' events and handlers.
* If you override this method, make sure you merge the parent result to the return value.
* @return array events (array keys) and the corresponding event handler methods (array values).
* @see CBehavior::events
*/
public function events()
{
return array(
'onAfterConstruct'=>'afterConstruct',
'onBeforeValidate'=>'beforeValidate',
'onAfterValidate'=>'afterValidate',
);
}
/**
* Responds to {@link CModel::onAfterConstruct} event.
* Overrides this method if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
* @param CEvent $event event parameter
*/
public function afterConstruct($event)
{
}
/**
* Responds to {@link CModel::onBeforeValidate} event.
* Overrides this method if you want to handle the corresponding event of the {@link owner}.
* You may set {@link CModelEvent::isValid} to be false to quit the validation process.
* @param CModelEvent $event event parameter
*/
public function beforeValidate($event)
{
}
/**
* Responds to {@link CModel::onAfterValidate} event.
* Overrides this method if you want to handle the corresponding event of the {@link owner}.
* @param CEvent $event event parameter
*/
public function afterValidate($event)
{
}
}
<?php
/**
* CModelEvent class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CModelEvent class.
*
* CModelEvent represents the event parameters needed by events raised by a model.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CModelEvent extends CEvent
{
/**
* @var boolean whether the model is in valid status and should continue its normal method execution cycles. Defaults to true.
* For example, when this event is raised in a {@link CFormModel} object that is executing {@link CModel::beforeValidate},
* if this property is set false by the event handler, the {@link CModel::validate} method will quit after handling this event.
* If true, the normal execution cycles will continue, including performing the real validations and calling
* {@link CModel::afterValidate}.
*/
public $isValid=true;
}
<?php
/**
* This file contains classes implementing security manager feature.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CStatePersister implements a file-based persistent data storage.
*
* It can be used to keep data available through multiple requests and sessions.
*
* By default, CStatePersister stores data in a file named 'state.bin' that is located
* under the application {@link CApplication::getRuntimePath runtime path}.
* You may change the location by setting the {@link stateFile} property.
*
* To retrieve the data from CStatePersister, call {@link load()}. To save the data,
* call {@link save()}.
*
* Comparison among state persister, session and cache is as follows:
* <ul>
* <li>session: data persisting within a single user session.</li>
* <li>state persister: data persisting through all requests/sessions (e.g. hit counter).</li>
* <li>cache: volatile and fast storage. It may be used as storage medium for session or state persister.</li>
* </ul>
*
* Since server resource is often limited, be cautious if you plan to use CStatePersister
* to store large amount of data. You should also consider using database-based persister
* to improve the throughput.
*
* CStatePersister is a core application component used to store global application state.
* It may be accessed via {@link CApplication::getStatePersister()}.
* page state persistent method based on cache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.base
* @since 1.0
*/
class CStatePersister extends CApplicationComponent implements IStatePersister
{
/**
* @var string the file path storing the state data. Make sure the directory containing
* the file exists and is writable by the Web server process. If using relative path, also
* make sure the path is correct.
*/
public $stateFile;
/**
* @var string the ID of the cache application component that is used to cache the state values.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable caching state values.
*/
public $cacheID='cache';
/**
* Initializes the component.
* This method overrides the parent implementation by making sure {@link stateFile}
* contains valid value.
*/
public function init()
{
parent::init();
if($this->stateFile===null)
$this->stateFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'state.bin';
$dir=dirname($this->stateFile);
if(!is_dir($dir) || !is_writable($dir))
throw new CException(Yii::t('yii','Unable to create application state file "{file}". Make sure the directory containing the file exists and is writable by the Web server process.',
array('{file}'=>$this->stateFile)));
}
/**
* Loads state data from persistent storage.
* @return mixed state data. Null if no state data available.
*/
public function load()
{
$stateFile=$this->stateFile;
if($this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
{
$cacheKey='Yii.CStatePersister.'.$stateFile;
if(($value=$cache->get($cacheKey))!==false)
return unserialize($value);
else if(($content=@file_get_contents($stateFile))!==false)
{
$cache->set($cacheKey,$content,0,new CFileCacheDependency($stateFile));
return unserialize($content);
}
else
return null;
}
else if(($content=@file_get_contents($stateFile))!==false)
return unserialize($content);
else
return null;
}
/**
* Saves application state in persistent storage.
* @param mixed $state state data (must be serializable).
*/
public function save($state)
{
file_put_contents($this->stateFile,serialize($state),LOCK_EX);
}
}
<?php
/**
* CApcCache class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CApcCache provides APC caching in terms of an application component.
*
* The caching is based on {@link http://www.php.net/apc APC}.
* To use this application component, the APC PHP extension must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CApcCache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class CApcCache extends CCache
{
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of APC.
* @throws CException if APC cache extension is not loaded or is disabled.
*/
public function init()
{
parent::init();
if(!extension_loaded('apc'))
throw new CException(Yii::t('yii','CApcCache requires PHP apc extension to be loaded.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return apc_fetch($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return apc_fetch($keys);
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
return apc_store($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
return apc_add($key,$value,$expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return apc_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
return apc_clear_cache('user');
}
}
<?php
/**
* CDbCache class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbCache implements a cache application component by storing cached data in a database.
*
* CDbCache stores cache data in a DB table named {@link cacheTableName}.
* If the table does not exist, it will be automatically created.
* By setting {@link autoCreateCacheTable} to false, you can also manually create the DB table.
*
* CDbCache relies on {@link http://www.php.net/manual/en/ref.pdo.php PDO} to access database.
* By default, it will use a SQLite3 database under the application runtime directory.
* You can also specify {@link connectionID} so that it makes use of
* a DB application component to access database.
*
* See {@link CCache} manual for common cache operations that are supported by CDbCache.
*
* @property integer $gCProbability The probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* @property CDbConnection $dbConnection The DB connection instance.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class CDbCache extends CCache
{
/**
* @var string the ID of the {@link CDbConnection} application component. If not set,
* a SQLite3 database will be automatically created and used. The SQLite database file
* is <code>protected/runtime/cache-YiiVersion.db</code>.
*/
public $connectionID;
/**
* @var string name of the DB table to store cache content. Defaults to 'YiiCache'.
* Note, if {@link autoCreateCacheTable} is false and you want to create the DB table
* manually by yourself, you need to make sure the DB table is of the following structure:
* <pre>
* (id CHAR(128) PRIMARY KEY, expire INTEGER, value BLOB)
* </pre>
* Note, some DBMS might not support BLOB type. In this case, replace 'BLOB' with a suitable
* binary data type (e.g. LONGBLOB in MySQL, BYTEA in PostgreSQL.)
* @see autoCreateCacheTable
*/
public $cacheTableName='YiiCache';
/**
* @var boolean whether the cache DB table should be created automatically if it does not exist. Defaults to true.
* If you already have the table created, it is recommended you set this property to be false to improve performance.
* @see cacheTableName
*/
public $autoCreateCacheTable=true;
/**
* @var CDbConnection the DB connection instance
*/
private $_db;
private $_gcProbability=100;
private $_gced=false;
/**
* Initializes this application component.
*
* This method is required by the {@link IApplicationComponent} interface.
* It ensures the existence of the cache DB table.
* It also removes expired data items from the cache.
*/
public function init()
{
parent::init();
$db=$this->getDbConnection();
$db->setActive(true);
if($this->autoCreateCacheTable)
{
$sql="DELETE FROM {$this->cacheTableName} WHERE expire>0 AND expire<".time();
try
{
$db->createCommand($sql)->execute();
}
catch(Exception $e)
{
$this->createCacheTable($db,$this->cacheTableName);
}
}
}
/**
* @return integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
*/
public function getGCProbability()
{
return $this->_gcProbability;
}
/**
* @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
*/
public function setGCProbability($value)
{
$value=(int)$value;
if($value<0)
$value=0;
if($value>1000000)
$value=1000000;
$this->_gcProbability=$value;
}
/**
* Creates the cache DB table.
* @param CDbConnection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected function createCacheTable($db,$tableName)
{
$driver=$db->getDriverName();
if($driver==='mysql')
$blob='LONGBLOB';
else if($driver==='pgsql')
$blob='BYTEA';
else
$blob='BLOB';
$sql=<<<EOD
CREATE TABLE $tableName
(
id CHAR(128) PRIMARY KEY,
expire INTEGER,
value $blob
)
EOD;
$db->createCommand($sql)->execute();
}
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
public function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
else if(($id=$this->connectionID)!==null)
{
if(($this->_db=Yii::app()->getComponent($id)) instanceof CDbConnection)
return $this->_db;
else
throw new CException(Yii::t('yii','CDbCache.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
array('{id}'=>$id)));
}
else
{
$dbFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache-'.Yii::getVersion().'.db';
return $this->_db=new CDbConnection('sqlite:'.$dbFile);
}
}
/**
* Sets the DB connection used by the cache component.
* @param CDbConnection $value the DB connection instance
* @since 1.1.5
*/
public function setDbConnection($value)
{
$this->_db=$value;
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$time=time();
$sql="SELECT value FROM {$this->cacheTableName} WHERE id='$key' AND (expire=0 OR expire>$time)";
$db=$this->getDbConnection();
if($db->queryCachingDuration>0)
{
$duration=$db->queryCachingDuration;
$db->queryCachingDuration=0;
$result=$db->createCommand($sql)->queryScalar();
$db->queryCachingDuration=$duration;
return $result;
}
else
return $db->createCommand($sql)->queryScalar();
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
if(empty($keys))
return array();
$ids=implode("','",$keys);
$time=time();
$sql="SELECT id, value FROM {$this->cacheTableName} WHERE id IN ('$ids') AND (expire=0 OR expire>$time)";
$db=$this->getDbConnection();
if($db->queryCachingDuration>0)
{
$duration=$db->queryCachingDuration;
$db->queryCachingDuration=0;
$rows=$db->createCommand($sql)->queryAll();
$db->queryCachingDuration=$duration;
}
else
$rows=$db->createCommand($sql)->queryAll();
$results=array();
foreach($keys as $key)
$results[$key]=false;
foreach($rows as $row)
$results[$row['id']]=$row['value'];
return $results;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
$this->deleteValue($key);
return $this->addValue($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
{
$this->gc();
$this->_gced=true;
}
if($expire>0)
$expire+=time();
else
$expire=0;
$sql="INSERT INTO {$this->cacheTableName} (id,expire,value) VALUES ('$key',$expire,:value)";
try
{
$command=$this->getDbConnection()->createCommand($sql);
$command->bindValue(':value',$value,PDO::PARAM_LOB);
$command->execute();
return true;
}
catch(Exception $e)
{
return false;
}
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
$sql="DELETE FROM {$this->cacheTableName} WHERE id='$key'";
$this->getDbConnection()->createCommand($sql)->execute();
return true;
}
/**
* Removes the expired data values.
*/
protected function gc()
{
$this->getDbConnection()->createCommand("DELETE FROM {$this->cacheTableName} WHERE expire>0 AND expire<".time())->execute();
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
$this->getDbConnection()->createCommand("DELETE FROM {$this->cacheTableName}")->execute();
return true;
}
}
<?php
/**
* CDummyCache class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDummyCache is a placeholder cache component.
*
* CDummyCache does not cache anything. It is provided so that one can always configure
* a 'cache' application component and he does not need to check if Yii::app()->cache is null or not.
* By replacing CDummyCache with some other cache component, one can quickly switch from
* non-caching mode to caching mode.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class CDummyCache extends CApplicationComponent implements ICache, ArrayAccess
{
/**
* @var string a string prefixed to every cache key so that it is unique. Defaults to {@link CApplication::getId() application ID}.
*/
public $keyPrefix;
/**
* Initializes the application component.
* This method overrides the parent implementation by setting default cache key prefix.
*/
public function init()
{
parent::init();
if($this->keyPrefix===null)
$this->keyPrefix=Yii::app()->getId();
}
/**
* Retrieves a value from cache with a specified key.
* @param string $id a key identifying the cached value
* @return mixed the value stored in cache, false if the value is not in the cache, expired or the dependency has changed.
*/
public function get($id)
{
return false;
}
/**
* Retrieves multiple values from cache with the specified keys.
* Some caches (such as memcache, apc) allow retrieving multiple cached values at one time,
* which may improve the performance since it reduces the communication cost.
* In case a cache doesn't support this feature natively, it will be simulated by this method.
* @param array $ids list of keys identifying the cached values
* @return array list of cached values corresponding to the specified keys. The array
* is returned in terms of (key,value) pairs.
* If a value is not cached or expired, the corresponding array value will be false.
*/
public function mget($ids)
{
$results=array();
foreach($ids as $id)
$results[$id]=false;
return $results;
}
/**
* Stores a value identified by a key into cache.
* If the cache already contains such a key, the existing value and
* expiration time will be replaced with the new ones.
*
* @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function set($id,$value,$expire=0,$dependency=null)
{
return true;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* Nothing will be done if the cache already contains the key.
* @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @param ICacheDependency $dependency dependency of the cached item. If the dependency changes, the item is labeled invalid.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
public function add($id,$value,$expire=0,$dependency=null)
{
return true;
}
/**
* Deletes a value with the specified key from cache
* @param string $id the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
public function delete($id)
{
return true;
}
/**
* Deletes all values from cache.
* Be careful of performing this operation if the cache is shared by multiple applications.
* @return boolean whether the flush operation was successful.
* @throws CException if this method is not overridden by child classes
*/
public function flush()
{
return true;
}
/**
* Returns whether there is a cache entry with a specified key.
* This method is required by the interface ArrayAccess.
* @param string $id a key identifying the cached value
* @return boolean
*/
public function offsetExists($id)
{
return false;
}
/**
* Retrieves the value from cache with a specified key.
* This method is required by the interface ArrayAccess.
* @param string $id a key identifying the cached value
* @return mixed the value stored in cache, false if the value is not in the cache or expired.
*/
public function offsetGet($id)
{
return false;
}
/**
* Stores the value identified by a key into cache.
* If the cache already contains such a key, the existing value will be
* replaced with the new ones. To add expiration and dependencies, use the set() method.
* This method is required by the interface ArrayAccess.
* @param string $id the key identifying the value to be cached
* @param mixed $value the value to be cached
*/
public function offsetSet($id, $value)
{
}
/**
* Deletes the value with the specified key from cache
* This method is required by the interface ArrayAccess.
* @param string $id the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
public function offsetUnset($id)
{
}
}
<?php
/**
* CEAcceleratorCache class file
*
* @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CEAcceleratorCache implements a cache application module based on {@link http://eaccelerator.net/ eaccelerator}.
*
* To use this application component, the eAccelerator PHP extension must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CEAccelerator.
*
* Please note that as of v0.9.6, eAccelerator no longer supports data caching.
* This means if you still want to use this component, your eAccelerator should be of 0.9.5.x or lower version.
*
* @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
* @version $Id$
* @package system.caching
*/
class CEAcceleratorCache extends CCache
{
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of eAccelerator.
* @throws CException if eAccelerator extension is not loaded, is disabled or the cache functions are not compiled in.
*/
public function init()
{
parent::init();
if(!function_exists('eaccelerator_get'))
throw new CException(Yii::t('yii','CEAcceleratorCache requires PHP eAccelerator extension to be loaded, enabled or compiled with the "--with-eaccelerator-shared-memory" option.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$result = eaccelerator_get($key);
return $result !== NULL ? $result : false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
return eaccelerator_put($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
return (NULL === eaccelerator_get($key)) ? $this->setValue($key,$value,$expire) : false;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return eaccelerator_rm($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
// first, remove expired content from cache
eaccelerator_gc();
// now, remove leftover cache-keys
$keys = eaccelerator_list_keys();
foreach($keys as $key)
$this->deleteValue(substr($key['name'], 1));
return true;
}
}
<?php
/**
* CFileCache class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CFileCache provides a file-based caching mechanism.
*
* For each data value being cached, CFileCache will use store it in a separate file
* under {@link cachePath} which defaults to 'protected/runtime/cache'.
* CFileCache will perform garbage collection automatically to remove expired cache files.
*
* See {@link CCache} manual for common cache operations that are supported by CFileCache.
*
* @property integer $gCProbability The probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
*/
class CFileCache extends CCache
{
/**
* @var string the directory to store cache files. Defaults to null, meaning
* using 'protected/runtime/cache' as the directory.
*/
public $cachePath;
/**
* @var string cache file suffix. Defaults to '.bin'.
*/
public $cacheFileSuffix='.bin';
/**
* @var integer the level of sub-directories to store cache files. Defaults to 0,
* meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+),
* you may want to set this value to be 1 or 2 so that the file system is not over burdened.
* The value of this property should not exceed 16 (less than 3 is recommended).
*/
public $directoryLevel=0;
private $_gcProbability=100;
private $_gced=false;
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
*/
public function init()
{
parent::init();
if($this->cachePath===null)
$this->cachePath=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'cache';
if(!is_dir($this->cachePath))
mkdir($this->cachePath,0777,true);
}
/**
* @return integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
*/
public function getGCProbability()
{
return $this->_gcProbability;
}
/**
* @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
*/
public function setGCProbability($value)
{
$value=(int)$value;
if($value<0)
$value=0;
if($value>1000000)
$value=1000000;
$this->_gcProbability=$value;
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
$this->gc(false);
return true;
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$cacheFile=$this->getCacheFile($key);
if(($time=@filemtime($cacheFile))>time())
return @file_get_contents($cacheFile);
else if($time>0)
@unlink($cacheFile);
return false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
{
$this->gc();
$this->_gced=true;
}
if($expire<=0)
$expire=31536000; // 1 year
$expire+=time();
$cacheFile=$this->getCacheFile($key);
if($this->directoryLevel>0)
@mkdir(dirname($cacheFile),0777,true);
if(@file_put_contents($cacheFile,$value,LOCK_EX)!==false)
{
@chmod($cacheFile,0777);
return @touch($cacheFile,$expire);
}
else
return false;
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
$cacheFile=$this->getCacheFile($key);
if(@filemtime($cacheFile)>time())
return false;
return $this->setValue($key,$value,$expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
$cacheFile=$this->getCacheFile($key);
return @unlink($cacheFile);
}
/**
* Returns the cache file path given the cache key.
* @param string $key cache key
* @return string the cache file path
*/
protected function getCacheFile($key)
{
if($this->directoryLevel>0)
{
$base=$this->cachePath;
for($i=0;$i<$this->directoryLevel;++$i)
{
if(($prefix=substr($key,$i+$i,2))!==false)
$base.=DIRECTORY_SEPARATOR.$prefix;
}
return $base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
}
else
return $this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
}
/**
* Removes expired cache files.
* @param boolean $expiredOnly whether only expired cache files should be removed.
* If false, all cache files under {@link cachePath} will be removed.
* @param string $path the path to clean with. If null, it will be {@link cachePath}.
*/
public function gc($expiredOnly=true,$path=null)
{
if($path===null)
$path=$this->cachePath;
if(($handle=opendir($path))===false)
return;
while(($file=readdir($handle))!==false)
{
if($file[0]==='.')
continue;
$fullPath=$path.DIRECTORY_SEPARATOR.$file;
if(is_dir($fullPath))
$this->gc($expiredOnly,$fullPath);
else if($expiredOnly && @filemtime($fullPath)<time() || !$expiredOnly)
@unlink($fullPath);
}
closedir($handle);
}
}
<?php
/**
* CMemCache class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CMemCache implements a cache application component based on {@link http://memcached.org/ memcached}.
*
* CMemCache can be configured with a list of memcache servers by settings
* its {@link setServers servers} property. By default, CMemCache assumes
* there is a memcache server running on localhost at port 11211.
*
* See {@link CCache} manual for common cache operations that are supported by CMemCache.
*
* Note, there is no security measure to protected data in memcache.
* All data in memcache can be accessed by any process running in the system.
*
* To use CMemCache as the cache application component, configure the application as follows,
* <pre>
* array(
* 'components'=>array(
* 'cache'=>array(
* 'class'=>'CMemCache',
* 'servers'=>array(
* array(
* 'host'=>'server1',
* 'port'=>11211,
* 'weight'=>60,
* ),
* array(
* 'host'=>'server2',
* 'port'=>11211,
* 'weight'=>40,
* ),
* ),
* ),
* ),
* )
* </pre>
* In the above, two memcache servers are used: server1 and server2.
* You can configure more properties of every server, including:
* host, port, persistent, weight, timeout, retryInterval, status.
* See {@link http://www.php.net/manual/en/function.memcache-addserver.php}
* for more details.
*
* CMemCache can also be used with {@link http://pecl.php.net/package/memcached memcached}.
* To do so, set {@link useMemcached} to be true.
*
* @property mixed $memCache The memcache instance (or memcached if {@link useMemcached} is true) used by this component.
* @property array $servers List of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class CMemCache extends CCache
{
/**
* @var boolean whether to use memcached or memcache as the underlying caching extension.
* If true {@link http://pecl.php.net/package/memcached memcached} will be used.
* If false {@link http://pecl.php.net/package/memcache memcache}. will be used.
* Defaults to false.
*/
public $useMemcached=false;
/**
* @var Memcache the Memcache instance
*/
private $_cache=null;
/**
* @var array list of memcache server configurations
*/
private $_servers=array();
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It creates the memcache instance and adds memcache servers.
* @throws CException if memcache extension is not loaded
*/
public function init()
{
parent::init();
$servers=$this->getServers();
$cache=$this->getMemCache();
if(count($servers))
{
foreach($servers as $server)
{
if($this->useMemcached)
$cache->addServer($server->host,$server->port,$server->weight);
else
$cache->addServer($server->host,$server->port,$server->persistent,$server->weight,$server->timeout,$server->status);
}
}
else
$cache->addServer('localhost',11211);
}
/**
* @throws CException if extension isn't loaded
* @return Memcache|Memcached the memcache instance (or memcached if {@link useMemcached} is true) used by this component.
*/
public function getMemCache()
{
if($this->_cache!==null)
return $this->_cache;
else
{
$extension=$this->useMemcached ? 'memcached' : 'memcache';
if(!extension_loaded($extension))
throw new CException(Yii::t('yii',"CMemCache requires PHP {extension} extension to be loaded.",
array('{extension}'=>$extension)));
return $this->_cache=$this->useMemcached ? new Memcached : new Memcache;
}
}
/**
* @return array list of memcache server configurations. Each element is a {@link CMemCacheServerConfiguration}.
*/
public function getServers()
{
return $this->_servers;
}
/**
* @param array $config list of memcache server configurations. Each element must be an array
* with the following keys: host, port, persistent, weight, timeout, retryInterval, status.
* @see http://www.php.net/manual/en/function.Memcache-addServer.php
*/
public function setServers($config)
{
foreach($config as $c)
$this->_servers[]=new CMemCacheServerConfiguration($c);
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return $this->_cache->get($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return $this->useMemcached ? $this->_cache->getMulti($keys) : $this->_cache->get($keys);
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
if($expire>0)
$expire+=time();
else
$expire=0;
return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
if($expire>0)
$expire+=time();
else
$expire=0;
return $this->useMemcached ? $this->_cache->add($key,$value,$expire) : $this->_cache->add($key,$value,0,$expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return $this->_cache->delete($key, 0);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
return $this->_cache->flush();
}
}
/**
* CMemCacheServerConfiguration represents the configuration data for a single memcache server.
*
* See {@link http://www.php.net/manual/en/function.Memcache-addServer.php}
* for detailed explanation of each configuration property.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching
* @since 1.0
*/
class CMemCacheServerConfiguration extends CComponent
{
/**
* @var string memcache server hostname or IP address
*/
public $host;
/**
* @var integer memcache server port
*/
public $port=11211;
/**
* @var boolean whether to use a persistent connection
*/
public $persistent=true;
/**
* @var integer probability of using this server among all servers.
*/
public $weight=1;
/**
* @var integer value in seconds which will be used for connecting to the server
*/
public $timeout=15;
/**
* @var integer how often a failed server will be retried (in seconds)
*/
public $retryInterval=15;
/**
* @var boolean if the server should be flagged as online upon a failure
*/
public $status=true;
/**
* Constructor.
* @param array $config list of memcache server configurations.
* @throws CException if the configuration is not an array
*/
public function __construct($config)
{
if(is_array($config))
{
foreach($config as $key=>$value)
$this->$key=$value;
if($this->host===null)
throw new CException(Yii::t('yii','CMemCache server configuration must have "host" value.'));
}
else
throw new CException(Yii::t('yii','CMemCache server configuration must be an array.'));
}
}
<?php
/**
* CWinCache class file
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CWinCache implements a cache application component based on {@link http://www.iis.net/expand/wincacheforphp WinCache}.
*
* To use this application component, the WinCache PHP extension must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CWinCache.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @version $Id$
* @package system.caching
* @since 1.1.2
*/
class CWinCache extends CCache {
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of WinCache extension and WinCache user cache.
* @throws CException if WinCache extension is not loaded or user cache is disabled
*/
public function init()
{
parent::init();
if(!extension_loaded('wincache'))
throw new CException(Yii::t('yii', 'CWinCache requires PHP wincache extension to be loaded.'));
if(!ini_get('wincache.ucenabled'))
throw new CException(Yii::t('yii', 'CWinCache user cache is disabled. Please set wincache.ucenabled to On in your php.ini.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return wincache_ucache_get($key);
}
/**
* Retrieves multiple values from cache with the specified keys.
* @param array $keys a list of keys identifying the cached values
* @return array a list of cached values indexed by the keys
*/
protected function getValues($keys)
{
return wincache_ucache_get($keys);
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
return wincache_ucache_set($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
return wincache_ucache_add($key,$value,$expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return wincache_ucache_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
return wincache_ucache_clear();
}
}
\ No newline at end of file
<?php
/**
* CXCache class file
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CXCache implements a cache application module based on {@link http://xcache.lighttpd.net/ xcache}.
*
* To use this application component, the XCache PHP extension must be loaded.
* Flush functionality will only work correctly if "xcache.admin.enable_auth" is set to "Off" in php.ini.
*
* See {@link CCache} manual for common cache operations that are supported by CXCache.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @version $Id$
* @package system.caching
*/
class CXCache extends CCache
{
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of memcache.
* @throws CException if memcache extension is not loaded or is disabled.
*/
public function init()
{
parent::init();
if(!function_exists('xcache_isset'))
throw new CException(Yii::t('yii','CXCache requires PHP XCache extension to be loaded.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
return xcache_isset($key) ? xcache_get($key) : false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
return xcache_set($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
return !xcache_isset($key) ? $this->setValue($key,$value,$expire) : false;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return xcache_unset($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
for($i=0, $max=xcache_count(XC_TYPE_VAR); $i<$max; $i++)
{
if(xcache_clear_cache(XC_TYPE_VAR, $i)===false)
return false;
}
return true;
}
}
<?php
/**
* CZendDataCache class file
*
* @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CZendDataCache implements a cache application module based on the Zend Data Cache
* delivered with {@link http://www.zend.com/en/products/server/ ZendServer}.
*
* To use this application component, the Zend Data Cache PHP extension must be loaded.
*
* See {@link CCache} manual for common cache operations that are supported by CZendDataCache.
*
* @author Steffen Dietz <steffo.dietz[at]googlemail[dot]com>
* @version $Id$
* @package system.caching
*/
class CZendDataCache extends CCache
{
/**
* Initializes this application component.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of Zend Data Cache.
* @throws CException if Zend Data Cache extension is not loaded.
*/
public function init()
{
parent::init();
if(!function_exists('zend_shm_cache_store'))
throw new CException(Yii::t('yii','CZendDataCache requires PHP Zend Data Cache extension to be loaded.'));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key)
{
$result = zend_shm_cache_fetch($key);
return $result !== NULL ? $result : false;
}
/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key,$value,$expire)
{
return zend_shm_cache_store($key,$value,$expire);
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
*
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key,$value,$expire)
{
return (NULL === zend_shm_cache_fetch($key)) ? $this->setValue($key,$value,$expire) : false;
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key)
{
return zend_shm_cache_delete($key);
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
return zend_shm_cache_clear();
}
}
<?php
/**
* CCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CCacheDependency is the base class for cache dependency classes.
*
* CCacheDependency implements the {@link ICacheDependency} interface.
* Child classes should override its {@link generateDependentData} for
* actual dependency checking.
*
* @property boolean $hasChanged Whether the dependency has changed.
* @property mixed $dependentData The data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CCacheDependency extends CComponent implements ICacheDependency
{
/**
* @var boolean Whether this dependency is reusable or not.
* If set to true, dependent data for this cache dependency will only be generated once per request.
* You can then use the same cache dependency for multiple separate cache calls on the same page
* without the overhead of re-evaluating the dependency each time.
* Defaults to false;
* @since 1.1.11
*/
public $reuseDependentData=false;
/**
* @var array cached data for reusable dependencies.
* @since 1.1.11
*/
private static $_reusableData=array();
private $_hash;
private $_data;
/**
* Evaluates the dependency by generating and saving the data related with dependency.
* This method is invoked by cache before writing data into it.
*/
public function evaluateDependency()
{
if ($this->reuseDependentData)
{
$hash=$this->getHash();
if (!isset(self::$_reusableData[$hash]['dependentData']))
self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
$this->_data=self::$_reusableData[$hash]['dependentData'];
}
else
$this->_data=$this->generateDependentData();
}
/**
* @return boolean whether the dependency has changed.
*/
public function getHasChanged()
{
if ($this->reuseDependentData)
{
$hash=$this->getHash();
if (!isset(self::$_reusableData[$hash]['hasChanged']))
{
if (!isset(self::$_reusableData[$hash]['dependentData']))
self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
self::$_reusableData[$hash]['hasChanged']=self::$_reusableData[$hash]['dependentData']!=$this->_data;
}
return self::$_reusableData[$hash]['hasChanged'];
}
else
return $this->generateDependentData()!=$this->_data;
}
/**
* @return mixed the data used to determine if dependency has been changed.
* This data is available after {@link evaluateDependency} is called.
*/
public function getDependentData()
{
return $this->_data;
}
/**
* Generates the data needed to determine if dependency has been changed.
* Derived classes should override this method to generate actual dependent data.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
return null;
}
/**
* Generates a unique hash that identifies this cache dependency.
* @return string the hash for this cache dependency
*/
private function getHash()
{
if($this->_hash===null)
$this->_hash=sha1(serialize($this));
return $this->_hash;
}
}
\ No newline at end of file
<?php
/**
* CChainedCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CChainedCacheDependency represents a list of cache dependencies.
*
* If any of the dependencies reports a dependency change, CChainedCacheDependency
* will return true for the checking.
*
* To add dependencies to CChainedCacheDependency, use {@link getDependencies Dependencies}
* which gives a {@link CTypedList} instance and can be used like an array
* (see {@link CList} for more details}).
*
* @property CTypedList $dependencies List of dependency objects.
* @property boolean $hasChanged Whether the dependency is changed or not.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CChainedCacheDependency extends CComponent implements ICacheDependency
{
private $_dependencies=null;
/**
* Constructor.
* @param array $dependencies the dependencies to be added to this chain.
* @since 1.1.4
*/
public function __construct($dependencies=array())
{
if(!empty($dependencies))
$this->setDependencies($dependencies);
}
/**
* @return CTypedList list of dependency objects
*/
public function getDependencies()
{
if($this->_dependencies===null)
$this->_dependencies=new CTypedList('ICacheDependency');
return $this->_dependencies;
}
/**
* @param array $values list of dependency objects or configurations to be added to this chain.
* If a depedency is specified as a configuration, it must be an array that can be recognized
* by {@link YiiBase::createComponent}.
*/
public function setDependencies($values)
{
$dependencies=$this->getDependencies();
foreach($values as $value)
{
if(is_array($value))
$value=Yii::createComponent($value);
$dependencies->add($value);
}
}
/**
* Evaluates the dependency by generating and saving the data related with dependency.
*/
public function evaluateDependency()
{
if($this->_dependencies!==null)
{
foreach($this->_dependencies as $dependency)
$dependency->evaluateDependency();
}
}
/**
* Performs the actual dependency checking.
* This method returns true if any of the dependency objects
* reports a dependency change.
* @return boolean whether the dependency is changed or not.
*/
public function getHasChanged()
{
if($this->_dependencies!==null)
{
foreach($this->_dependencies as $dependency)
if($dependency->getHasChanged())
return true;
}
return false;
}
}
<?php
/**
* CDbCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDbCacheDependency represents a dependency based on the query result of a SQL statement.
*
* If the query result (a scalar) changes, the dependency is considered as changed.
* To specify the SQL statement, set {@link sql} property.
* The {@link connectionID} property specifies the ID of a {@link CDbConnection} application
* component. It is this DB connection that is used to perform the query.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CDbCacheDependency extends CCacheDependency
{
/**
* @var string the ID of a {@link CDbConnection} application component. Defaults to 'db'.
*/
public $connectionID='db';
/**
* @var string the SQL statement whose result is used to determine if the dependency has been changed.
* Note, the SQL statement should return back a single value.
*/
public $sql;
/**
* @var array parameters (name=>value) to be bound to the SQL statement specified by {@link sql}.
* @since 1.1.4
*/
public $params;
private $_db;
/**
* Constructor.
* @param string $sql the SQL statement whose result is used to determine if the dependency has been changed.
*/
public function __construct($sql=null)
{
$this->sql=$sql;
}
/**
* PHP sleep magic method.
* This method ensures that the database instance is set null because it contains resource handles.
* @return array
*/
public function __sleep()
{
$this->_db=null;
return array_keys((array)$this);
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
if($this->sql!==null)
{
$db=$this->getDbConnection();
$command=$db->createCommand($this->sql);
if(is_array($this->params))
{
foreach($this->params as $name=>$value)
$command->bindValue($name,$value);
}
if($db->queryCachingDuration>0)
{
// temporarily disable and re-enable query caching
$duration=$db->queryCachingDuration;
$db->queryCachingDuration=0;
$result=$command->queryRow();
$db->queryCachingDuration=$duration;
}
else
$result=$command->queryRow();
return $result;
}
else
throw new CException(Yii::t('yii','CDbCacheDependency.sql cannot be empty.'));
}
/**
* @return CDbConnection the DB connection instance
* @throws CException if {@link connectionID} does not point to a valid application component.
*/
protected function getDbConnection()
{
if($this->_db!==null)
return $this->_db;
else
{
if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
return $this->_db;
else
throw new CException(Yii::t('yii','CDbCacheDependency.connectionID "{id}" is invalid. Please make sure it refers to the ID of a CDbConnection application component.',
array('{id}'=>$this->connectionID)));
}
}
}
<?php
/**
* CDirectoryCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CDirectoryCacheDependency represents a dependency based on change of a directory.
*
* CDirectoryCacheDependency performs dependency checking based on the
* modification time of the files contained in the specified directory.
* The directory being checked is specified via {@link directory}.
*
* By default, all files under the specified directory and subdirectories
* will be checked. If the last modification time of any of them is changed
* or if different number of files are contained in a directory, the dependency
* is reported as changed. By specifying {@link recursiveLevel},
* one can limit the checking to a certain depth of the directory.
*
* Note, dependency checking for a directory is expensive because it involves
* accessing modification time of multiple files under the directory.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CDirectoryCacheDependency extends CCacheDependency
{
/**
* @var string the directory whose change is used to determine if the dependency has been changed.
* If any of the files under the directory is changed, the dependency is considered as changed.
*/
public $directory;
/**
* @var integer the depth of the subdirectories to be recursively checked.
* If the value is less than 0, it means unlimited depth.
* If the value is 0, it means checking the files directly under the specified directory.
*/
public $recursiveLevel=-1;
/**
* @var string the regular expression matching valid file/directory names.
* Only the matching files or directories will be checked for changes.
* Defaults to null, meaning all files/directories will qualify.
*/
public $namePattern;
/**
* Constructor.
* @param string $directory the directory to be checked
*/
public function __construct($directory=null)
{
$this->directory=$directory;
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the modification timestamps for files under the directory.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
if($this->directory!==null)
return $this->generateTimestamps($this->directory);
else
throw new CException(Yii::t('yii','CDirectoryCacheDependency.directory cannot be empty.'));
}
/**
* Determines the last modification time for files under the directory.
* This method may go recursively into subdirectories if {@link recursiveLevel} is not 0.
* @param string $directory the directory name
* @param integer $level level of the recursion
* @return array list of file modification time indexed by the file path
*/
protected function generateTimestamps($directory,$level=0)
{
if(($dir=@opendir($directory))===false)
throw new CException(Yii::t('yii','"{path}" is not a valid directory.',
array('{path}'=>$directory)));
$timestamps=array();
while(($file=readdir($dir))!==false)
{
$path=$directory.DIRECTORY_SEPARATOR.$file;
if($file==='.' || $file==='..')
continue;
if($this->namePattern!==null && !preg_match($this->namePattern,$file))
continue;
if(is_file($path))
{
if($this->validateFile($path))
$timestamps[$path]=filemtime($path);
}
else
{
if(($this->recursiveLevel<0 || $level<$this->recursiveLevel) && $this->validateDirectory($path))
$timestamps=array_merge($timestamps, $this->generateTimestamps($path,$level+1));
}
}
closedir($dir);
return $timestamps;
}
/**
* Checks to see if the file should be checked for dependency.
* This method is invoked when dependency of the whole directory is being checked.
* By default, it always returns true, meaning the file should be checked.
* You may override this method to check only certain files.
* @param string $fileName the name of the file that may be checked for dependency.
* @return boolean whether this file should be checked.
*/
protected function validateFile($fileName)
{
return true;
}
/**
* Checks to see if the specified subdirectory should be checked for dependency.
* This method is invoked when dependency of the whole directory is being checked.
* By default, it always returns true, meaning the subdirectory should be checked.
* You may override this method to check only certain subdirectories.
* @param string $directory the name of the subdirectory that may be checked for dependency.
* @return boolean whether this subdirectory should be checked.
*/
protected function validateDirectory($directory)
{
return true;
}
}
<?php
/**
* CExpressionDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CExpressionDependency represents a dependency based on the result of a PHP expression.
*
* CExpressionDependency performs dependency checking based on the
* result of a PHP {@link expression}.
* The dependency is reported as unchanged if and only if the result is
* the same as the one evaluated when storing the data to cache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CExpressionDependency extends CCacheDependency
{
/**
* @var string the PHP expression whose result is used to determine the dependency.
* The expression can also be a valid serializable PHP callback.
* The callback will be passed with a parameter which is the dependency object itself.
*/
public $expression;
/**
* Constructor.
* @param string $expression the PHP expression whose result is used to determine the dependency.
*/
public function __construct($expression='true')
{
$this->expression=$expression;
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the result of the PHP expression.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
return $this->evaluateExpression($this->expression);
}
}
<?php
/**
* CFileCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CFileCacheDependency represents a dependency based on a file's last modification time.
*
* CFileCacheDependency performs dependency checking based on the
* last modification time of the file specified via {@link fileName}.
* The dependency is reported as unchanged if and only if the file's
* last modification time remains unchanged.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CFileCacheDependency extends CCacheDependency
{
/**
* @var string the name of the file whose last modification time is used to
* check if the dependency has been changed.
*/
public $fileName;
/**
* Constructor.
* @param string $fileName name of the file whose change is to be checked.
*/
public function __construct($fileName=null)
{
$this->fileName=$fileName;
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the file's last modification time.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
if($this->fileName!==null)
return @filemtime($this->fileName);
else
throw new CException(Yii::t('yii','CFileCacheDependency.fileName cannot be empty.'));
}
}
<?php
/**
* CGlobalStateCacheDependency class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CGlobalStateCacheDependency represents a dependency based on a global state value.
*
* CGlobalStateCacheDependency checks if a global state is changed or not.
* If the global state is changed, the dependency is reported as changed.
* To specify which global state this dependency should check with,
* set {@link stateName} to the name of the global state.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.caching.dependencies
* @since 1.0
*/
class CGlobalStateCacheDependency extends CCacheDependency
{
/**
* @var string the name of the global state whose value is to check
* if the dependency has changed.
* @see CApplication::setGlobalState
*/
public $stateName;
/**
* Constructor.
* @param string $name the name of the global state
*/
public function __construct($name=null)
{
$this->stateName=$name;
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the value of the global state.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependentData()
{
if($this->stateName!==null)
return Yii::app()->getGlobalState($this->stateName);
else
throw new CException(Yii::t('yii','CGlobalStateCacheDependency.stateName cannot be empty.'));
}
}
<?php
/**
* MessageCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* MessageCommand extracts messages to be translated from source files.
* The extracted messages are saved as PHP message source files
* under the specified directory.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands
* @since 1.0
*/
class MessageCommand extends CConsoleCommand
{
public function getHelp()
{
return <<<EOD
USAGE
yiic message <config-file>
DESCRIPTION
This command searches for messages to be translated in the specified
source files and compiles them into PHP arrays as message source.
PARAMETERS
* config-file: required, the path of the configuration file. You can find
an example in framework/messages/config.php.
The file can be placed anywhere and must be a valid PHP script which
returns an array of name-value pairs. Each name-value pair represents
a configuration option.
The following options are available:
- sourcePath: string, root directory of all source files.
- messagePath: string, root directory containing message translations.
- languages: array, list of language codes that the extracted messages
should be translated to. For example, array('zh_cn','en_au').
- fileTypes: array, a list of file extensions (e.g. 'php', 'xml').
Only the files whose extension name can be found in this list
will be processed. If empty, all files will be processed.
- exclude: array, a list of directory and file exclusions. Each
exclusion can be either a name or a path. If a file or directory name
or path matches the exclusion, it will not be copied. For example,
an exclusion of '.svn' will exclude all files and directories whose
name is '.svn'. And an exclusion of '/a/b' will exclude file or
directory 'sourcePath/a/b'.
- translator: the name of the function for translating messages.
Defaults to 'Yii::t'. This is used as a mark to find messages to be
translated.
- overwrite: if message file must be overwritten with the merged messages.
- removeOld: if message no longer needs translation it will be removed,
instead of being enclosed between a pair of '@@' marks.
- sort: sort messages by key when merging, regardless of their translation
state (new, obsolete, translated.)
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
*/
public function run($args)
{
if(!isset($args[0]))
$this->usageError('the configuration file is not specified.');
if(!is_file($args[0]))
$this->usageError("the configuration file {$args[0]} does not exist.");
$config=require_once($args[0]);
$translator='Yii::t';
extract($config);
if(!isset($sourcePath,$messagePath,$languages))
$this->usageError('The configuration file must specify "sourcePath", "messagePath" and "languages".');
if(!is_dir($sourcePath))
$this->usageError("The source path $sourcePath is not a valid directory.");
if(!is_dir($messagePath))
$this->usageError("The message path $messagePath is not a valid directory.");
if(empty($languages))
$this->usageError("Languages cannot be empty.");
if(!isset($overwrite))
$overwrite = false;
if(!isset($removeOld))
$removeOld = false;
if(!isset($sort))
$sort = false;
$options=array();
if(isset($fileTypes))
$options['fileTypes']=$fileTypes;
if(isset($exclude))
$options['exclude']=$exclude;
$files=CFileHelper::findFiles(realpath($sourcePath),$options);
$messages=array();
foreach($files as $file)
$messages=array_merge_recursive($messages,$this->extractMessages($file,$translator));
foreach($languages as $language)
{
$dir=$messagePath.DIRECTORY_SEPARATOR.$language;
if(!is_dir($dir))
@mkdir($dir);
foreach($messages as $category=>$msgs)
{
$msgs=array_values(array_unique($msgs));
$this->generateMessageFile($msgs,$dir.DIRECTORY_SEPARATOR.$category.'.php',$overwrite,$removeOld,$sort);
}
}
}
protected function extractMessages($fileName,$translator)
{
echo "Extracting messages from $fileName...\n";
$subject=file_get_contents($fileName);
$n=preg_match_all('/\b'.$translator.'\s*\(\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*,\s*(\'.*?(?<!\\\\)\'|".*?(?<!\\\\)")\s*[,\)]/s',$subject,$matches,PREG_SET_ORDER);
$messages=array();
for($i=0;$i<$n;++$i)
{
if(($pos=strpos($matches[$i][1],'.'))!==false)
$category=substr($matches[$i][1],$pos+1,-1);
else
$category=substr($matches[$i][1],1,-1);
$message=$matches[$i][2];
$messages[$category][]=eval("return $message;"); // use eval to eliminate quote escape
}
return $messages;
}
protected function generateMessageFile($messages,$fileName,$overwrite,$removeOld,$sort)
{
echo "Saving messages to $fileName...";
if(is_file($fileName))
{
$translated=require($fileName);
sort($messages);
ksort($translated);
if(array_keys($translated)==$messages)
{
echo "nothing new...skipped.\n";
return;
}
$merged=array();
$untranslated=array();
foreach($messages as $message)
{
if(!empty($translated[$message]))
$merged[$message]=$translated[$message];
else
$untranslated[]=$message;
}
ksort($merged);
sort($untranslated);
$todo=array();
foreach($untranslated as $message)
$todo[$message]='';
ksort($translated);
foreach($translated as $message=>$translation)
{
if(!isset($merged[$message]) && !isset($todo[$message]) && !$removeOld)
{
if(substr($translation,0,2)==='@@' && substr($translation,-2)==='@@')
$todo[$message]=$translation;
else
$todo[$message]='@@'.$translation.'@@';
}
}
$merged=array_merge($todo,$merged);
if($sort)
ksort($merged);
if($overwrite === false)
$fileName.='.merged';
echo "translation merged.\n";
}
else
{
$merged=array();
foreach($messages as $message)
$merged[$message]='';
ksort($merged);
echo "saved.\n";
}
$array=str_replace("\r",'',var_export($merged,true));
$content=<<<EOD
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yiic message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE, this file must be saved in UTF-8 encoding.
*
* @version \$Id: \$
*/
return $array;
EOD;
file_put_contents($fileName, $content);
}
}
<?php
/**
* ShellCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* ShellCommand executes the specified Web application and provides a shell for interaction.
*
* @property string $help The help information for the shell command.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands
* @since 1.0
*/
class ShellCommand extends CConsoleCommand
{
/**
* @return string the help information for the shell command
*/
public function getHelp()
{
return <<<EOD
USAGE
yiic shell [entry-script | config-file]
DESCRIPTION
This command allows you to interact with a Web application
on the command line. It also provides tools to automatically
generate new controllers, views and data models.
It is recommended that you execute this command under
the directory that contains the entry script file of
the Web application.
PARAMETERS
* entry-script | config-file: optional, the path to
the entry script file or the configuration file for
the Web application. If not given, it is assumed to be
the 'index.php' file under the current directory.
EOD;
}
/**
* Execute the action.
* @param array $args command line parameters specific for this command
*/
public function run($args)
{
if(!isset($args[0]))
$args[0]='index.php';
$entryScript=isset($args[0]) ? $args[0] : 'index.php';
if(($entryScript=realpath($args[0]))===false || !is_file($entryScript))
$this->usageError("{$args[0]} does not exist or is not an entry script file.");
// fake the web server setting
$cwd=getcwd();
chdir(dirname($entryScript));
$_SERVER['SCRIPT_NAME']='/'.basename($entryScript);
$_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME'];
$_SERVER['SCRIPT_FILENAME']=$entryScript;
$_SERVER['HTTP_HOST']='localhost';
$_SERVER['SERVER_NAME']='localhost';
$_SERVER['SERVER_PORT']=80;
// reset context to run the web application
restore_error_handler();
restore_exception_handler();
Yii::setApplication(null);
Yii::setPathOfAlias('application',null);
ob_start();
$config=require($entryScript);
ob_end_clean();
// oops, the entry script turns out to be a config file
if(is_array($config))
{
chdir($cwd);
$_SERVER['SCRIPT_NAME']='/index.php';
$_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME'];
$_SERVER['SCRIPT_FILENAME']=$cwd.DIRECTORY_SEPARATOR.'index.php';
Yii::createWebApplication($config);
}
restore_error_handler();
restore_exception_handler();
$yiiVersion=Yii::getVersion();
echo <<<EOD
Yii Interactive Tool v1.1 (based on Yii v{$yiiVersion})
Please type 'help' for help. Type 'exit' to quit.
EOD;
$this->runShell();
}
protected function runShell()
{
// disable E_NOTICE so that the shell is more friendly
error_reporting(E_ALL ^ E_NOTICE);
$_runner_=new CConsoleCommandRunner;
$_runner_->addCommands(dirname(__FILE__).'/shell');
$_runner_->addCommands(Yii::getPathOfAlias('application.commands.shell'));
if(($_path_=@getenv('YIIC_SHELL_COMMAND_PATH'))!==false)
$_runner_->addCommands($_path_);
$_commands_=$_runner_->commands;
$log=Yii::app()->log;
while(($_line_=$this->prompt("\n>>"))!==false)
{
$_line_=trim($_line_);
if($_line_==='exit')
return;
try
{
$_args_=preg_split('/[\s,]+/',rtrim($_line_,';'),-1,PREG_SPLIT_NO_EMPTY);
if(isset($_args_[0]) && isset($_commands_[$_args_[0]]))
{
$_command_=$_runner_->createCommand($_args_[0]);
array_shift($_args_);
$_command_->init();
$_command_->run($_args_);
}
else
echo eval($_line_.';');
}
catch(Exception $e)
{
if($e instanceof ShellException)
echo $e->getMessage();
else
echo $e;
}
}
}
}
class ShellException extends CException
{
}
\ No newline at end of file
<?php
/**
* WebAppCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* WebAppCommand creates an Yii Web application at the specified location.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands
* @since 1.0
*/
class WebAppCommand extends CConsoleCommand
{
private $_rootPath;
public function getHelp()
{
return <<<EOD
USAGE
yiic webapp <app-path> [<vcs>]
DESCRIPTION
This command generates an Yii Web Application at the specified location.
PARAMETERS
* app-path: required, the directory where the new application will be created.
If the directory does not exist, it will be created. After the application
is created, please make sure the directory can be accessed by Web users.
* vcs: optional, version control system you're going to use in the new project.
Application generator will create all needed files to the specified VCS
(such as .gitignore, .gitkeep, etc.). Possible values: git, hg. Do not
use this argument if you're going to create VCS files yourself.
EOD;
}
/**
* Execute the action.
* @param array $args command line parameters specific for this command
*/
public function run($args)
{
$vcs=false;
if(isset($args[1]))
{
if($args[1]!='git' && $args[1]!='hg')
$this->usageError('Unsupported VCS specified. Currently only git and hg supported.');
$vcs=$args[1];
}
if(!isset($args[0]))
$this->usageError('the Web application location is not specified.');
$path=strtr($args[0],'/\\',DIRECTORY_SEPARATOR);
if(strpos($path,DIRECTORY_SEPARATOR)===false)
$path='.'.DIRECTORY_SEPARATOR.$path;
if(basename($path)=='..')
$path.=DIRECTORY_SEPARATOR.'.';
$dir=rtrim(realpath(dirname($path)),'\\/');
if($dir===false || !is_dir($dir))
$this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists.");
if(basename($path)==='.')
$this->_rootPath=$path=$dir;
else
$this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path);
if($this->confirm("Create a Web application under '$path'?"))
{
$sourceDir=$this->getSourceDir();
if($sourceDir===false)
die("\nUnable to locate the source directory.\n");
$ignoreFiles=array();
$renameMap=array();
switch($vcs)
{
case 'git':
$renameMap=array('git.gitignore'=>'.gitignore','git.gitkeep'=>'.gitkeep'); // move with rename git files
$ignoreFiles=array('hg.hgignore','hg.hgkeep'); // ignore only hg files
break;
case 'hg':
$renameMap=array('hg.hgignore'=>'.hgignore','hg.hgkeep'=>'.hgkeep'); // move with rename hg files
$ignoreFiles=array('git.gitignore','git.gitkeep'); // ignore only git files
break;
default:
// no files for renaming
$ignoreFiles=array('git.gitignore','git.gitkeep','hg.hgignore','hg.hgkeep'); // ignore both git and hg files
break;
}
$list=$this->buildFileList($sourceDir,$path,'',$ignoreFiles,$renameMap);
$this->addFileModificationCallbacks($list);
$this->copyFiles($list);
$this->setPermissions($path);
echo "\nYour application has been created successfully under {$path}.\n";
}
}
/**
* Adjusts created application file and directory permissions
*
* @param string $targetDir path to created application
*/
protected function setPermissions($targetDir)
{
@chmod($targetDir.'/assets',0777);
@chmod($targetDir.'/protected/runtime',0777);
@chmod($targetDir.'/protected/data',0777);
@chmod($targetDir.'/protected/data/testdrive.db',0777);
@chmod($targetDir.'/protected/yiic',0755);
}
/**
* @return string path to application bootstrap source files
*/
protected function getSourceDir()
{
return realpath(dirname(__FILE__).'/../views/webapp');
}
/**
* Adds callbacks that will modify source files
*
* @param array $fileList
*/
protected function addFileModificationCallbacks(&$fileList)
{
$fileList['index.php']['callback']=array($this,'generateIndex');
$fileList['index-test.php']['callback']=array($this,'generateIndex');
$fileList['protected/tests/bootstrap.php']['callback']=array($this,'generateTestBoostrap');
$fileList['protected/yiic.php']['callback']=array($this,'generateYiic');
}
/**
* Inserts path to framework's yii.php into application's index.php
*
* @param string $source source file path
* @param array $params
* @return string modified source file content
*/
public function generateIndex($source,$params)
{
$content=file_get_contents($source);
$yii=realpath(dirname(__FILE__).'/../../yii.php');
$yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'index.php');
$yii=str_replace('\\','\\\\',$yii);
return preg_replace('/\$yii\s*=(.*?);/',"\$yii=$yii;",$content);
}
/**
* Inserts path to framework's yiit.php into application's index-test.php
*
* @param string $source source file path
* @param array $params
* @return string modified source file content
*/
public function generateTestBoostrap($source,$params)
{
$content=file_get_contents($source);
$yii=realpath(dirname(__FILE__).'/../../yiit.php');
$yii=$this->getRelativePath($yii,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'bootstrap.php');
$yii=str_replace('\\','\\\\',$yii);
return preg_replace('/\$yiit\s*=(.*?);/',"\$yiit=$yii;",$content);
}
/**
* Inserts path to framework's yiic.php into application's yiic.php
*
* @param string $source source file path
* @param array $params
* @return string modified source file content
*/
public function generateYiic($source,$params)
{
$content=file_get_contents($source);
$yiic=realpath(dirname(__FILE__).'/../../yiic.php');
$yiic=$this->getRelativePath($yiic,$this->_rootPath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'yiic.php');
$yiic=str_replace('\\','\\\\',$yiic);
return preg_replace('/\$yiic\s*=(.*?);/',"\$yiic=$yiic;",$content);
}
/**
* Returns variant of $path1 relative to $path2
*
* @param string $path1
* @param string $path2
* @return string $path1 relative to $path2
*/
protected function getRelativePath($path1,$path2)
{
$segs1=explode(DIRECTORY_SEPARATOR,$path1);
$segs2=explode(DIRECTORY_SEPARATOR,$path2);
$n1=count($segs1);
$n2=count($segs2);
for($i=0;$i<$n1 && $i<$n2;++$i)
{
if($segs1[$i]!==$segs2[$i])
break;
}
if($i===0)
return "'".$path1."'";
$up='';
for($j=$i;$j<$n2-1;++$j)
$up.='/..';
for(;$i<$n1-1;++$i)
$up.='/'.$segs1[$i];
return 'dirname(__FILE__).\''.$up.'/'.basename($path1).'\'';
}
}
\ No newline at end of file
<?php
/**
* ControllerCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* ControllerCommand generates a controller class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands.shell
* @since 1.0
*/
class ControllerCommand extends CConsoleCommand
{
/**
* @var string the directory that contains templates for the model command.
* Defaults to null, meaning using 'framework/cli/views/shell/controller'.
* If you set this path and some views are missing in the directory,
* the default views will be used.
*/
public $templatePath;
public function getHelp()
{
return <<<EOD
USAGE
controller <controller-ID> [action-ID] ...
DESCRIPTION
This command generates a controller and views associated with
the specified actions.
PARAMETERS
* controller-ID: required, controller ID, e.g., 'post'.
If the controller should be located under a subdirectory,
please specify the controller ID as 'path/to/ControllerID',
e.g., 'admin/user'.
If the controller belongs to a module, please specify
the controller ID as 'ModuleID/ControllerID' or
'ModuleID/path/to/Controller' (assuming the controller is
under a subdirectory of that module).
* action-ID: optional, action ID. You may supply one or several
action IDs. A default 'index' action will always be generated.
EXAMPLES
* Generates the 'post' controller:
controller post
* Generates the 'post' controller with additional actions 'contact'
and 'about':
controller post contact about
* Generates the 'post' controller which should be located under
the 'admin' subdirectory of the base controller path:
controller admin/post
* Generates the 'post' controller which should belong to
the 'admin' module:
controller admin/post
NOTE: in the last two examples, the commands are the same, but
the generated controller file is located under different directories.
Yii is able to detect whether 'admin' refers to a module or a subdirectory.
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: controller name is required.\n";
echo $this->getHelp();
return 1;
}
$module=Yii::app();
$controllerID=$args[0];
if(($pos=strrpos($controllerID,'/'))===false)
{
$controllerClass=ucfirst($controllerID).'Controller';
$controllerFile=$module->controllerPath.DIRECTORY_SEPARATOR.$controllerClass.'.php';
$controllerID[0]=strtolower($controllerID[0]);
}
else
{
$last=substr($controllerID,$pos+1);
$last[0]=strtolower($last[0]);
$pos2=strpos($controllerID,'/');
$first=substr($controllerID,0,$pos2);
$middle=$pos===$pos2?'':substr($controllerID,$pos2+1,$pos-$pos2);
$controllerClass=ucfirst($last).'Controller';
$controllerFile=($middle===''?'':$middle.'/').$controllerClass.'.php';
$controllerID=$middle===''?$last:$middle.'/'.$last;
if(($m=Yii::app()->getModule($first))!==null)
$module=$m;
else
{
$controllerFile=$first.'/'.$controllerClass.'.php';
$controllerID=$first.'/'.$controllerID;
}
$controllerFile=$module->controllerPath.DIRECTORY_SEPARATOR.str_replace('/',DIRECTORY_SEPARATOR,$controllerFile);
}
$args[]='index';
$actions=array_unique(array_splice($args,1));
$templatePath=$this->templatePath===null?YII_PATH.'/cli/views/shell/controller':$this->templatePath;
$list=array(
basename($controllerFile)=>array(
'source'=>$templatePath.DIRECTORY_SEPARATOR.'controller.php',
'target'=>$controllerFile,
'callback'=>array($this,'generateController'),
'params'=>array($controllerClass, $actions),
),
);
$viewPath=$module->viewPath.DIRECTORY_SEPARATOR.str_replace('/',DIRECTORY_SEPARATOR,$controllerID);
foreach($actions as $name)
{
$list[$name.'.php']=array(
'source'=>$templatePath.DIRECTORY_SEPARATOR.'view.php',
'target'=>$viewPath.DIRECTORY_SEPARATOR.$name.'.php',
'callback'=>array($this,'generateAction'),
'params'=>array('controller'=>$controllerClass, 'action'=>$name),
);
}
$this->copyFiles($list);
if($module instanceof CWebModule)
$moduleID=$module->id.'/';
else
$moduleID='';
echo <<<EOD
Controller '{$controllerID}' has been created in the following file:
$controllerFile
You may access it in the browser using the following URL:
http://hostname/path/to/index.php?r={$moduleID}{$controllerID}
EOD;
}
public function generateController($source,$params)
{
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/controller/'.basename($source);
return $this->renderFile($source,array('className'=>$params[0],'actions'=>$params[1]),true);
}
public function generateAction($source,$params)
{
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/controller/'.basename($source);
return $this->renderFile($source,$params,true);
}
}
\ No newline at end of file
<?php
/**
* CrudCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* CrudCommand generates code implementing CRUD operations.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands.shell
* @since 1.0
*/
class CrudCommand extends CConsoleCommand
{
/**
* @var string the directory that contains templates for crud commands.
* Defaults to null, meaning using 'framework/cli/views/shell/crud'.
* If you set this path and some views are missing in the directory,
* the default views will be used.
*/
public $templatePath;
/**
* @var string the directory that contains functional test classes.
* Defaults to null, meaning using 'protected/tests/functional'.
* If this is false, it means functional test file should NOT be generated.
*/
public $functionalTestPath;
/**
* @var array list of actions to be created. Each action must be associated with a template file with the same name.
*/
public $actions=array('create','update','index','view','admin','_form','_view','_search');
public function getHelp()
{
return <<<EOD
USAGE
crud <model-class> [controller-ID] ...
DESCRIPTION
This command generates a controller and views that accomplish
CRUD operations for the specified data model.
PARAMETERS
* model-class: required, the name of the data model class. This can
also be specified as a path alias (e.g. application.models.Post).
If the model class belongs to a module, it should be specified
as 'ModuleID.models.ClassName'.
* controller-ID: optional, the controller ID (e.g. 'post').
If this is not specified, the model class name will be used
as the controller ID. In this case, if the model belongs to
a module, the controller will also be created under the same
module.
If the controller should be located under a subdirectory,
please specify the controller ID as 'path/to/ControllerID'
(e.g. 'admin/user').
If the controller belongs to a module (different from the module
that the model belongs to), please specify the controller ID
as 'ModuleID/ControllerID' or 'ModuleID/path/to/Controller'.
EXAMPLES
* Generates CRUD for the Post model:
crud Post
* Generates CRUD for the Post model which belongs to module 'admin':
crud admin.models.Post
* Generates CRUD for the Post model. The generated controller should
belong to module 'admin', but not the model class:
crud Post admin/post
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: data model class is required.\n";
echo $this->getHelp();
return 1;
}
$module=Yii::app();
$modelClass=$args[0];
if(($pos=strpos($modelClass,'.'))===false)
$modelClass='application.models.'.$modelClass;
else
{
$id=substr($modelClass,0,$pos);
if(($m=Yii::app()->getModule($id))!==null)
$module=$m;
}
$modelClass=Yii::import($modelClass);
if(isset($args[1]))
{
$controllerID=$args[1];
if(($pos=strrpos($controllerID,'/'))===false)
{
$controllerClass=ucfirst($controllerID).'Controller';
$controllerFile=$module->controllerPath.DIRECTORY_SEPARATOR.$controllerClass.'.php';
$controllerID[0]=strtolower($controllerID[0]);
}
else
{
$last=substr($controllerID,$pos+1);
$last[0]=strtolower($last);
$pos2=strpos($controllerID,'/');
$first=substr($controllerID,0,$pos2);
$middle=$pos===$pos2?'':substr($controllerID,$pos2+1,$pos-$pos2);
$controllerClass=ucfirst($last).'Controller';
$controllerFile=($middle===''?'':$middle.'/').$controllerClass.'.php';
$controllerID=$middle===''?$last:$middle.'/'.$last;
if(($m=Yii::app()->getModule($first))!==null)
$module=$m;
else
{
$controllerFile=$first.'/'.$controllerFile;
$controllerID=$first.'/'.$controllerID;
}
$controllerFile=$module->controllerPath.DIRECTORY_SEPARATOR.str_replace('/',DIRECTORY_SEPARATOR,$controllerFile);
}
}
else
{
$controllerID=$modelClass;
$controllerClass=ucfirst($controllerID).'Controller';
$controllerFile=$module->controllerPath.DIRECTORY_SEPARATOR.$controllerClass.'.php';
$controllerID[0]=strtolower($controllerID[0]);
}
$templatePath=$this->templatePath===null?YII_PATH.'/cli/views/shell/crud':$this->templatePath;
$functionalTestPath=$this->functionalTestPath===null?Yii::getPathOfAlias('application.tests.functional'):$this->functionalTestPath;
$viewPath=$module->viewPath.DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,$controllerID);
$fixtureName=$this->pluralize($modelClass);
$fixtureName[0]=strtolower($fixtureName);
$list=array(
basename($controllerFile)=>array(
'source'=>$templatePath.'/controller.php',
'target'=>$controllerFile,
'callback'=>array($this,'generateController'),
'params'=>array($controllerClass,$modelClass),
),
);
if($functionalTestPath!==false)
{
$list[$modelClass.'Test.php']=array(
'source'=>$templatePath.'/test.php',
'target'=>$functionalTestPath.DIRECTORY_SEPARATOR.$modelClass.'Test.php',
'callback'=>array($this,'generateTest'),
'params'=>array($controllerID,$fixtureName,$modelClass),
);
}
foreach($this->actions as $action)
{
$list[$action.'.php']=array(
'source'=>$templatePath.'/'.$action.'.php',
'target'=>$viewPath.'/'.$action.'.php',
'callback'=>array($this,'generateView'),
'params'=>$modelClass,
);
}
$this->copyFiles($list);
if($module instanceof CWebModule)
$moduleID=$module->id.'/';
else
$moduleID='';
echo "\nCrud '{$controllerID}' has been successfully created. You may access it via:\n";
echo "http://hostname/path/to/index.php?r={$moduleID}{$controllerID}\n";
}
public function generateController($source,$params)
{
list($controllerClass,$modelClass)=$params;
$model=CActiveRecord::model($modelClass);
$id=$model->tableSchema->primaryKey;
if($id===null)
throw new ShellException(Yii::t('yii','Error: Table "{table}" does not have a primary key.',array('{table}'=>$model->tableName())));
else if(is_array($id))
throw new ShellException(Yii::t('yii','Error: Table "{table}" has a composite primary key which is not supported by crud command.',array('{table}'=>$model->tableName())));
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/crud/'.basename($source);
return $this->renderFile($source,array(
'ID'=>$id,
'controllerClass'=>$controllerClass,
'modelClass'=>$modelClass,
),true);
}
public function generateView($source,$modelClass)
{
$model=CActiveRecord::model($modelClass);
$table=$model->getTableSchema();
$columns=$table->columns;
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/crud/'.basename($source);
return $this->renderFile($source,array(
'ID'=>$table->primaryKey,
'modelClass'=>$modelClass,
'columns'=>$columns),true);
}
public function generateTest($source,$params)
{
list($controllerID,$fixtureName,$modelClass)=$params;
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/crud/'.basename($source);
return $this->renderFile($source, array(
'controllerID'=>$controllerID,
'fixtureName'=>$fixtureName,
'modelClass'=>$modelClass,
),true);
}
public function generateInputLabel($modelClass,$column)
{
return "CHtml::activeLabelEx(\$model,'{$column->name}')";
}
public function generateInputField($modelClass,$column)
{
if($column->type==='boolean')
return "CHtml::activeCheckBox(\$model,'{$column->name}')";
else if(stripos($column->dbType,'text')!==false)
return "CHtml::activeTextArea(\$model,'{$column->name}',array('rows'=>6, 'cols'=>50))";
else
{
if(preg_match('/^(password|pass|passwd|passcode)$/i',$column->name))
$inputField='activePasswordField';
else
$inputField='activeTextField';
if($column->type!=='string' || $column->size===null)
return "CHtml::{$inputField}(\$model,'{$column->name}')";
else
{
if(($size=$maxLength=$column->size)>60)
$size=60;
return "CHtml::{$inputField}(\$model,'{$column->name}',array('size'=>$size,'maxlength'=>$maxLength))";
}
}
}
public function generateActiveLabel($modelClass,$column)
{
return "\$form->labelEx(\$model,'{$column->name}')";
}
public function generateActiveField($modelClass,$column)
{
if($column->type==='boolean')
return "\$form->checkBox(\$model,'{$column->name}')";
else if(stripos($column->dbType,'text')!==false)
return "\$form->textArea(\$model,'{$column->name}',array('rows'=>6, 'cols'=>50))";
else
{
if(preg_match('/^(password|pass|passwd|passcode)$/i',$column->name))
$inputField='passwordField';
else
$inputField='textField';
if($column->type!=='string' || $column->size===null)
return "\$form->{$inputField}(\$model,'{$column->name}')";
else
{
if(($size=$maxLength=$column->size)>60)
$size=60;
return "\$form->{$inputField}(\$model,'{$column->name}',array('size'=>$size,'maxlength'=>$maxLength))";
}
}
}
public function guessNameColumn($columns)
{
foreach($columns as $column)
{
if(!strcasecmp($column->name,'name'))
return $column->name;
}
foreach($columns as $column)
{
if(!strcasecmp($column->name,'title'))
return $column->name;
}
foreach($columns as $column)
{
if($column->isPrimaryKey)
return $column->name;
}
return 'id';
}
public function class2id($className)
{
return trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/', '-\0', $className))),'-');
}
public function class2name($className,$pluralize=false)
{
if($pluralize)
$className=$this->pluralize($className);
return ucwords(trim(strtolower(str_replace(array('-','_'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $className)))));
}
}
<?php
/**
* FormCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* FormCommand generates a form view based on a specified model.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands.shell
* @since 1.0
*/
class FormCommand extends CConsoleCommand
{
/**
* @var string the directory that contains templates for the form command.
* Defaults to null, meaning using 'framework/cli/views/shell/form'.
* If you set this path and some views are missing in the directory,
* the default views will be used.
*/
public $templatePath;
public function getHelp()
{
return <<<EOD
USAGE
form <model-class> <view-name> [scenario]
DESCRIPTION
This command generates a form view that can be used to collect inputs
for the specified model.
PARAMETERS
* model-class: required, model class. This can be either the name of
the model class (e.g. 'ContactForm') or the path alias of the model
class file (e.g. 'application.models.ContactForm'). The former can
be used only if the class can be autoloaded.
* view-name: required, the name of the view to be generated. This should
be the path alias of the view script (e.g. 'application.views.site.contact').
* scenario: optional, the name of the scenario in which the model is used
(e.g. 'update', 'login'). This determines which model attributes the
generated form view will be used to collect user inputs for. If this
is not provided, the scenario will be assumed to be '' (empty string).
EXAMPLES
* Generates the view script for the 'ContactForm' model:
form ContactForm application.views.site.contact
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0],$args[1]))
{
echo "Error: both model class and view name are required.\n";
echo $this->getHelp();
return 1;
}
$scenario=isset($args[2]) ? $args[2] : '';
$modelClass=Yii::import($args[0],true);
$model=new $modelClass($scenario);
$attributes=$model->getSafeAttributeNames();
$templatePath=$this->templatePath===null?YII_PATH.'/cli/views/shell/form':$this->templatePath;
$viewPath=Yii::getPathOfAlias($args[1]);
$viewName=basename($viewPath);
$viewPath.='.php';
$params=array(
'modelClass'=>$modelClass,
'viewName'=>$viewName,
'attributes'=>$attributes,
);
$list=array(
basename($viewPath)=>array(
'source'=>$templatePath.'/form.php',
'target'=>$viewPath,
'callback'=>array($this,'generateForm'),
'params'=>$params,
),
);
$this->copyFiles($list);
$actionFile=$templatePath.'/action.php';
if(!is_file($actionFile)) // fall back to default ones
$actionFile=YII_PATH.'/cli/views/shell/form/action.php';
echo "The following form view has been successfully created:\n";
echo "\t$viewPath\n\n";
echo "You may use the following code in your controller action:\n\n";
echo $this->renderFile($actionFile,$params,true);
echo "\n";
}
public function generateForm($source,$params)
{
if(!is_file($source)) // fall back to default ones
$source=YII_PATH.'/cli/views/shell/form/'.basename($source);
return $this->renderFile($source,$params,true);
}
public function class2id($className)
{
if(strrpos($className,'Form')===strlen($className)-4)
$className=substr($className,0,strlen($className)-4);
return trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/', '-\0', $className))),'-');
}
}
\ No newline at end of file
<?php
/**
* HelpCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id$
*/
/**
* HelpCommand displays help information for commands under yiic shell.
*
* @property string $help The command description.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands.shell
* @since 1.0
*/
class HelpCommand extends CConsoleCommand
{
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer non zero application exit code for help
*/
public function run($args)
{
$runner=$this->getCommandRunner();
$commands=$runner->commands;
if(isset($args[0]))
$name=strtolower($args[0]);
if(!isset($args[0]) || !isset($commands[$name]))
{
echo <<<EOD
At the prompt, you may enter a PHP statement or one of the following commands:
EOD;
$commandNames=array_keys($commands);
sort($commandNames);
echo ' - '.implode("\n - ",$commandNames);
echo <<<EOD
Type 'help <command-name>' for details about a command.
To expand the above command list, place your command class files
under 'protected/commands/shell', or a directory specified
by the 'YIIC_SHELL_COMMAND_PATH' environment variable. The command class
must extend from CConsoleCommand.
EOD;
}
else
echo $runner->createCommand($name)->getHelp();
return 1;
}
/**
* Provides the command description.
* @return string the command description.
*/
public function getHelp()
{
return <<<EOD
USAGE
help [command-name]
DESCRIPTION
Display the help information for the specified command.
If the command name is not given, all commands will be listed.
PARAMETERS
* command-name: optional, the name of the command to show help information.
EOD;
}
}
\ No newline at end of file
<?php
/**
* ModuleCommand class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @version $Id: ModuleCommand.php 433 2008-12-30 22:59:17Z qiang.xue $
*/
/**
* ModuleCommand generates a controller class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: ModuleCommand.php 433 2008-12-30 22:59:17Z qiang.xue $
* @package system.cli.commands.shell
*/
class ModuleCommand extends CConsoleCommand
{
/**
* @var string the directory that contains templates for the module command.
* Defaults to null, meaning using 'framework/cli/views/shell/module'.
* If you set this path and some views are missing in the directory,
* the default views will be used.
*/
public $templatePath;
public function getHelp()
{
return <<<EOD
USAGE
module <module-ID>
DESCRIPTION
This command generates an application module.
PARAMETERS
* module-ID: required, module ID. It is case-sensitive.
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
* @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: module ID is required.\n";
echo $this->getHelp();
return 1;
}
$moduleID=$args[0];
$moduleClass=ucfirst($moduleID).'Module';
$modulePath=Yii::app()->getModulePath().DIRECTORY_SEPARATOR.$moduleID;
$sourceDir=$this->templatePath===null?YII_PATH.'/cli/views/shell/module':$this->templatePath;
$list=$this->buildFileList($sourceDir,$modulePath);
$list['module.php']['target']=$modulePath.DIRECTORY_SEPARATOR.$moduleClass.'.php';
$list['module.php']['callback']=array($this,'generateModuleClass');
$list['module.php']['params']=array(
'moduleClass'=>$moduleClass,
'moduleID'=>$moduleID,
);
$list[$moduleClass.'.php']=$list['module.php'];
unset($list['module.php']);
$this->copyFiles($list);
echo <<<EOD
Module '{$moduleID}' has been created under the following folder:
$modulePath
You may access it in the browser using the following URL:
http://hostname/path/to/index.php?r=$moduleID
Note, the module needs to be installed first by adding '{$moduleID}'
to the 'modules' property in the application configuration.
EOD;
}
public function generateModuleClass($source,$params)
{
return $this->renderFile($source,$params,true);
}
}
\ No newline at end of file
<?php
/**
* This is the template for generating a controller class file.
* The following variables are available in this template:
* - $className: the class name of the controller
* - $actions: a list of action names for the controller
*/
?>
<?php echo "<?php\n"; ?>
class <?php echo $className; ?> extends Controller
{
<?php foreach($actions 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
<?php
echo "<?php\n";
$controller=substr($controller,0,strlen($controller)-10);
$label=ucwords(trim(strtolower(str_replace(array('-','_','.'),' ',preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $controller)))));
if($action==='index')
{
echo "\$this->breadcrumbs=array(
'$label',
);";
}
else
{
$route=$controller.'/index';
$route[0]=strtolower($route[0]);
$action=ucfirst($action);
echo "\$this->breadcrumbs=array(
'$label'=>array('$route'),
'$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>
<?php
/**
* This is the template for generating the form view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<div class="form">
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array(
'id'=>'".$this->class2id($modelClass)."-form',
'enableAjaxValidation'=>false,
)); ?>\n"; ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo "<?php echo \$form->errorSummary(\$model); ?>\n"; ?>
<?php
foreach($columns as $column)
{
if($column->isPrimaryKey)
continue;
?>
<div class="row">
<?php echo "<?php echo ".$this->generateActiveLabel($modelClass,$column)."; ?>\n"; ?>
<?php echo "<?php echo ".$this->generateActiveField($modelClass,$column)."; ?>\n"; ?>
<?php echo "<?php echo \$form->error(\$model,'{$column->name}'); ?>\n"; ?>
</div>
<?php
}
?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton(\$model->isNewRecord ? 'Create' : 'Save'); ?>\n"; ?>
</div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?>
</div><!-- form -->
\ No newline at end of file
<?php
/**
* This is the template for generating the form view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<div class="wide form">
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl(\$this->route),
'method'=>'get',
)); ?>\n"; ?>
<?php foreach($columns as $column): ?>
<?php
$field=$this->generateInputField($modelClass,$column);
if(strpos($field,'password')!==false)
continue;
?>
<div class="row">
<?php echo "<?php echo \$form->label(\$model,'{$column->name}'); ?>\n"; ?>
<?php echo "<?php echo ".$this->generateActiveField($modelClass,$column)."; ?>\n"; ?>
</div>
<?php endforeach; ?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton('Search'); ?>\n"; ?>
</div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?>
</div><!-- search-form -->
\ No newline at end of file
<?php
/**
* This is the template for generating the partial view for rendering a single model.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<div class="view">
<?php
echo "\t<b><?php echo CHtml::encode(\$data->getAttributeLabel('{$ID}')); ?>:</b>\n";
echo "\t<?php echo CHtml::link(CHtml::encode(\$data->{$ID}), array('view', 'id'=>\$data->{$ID})); ?>\n\t<br />\n\n";
$count=0;
foreach($columns as $column)
{
if($column->isPrimaryKey)
continue;
if(++$count==7)
echo "\t<?php /*\n";
echo "\t<b><?php echo CHtml::encode(\$data->getAttributeLabel('{$column->name}')); ?>:</b>\n";
echo "\t<?php echo CHtml::encode(\$data->{$column->name}); ?>\n\t<br />\n\n";
}
if($count>=7)
echo "\t*/ ?>\n";
?>
</div>
\ No newline at end of file
<?php
/**
* This is the template for generating the admin view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<?php
echo "<?php\n";
$label=$this->class2name($modelClass,true);
echo "\$this->breadcrumbs=array(
'$label'=>array('index'),
'Manage',
);\n";
?>
$this->menu=array(
array('label'=>'List <?php echo $modelClass; ?>', 'url'=>array('index')),
array('label'=>'Create <?php echo $modelClass; ?>', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('<?php echo $this->class2id($modelClass); ?>-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage <?php echo $this->class2name($modelClass,true); ?></h1>
<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo "<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>"; ?>
<div class="search-form" style="display:none">
<?php echo "<?php \$this->renderPartial('_search',array(
'model'=>\$model,
)); ?>\n"; ?>
</div><!-- search-form -->
<?php echo "<?php"; ?> $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'<?php echo $this->class2id($modelClass); ?>-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
<?php
$count=0;
foreach($columns as $column)
{
if(++$count==7)
echo "\t\t/*\n";
echo "\t\t'".$column->name."',\n";
}
if($count>=7)
echo "\t\t*/\n";
?>
array(
'class'=>'CButtonColumn',
),
),
)); ?>
<?php
/**
* This is the template for generating the controller class file for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $controllerClass: the controller class name
* - $modelClass: the model class name
*/
?>
<?php echo "<?php\n"; ?>
class <?php echo $controllerClass; ?> extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @var CActiveRecord the currently loaded data model instance.
*/
private $_model;
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
*/
public function actionView()
{
$this->render('view',array(
'model'=>$this->loadModel(),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new <?php echo $modelClass; ?>;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['<?php echo $modelClass; ?>']))
{
$model->attributes=$_POST['<?php echo $modelClass; ?>'];
if($model->save())
$this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
*/
public function actionUpdate()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['<?php echo $modelClass; ?>']))
{
$model->attributes=$_POST['<?php echo $modelClass; ?>'];
if($model->save())
$this->redirect(array('view','id'=>$model-><?php echo $ID; ?>));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*/
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel()->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(array('index'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('<?php echo $modelClass; ?>');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new <?php echo $modelClass; ?>('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['<?php echo $modelClass; ?>']))
$model->attributes=$_GET['<?php echo $modelClass; ?>'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
*/
public function loadModel()
{
if($this->_model===null)
{
if(isset($_GET['id']))
$this->_model=<?php echo $modelClass; ?>::model()->findbyPk($_GET['id']);
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($modelClass); ?>-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
<?php
/**
* This is the template for generating the create view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<?php
echo "<?php\n";
$label=$this->class2name($modelClass,true);
echo "\$this->breadcrumbs=array(
'$label'=>array('index'),
'Create',
);\n";
?>
$this->menu=array(
array('label'=>'List <?php echo $modelClass; ?>', 'url'=>array('index')),
array('label'=>'Manage <?php echo $modelClass; ?>', 'url'=>array('admin')),
);
?>
<h1>Create <?php echo $modelClass; ?></h1>
<?php echo "<?php echo \$this->renderPartial('_form', array('model'=>\$model)); ?>"; ?>
<?php
/**
* This is the template for generating the index view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<?php
echo "<?php\n";
$label=$this->class2name($modelClass,true);
$route=$modelClass.'/index';
$route[0]=strtolower($route[0]);
echo "\$this->breadcrumbs=array(
'$label',
);\n";
?>
$this->menu=array(
array('label'=>'Create <?php echo $modelClass; ?>', 'url'=>array('create')),
array('label'=>'Manage <?php echo $modelClass; ?>', 'url'=>array('admin')),
);
?>
<h1><?php echo $label; ?></h1>
<?php echo "<?php"; ?> $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
<?php
/**
* This is the template for generating the functional test for controller.
* The following variables are available in this template:
* - $controllerID: the controller ID
* - $fixtureName: the fixture name
* - $modelClass: the model class name
*/
?>
<?php echo "<?php\n"; ?>
class <?php echo $modelClass; ?>Test extends WebTestCase
{
public $fixtures=array(
'<?php echo $fixtureName; ?>'=>'<?php echo $modelClass; ?>',
);
public function testShow()
{
$this->open('?r=<?php echo $controllerID; ?>/view&id=1');
}
public function testCreate()
{
$this->open('?r=<?php echo $controllerID; ?>/create');
}
public function testUpdate()
{
$this->open('?r=<?php echo $controllerID; ?>/update&id=1');
}
public function testDelete()
{
$this->open('?r=<?php echo $controllerID; ?>/view&id=1');
}
public function testList()
{
$this->open('?r=<?php echo $controllerID; ?>/index');
}
public function testAdmin()
{
$this->open('?r=<?php echo $controllerID; ?>/admin');
}
}
<?php
/**
* This is the template for generating the update view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<?php
echo "<?php\n";
$nameColumn=$this->guessNameColumn($columns);
$label=$this->class2name($modelClass,true);
echo "\$this->breadcrumbs=array(
'$label'=>array('index'),
\$model->{$nameColumn}=>array('view','id'=>\$model->{$ID}),
'Update',
);\n";
?>
$this->menu=array(
array('label'=>'List <?php echo $modelClass; ?>', 'url'=>array('index')),
array('label'=>'Create <?php echo $modelClass; ?>', 'url'=>array('create')),
array('label'=>'View <?php echo $modelClass; ?>', 'url'=>array('view', 'id'=>$model-><?php echo $ID; ?>)),
array('label'=>'Manage <?php echo $modelClass; ?>', 'url'=>array('admin')),
);
?>
<h1>Update <?php echo $modelClass." <?php echo \$model->{$ID}; ?>"; ?></h1>
<?php echo "<?php echo \$this->renderPartial('_form', array('model'=>\$model)); ?>"; ?>
\ No newline at end of file
<?php
/**
* This is the template for generating the 'view' view for crud.
* The following variables are available in this template:
* - $ID: the primary key name
* - $modelClass: the model class name
* - $columns: a list of column schema objects
*/
?>
<?php
echo "<?php\n";
$nameColumn=$this->guessNameColumn($columns);
$label=$this->class2name($modelClass,true);
echo "\$this->breadcrumbs=array(
'$label'=>array('index'),
\$model->{$nameColumn},
);\n";
?>
$this->menu=array(
array('label'=>'List <?php echo $modelClass; ?>', 'url'=>array('index')),
array('label'=>'Create <?php echo $modelClass; ?>', 'url'=>array('create')),
array('label'=>'Update <?php echo $modelClass; ?>', 'url'=>array('update', 'id'=>$model-><?php echo $ID; ?>)),
array('label'=>'Delete <?php echo $modelClass; ?>', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model-><?php echo $ID; ?>),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage <?php echo $modelClass; ?>', 'url'=>array('admin')),
);
?>
<h1>View <?php echo $modelClass." #<?php echo \$model->{$ID}; ?>"; ?></h1>
<?php echo "<?php"; ?> $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
<?php
foreach($columns as $column)
echo "\t\t'".$column->name."',\n";
?>
),
)); ?>
<?php
/**
* This is the template for generating the action script for the form.
* The following variables are available in this template:
* - $modelClass: the model class name
* - $viewName: the name of the view
*/
?>
<?php
$actionName=$modelClass;
if(strrpos($modelClass,'Form')===strlen($modelClass)-4)
$actionName=substr($modelClass,0,strlen($modelClass)-4);
?>
public function action<?php echo $actionName; ?>()
{
$model=new <?php echo $modelClass; ?>;
// uncomment the following code to enable ajax-based validation
/*
if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($modelClass); ?>-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
*/
if(isset($_POST['<?php echo $modelClass; ?>']))
{
$model->attributes=$_POST['<?php echo $modelClass; ?>'];
if($model->validate())
{
// form inputs are valid, do something here
return;
}
}
$this->render('<?php echo $viewName; ?>',array('model'=>$model));
}
\ No newline at end of file
<?php
/**
* This is the template for generating the form view for the specified model.
* The following variables are available in this template:
* - $modelClass: the model class name
* - $attributes: a list of attribute names to receive form inputs
*/
?>
<div class="form">
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array(
'id'=>'".$this->class2id($modelClass)."-form',
'enableAjaxValidation'=>false,
)); ?>\n"; ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo "<?php echo \$form->errorSummary(\$model); ?>\n"; ?>
<?php
foreach($attributes as $attribute)
{
?>
<div class="row">
<?php echo "<?php echo \$form->labelEx(\$model,'$attribute'); ?>\n"; ?>
<?php echo "<?php echo \$form->textField(\$model,'$attribute'); ?>\n"; ?>
<?php echo "<?php echo \$form->error(\$model,'$attribute'); ?>\n"; ?>
</div>
<?php
}
?>
<div class="row buttons">
<?php echo "<?php echo CHtml::submitButton('Submit'); ?>\n"; ?>
</div>
<?php echo "<?php \$this->endWidget(); ?>\n"; ?>
</div><!-- form -->
\ No newline at end of file
<?php
/**
* This is the template for generating the fixture file for a model class.
* The following variables are available in this template:
* - $table: the table schema
*/
?>
<?php echo "<?php\n"; ?>
return array(
/*
'sample1'=>array(
<?php foreach($table->columns as $name=>$column) {
if($table->sequenceName===null || $table->primaryKey!==$column->name)
echo "\t\t'$name' => '',\n";
} ?>
),
'sample2'=>array(
<?php foreach($table->columns as $name=>$column) {
if($table->sequenceName===null || $table->primaryKey!==$column->name)
echo "\t\t'$name' => '',\n";
} ?>
),
*/
);
<?php
/**
* This is the template for generating a model class file.
* The following variables are available in this template:
* - $className: the class name
* - $tableName: the table name
* - $columns: a list of table column schema objects
* - $rules: a list of validation rules (string)
* - $labels: a list of labels (column name => label)
* - $relations: a list of relations (string)
*/
?>
<?php echo "<?php\n"; ?>
/**
* This is the model class for table "<?php echo $tableName; ?>".
*
* The followings are the available columns in table '<?php echo $tableName; ?>':
<?php foreach($columns as $column): ?>
* @property <?php echo $column->type.' $'.$column->name."\n"; ?>
<?php endforeach; ?>
*/
class <?php echo $className; ?> extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return <?php echo $className; ?> the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return '<?php echo $tableName; ?>';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
<?php foreach($rules as $rule): ?>
<?php echo $rule.",\n"; ?>
<?php endforeach; ?>
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
<?php foreach($relations as $name=>$relation): ?>
<?php echo "'$name' => $relation,\n"; ?>
<?php endforeach; ?>
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
<?php foreach($labels as $column=>$label): ?>
<?php echo "'$column' => '$label',\n"; ?>
<?php endforeach; ?>
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
<?php
foreach($columns as $name=>$column)
{
if($column->type==='string')
{
echo "\t\t\$criteria->compare('$name',\$this->$name,true);\n\n";
}
else
{
echo "\t\t\$criteria->compare('$name',\$this->$name);\n\n";
}
}
?>
return new CActiveDataProvider('<?php echo $className; ?>', array(
'criteria'=>$criteria,
));
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!