ServerShell.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * built-in Server Shell
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 2.3.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Console\Command;
  18. use Cake\Console\Shell;
  19. use Cake\Core\Configure;
  20. /**
  21. * built-in Server Shell
  22. *
  23. */
  24. class ServerShell extends Shell {
  25. /**
  26. * Default ServerHost
  27. *
  28. * @var string
  29. */
  30. const DEFAULT_HOST = 'localhost';
  31. /**
  32. * Default ListenPort
  33. *
  34. * @var integer
  35. */
  36. const DEFAULT_PORT = 80;
  37. /**
  38. * server host
  39. *
  40. * @var string
  41. */
  42. protected $_host = null;
  43. /**
  44. * listen port
  45. *
  46. * @var string
  47. */
  48. protected $_port = null;
  49. /**
  50. * document root
  51. *
  52. * @var string
  53. */
  54. protected $_documentRoot = null;
  55. /**
  56. * Override initialize of the Shell
  57. *
  58. * @return void
  59. */
  60. public function initialize() {
  61. $this->_host = self::DEFAULT_HOST;
  62. $this->_port = self::DEFAULT_PORT;
  63. $this->_documentRoot = WWW_ROOT;
  64. }
  65. /**
  66. * Starts up the Shell and displays the welcome message.
  67. * Allows for checking and configuring prior to command or main execution
  68. *
  69. * Override this method if you want to remove the welcome information,
  70. * or otherwise modify the pre-command flow.
  71. *
  72. * @return void
  73. * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
  74. */
  75. public function startup() {
  76. if (!empty($this->params['host'])) {
  77. $this->_host = $this->params['host'];
  78. }
  79. if (!empty($this->params['port'])) {
  80. $this->_port = $this->params['port'];
  81. }
  82. if (!empty($this->params['document_root'])) {
  83. $this->_documentRoot = $this->params['document_root'];
  84. }
  85. // for windows
  86. if (substr($this->_documentRoot, -1, 1) == DIRECTORY_SEPARATOR) {
  87. $this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1);
  88. }
  89. if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) {
  90. $this->_documentRoot = $m[1] . '\\' . $m[2];
  91. }
  92. parent::startup();
  93. }
  94. /**
  95. * Displays a header for the shell
  96. *
  97. * @return void
  98. */
  99. protected function _welcome() {
  100. $this->out();
  101. $this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
  102. $this->hr();
  103. $this->out(__d('cake_console', 'App : %s', APP_DIR));
  104. $this->out(__d('cake_console', 'Path: %s', APP));
  105. $this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));
  106. $this->hr();
  107. }
  108. /**
  109. * Override main() to handle action
  110. *
  111. * @return void
  112. */
  113. public function main() {
  114. $command = sprintf("php -S %s:%d -t %s %s",
  115. $this->_host,
  116. $this->_port,
  117. escapeshellarg($this->_documentRoot),
  118. escapeshellarg($this->_documentRoot . '/index.php')
  119. );
  120. $port = ($this->_port == static::DEFAULT_PORT) ? '' : ':' . $this->_port;
  121. $this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port));
  122. system($command);
  123. }
  124. /**
  125. * Gets the option parser instance and configures it.
  126. *
  127. * @return ConsoleOptionParser
  128. */
  129. public function getOptionParser() {
  130. $parser = parent::getOptionParser();
  131. $parser->description([
  132. __d('cake_console', 'PHP Built-in Server for CakePHP'),
  133. __d('cake_console', '<warning>[WARN] Don\'t use this at the production environment</warning>'),
  134. ])->addOption('host', [
  135. 'short' => 'H',
  136. 'help' => __d('cake_console', 'ServerHost')
  137. ])->addOption('port', [
  138. 'short' => 'p',
  139. 'help' => __d('cake_console', 'ListenPort')
  140. ])->addOption('document_root', [
  141. 'short' => 'd',
  142. 'help' => __d('cake_console', 'DocumentRoot')
  143. ]);
  144. return $parser;
  145. }
  146. }