首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

YII 自定义命令行工具(二)

2012-09-20 
YII 自定义命令行工具(2)1.yiic 命令用到的是yiic.php2.控制台的命令配置文件是应用的protected/config/co

YII 自定义命令行工具(2)

1.yiic 命令用到的是yiic.php
2.控制台的命令配置文件是应用的protected/config/console.php文件,系统默认的路径是protected/commands/shell如果你执行单一的任务,直接在run方法里面写,另外一种就是同写你的Controller(控制器),前面增加actionXXX

protected/extensions/clean_command/ECleanCommand.php

<?phpclass ECleanCommand extends CConsoleCommand{public $webRoot = null;public function getHelp(){$out = "Clean command allows you to clean up various temporary data Yii and an application are generating.\n\n";return $out.parent::getHelp();}public function actionCache(){$cache=Yii::app()->getComponent('cache');if($cache!==null){$cache->flush();echo "Done.\n";}else {echo "Please configure cache component.\n";}}public function actionAssets(){if(empty($this->webRoot)){echo "Please specify a path to webRoot in command properties.\n";Yii::app()->end();}$this->cleanDir($this->webRoot.'/assets');echo "Done.\n";}public function actionRuntime(){$this->cleanDir(Yii::app()->getRuntimePath());echo "Done.\n";}private function cleanDir($dir){$di = new DirectoryIterator ($dir);foreach($di as $d){if(!$d->isDot()){echo "Removed ".$d->getPathname()."\n";$this->removeDirRecursive($d->getPathname());}}}private function removeDirRecursive($dir){$files = glob($dir.'*', GLOB_MARK);foreach ($files as $file){if (is_dir($file))$this->removeDirRecursive($file);elseunlink($file);}if (is_dir($dir))rmdir($dir);}}

?console.php,commandMap配置后不需要指定yiic shell index.php

<?php// This is the configuration for yiic console application.// Any writable CConsoleApplication properties can be configured here.return array('basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..','name'=>'My Console Application','commandMap' => array('clean' => array('class' => 'ext.clean_command.ECleanCommand','webRoot'=> 'E:\Apache2\htdocs\webapp',//注意修改 class::webRoot),'rbac' => array(  'class' => 'application.commands.shell.RbacCommand',) ),);

命令行运行cd E:\Apache2\htdocs\webapp\protected\ 进入yiic.php的目录

yiic clean

Usage: E:\Apache2\htdocs\webapp\protected\yiic.php clean <action>Actions:    cache    assets    runtime

yiic clean cache
yiic clean assets

E:\Apache2\htdocs\webapp\protected>yiic clean assetsRemoved E:\Apache2\htdocs\webapp/assets\1f5cfc05Removed E:\Apache2\htdocs\webapp/assets\836290ccDone.

yiic clean runtime

?

例子2? protected/commands/shell/RbacCommand.php

<?phpclass RbacCommand extends CConsoleCommand{       private $_authManager;     public function getHelp(){return <<<EODUSAGE  rbacDESCRIPTION  This command generates an initial RBAC authorization hierarchy.EOD;}/** * Execute the action. * @param array command line parameters specific for this command */public function run($args){//ensure that an authManager is defined as this is mandatory for creating an auth heirarchyif(($this->_authManager=Yii::app()->authManager)===null){    echo "Error: an authorization manager, named 'authManager' must be con-figured to use this command.\n";echo "If you already added 'authManager' component in application con-figuration,\n";echo "please quit and re-enter the yiic shell.\n";return;}  //provide the oportunity for the use to abort the requestecho "This command will create three roles: Owner, Member, and Reader and the following premissions:\n";echo "create, read, update and delete user\n";echo "create, read, update and delete project\n";echo "create, read, update and delete issue\n";echo "Would you like to continue? [Yes|No] ";       //check the input from the user and continue if they indicated yes to the above question    if(!strncasecmp(trim(fgets(STDIN)),'y',1)) {     //first we need to remove all operations, roles, child relationship and as-signments $this->_authManager->clearAll(); //create the lowest level operations for users $this->_authManager->createOperation("createUser","create a new user");  $this->_authManager->createOperation("readUser","read user profile in-formation");  $this->_authManager->createOperation("updateUser","update a users in-formation");  $this->_authManager->createOperation("deleteUser","remove a user from a project");  //create the lowest level operations for projects $this->_authManager->createOperation("createProject","create a new project");  $this->_authManager->createOperation("readProject","read project in-formation");   $this->_authManager->createOperation("updateProject","update project information");  $this->_authManager->createOperation("deleteProject","delete a pro-ject");  //create the lowest level operations for issues $this->_authManager->createOperation("createIssue","create a new is-sue");  $this->_authManager->createOperation("readIssue","read issue informa-tion");  $this->_authManager->createOperation("updateIssue","update issue in-formation");  $this->_authManager->createOperation("deleteIssue","delete an issue from a project");      //create the reader role and add the appropriate permissions as children to this role $role=$this->_authManager->createRole("reader");  $role->addChild("readUser"); $role->addChild("readProject");  $role->addChild("readIssue");  //create the member role, and add the appropriate permissions, as well as the reader role itself, as children $role=$this->_authManager->createRole("member");  $role->addChild("reader");  $role->addChild("createIssue");  $role->addChild("updateIssue");  $role->addChild("deleteIssue");  //create the owner role, and add the appropriate permissions, as well as both the reader and member roles as children $role=$this->_authManager->createRole("owner");  $role->addChild("reader");  $role->addChild("member");     $role->addChild("createUser");  $role->addChild("updateUser");  $role->addChild("deleteUser");   $role->addChild("createProject");  $role->addChild("updateProject");  $role->addChild("deleteProject");     //provide a message indicating success     echo "Authorization hierarchy successfully generated.";        }     }}

?命令行运行

yiic help rbac

yiic rbac

?

热点排行