ShellCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * ShellCommand class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * ShellCommand executes the specified Web application and provides a shell for interaction.
  12. *
  13. * @property string $help The help information for the shell command.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @package system.cli.commands
  17. * @since 1.0
  18. */
  19. class ShellCommand extends CConsoleCommand
  20. {
  21. /**
  22. * @return string the help information for the shell command
  23. */
  24. public function getHelp()
  25. {
  26. return <<<EOD
  27. USAGE
  28. yiic shell [entry-script | config-file]
  29. DESCRIPTION
  30. This command allows you to interact with a Web application
  31. on the command line. It also provides tools to automatically
  32. generate new controllers, views and data models.
  33. It is recommended that you execute this command under
  34. the directory that contains the entry script file of
  35. the Web application.
  36. PARAMETERS
  37. * entry-script | config-file: optional, the path to
  38. the entry script file or the configuration file for
  39. the Web application. If not given, it is assumed to be
  40. the 'index.php' file under the current directory.
  41. EOD;
  42. }
  43. /**
  44. * Execute the action.
  45. * @param array $args command line parameters specific for this command
  46. */
  47. public function run($args)
  48. {
  49. if(!isset($args[0]))
  50. $args[0]='index.php';
  51. $entryScript=isset($args[0]) ? $args[0] : 'index.php';
  52. if(($entryScript=realpath($args[0]))===false || !is_file($entryScript))
  53. $this->usageError("{$args[0]} does not exist or is not an entry script file.");
  54. // fake the web server setting
  55. $cwd=getcwd();
  56. chdir(dirname($entryScript));
  57. $_SERVER['SCRIPT_NAME']='/'.basename($entryScript);
  58. $_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME'];
  59. $_SERVER['SCRIPT_FILENAME']=$entryScript;
  60. $_SERVER['HTTP_HOST']='localhost';
  61. $_SERVER['SERVER_NAME']='localhost';
  62. $_SERVER['SERVER_PORT']=80;
  63. // reset context to run the web application
  64. restore_error_handler();
  65. restore_exception_handler();
  66. Yii::setApplication(null);
  67. Yii::setPathOfAlias('application',null);
  68. ob_start();
  69. $config=require($entryScript);
  70. ob_end_clean();
  71. // oops, the entry script turns out to be a config file
  72. if(is_array($config))
  73. {
  74. chdir($cwd);
  75. $_SERVER['SCRIPT_NAME']='/index.php';
  76. $_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME'];
  77. $_SERVER['SCRIPT_FILENAME']=$cwd.DIRECTORY_SEPARATOR.'index.php';
  78. Yii::createWebApplication($config);
  79. }
  80. restore_error_handler();
  81. restore_exception_handler();
  82. $yiiVersion=Yii::getVersion();
  83. echo <<<EOD
  84. Yii Interactive Tool v1.1 (based on Yii v{$yiiVersion})
  85. Please type 'help' for help. Type 'exit' to quit.
  86. EOD;
  87. $this->runShell();
  88. }
  89. protected function runShell()
  90. {
  91. // disable E_NOTICE so that the shell is more friendly
  92. error_reporting(E_ALL ^ E_NOTICE);
  93. $_runner_=$this->createCommandRunner();
  94. $this->addCommands($_runner_);
  95. $_commands_=$_runner_->commands;
  96. $log=Yii::app()->log;
  97. while(($_line_=$this->prompt("\n>>"))!==false)
  98. {
  99. $_line_=trim($_line_);
  100. if($_line_==='exit')
  101. return;
  102. try
  103. {
  104. $_args_=preg_split('/[\s,]+/',rtrim($_line_,';'),-1,PREG_SPLIT_NO_EMPTY);
  105. if(isset($_args_[0]) && isset($_commands_[$_args_[0]]))
  106. {
  107. $_command_=$_runner_->createCommand($_args_[0]);
  108. array_shift($_args_);
  109. $_command_->init();
  110. $_command_->run($_args_);
  111. }
  112. else
  113. echo eval($_line_.';');
  114. }
  115. catch(Exception $e)
  116. {
  117. if($e instanceof ShellException)
  118. echo $e->getMessage();
  119. else
  120. echo $e;
  121. }
  122. }
  123. }
  124. /**
  125. * Creates a commands runner
  126. * @return CConsoleCommandRunner
  127. * @since 1.1.16
  128. */
  129. protected function createCommandRunner()
  130. {
  131. return new CConsoleCommandRunner;
  132. }
  133. /**
  134. * Adds commands to runner
  135. * @param CConsoleCommandRunner $runner
  136. * @since 1.1.16
  137. */
  138. protected function addCommands(CConsoleCommandRunner $runner)
  139. {
  140. $runner->addCommands(Yii::getPathOfAlias('system.cli.commands.shell'));
  141. $runner->addCommands(Yii::getPathOfAlias('application.commands.shell'));
  142. if(($_path_=@getenv('YIIC_SHELL_COMMAND_PATH'))!==false)
  143. $runner->addCommands($_path_);
  144. }
  145. }
  146. class ShellException extends CException
  147. {
  148. }