ServerShell.php 4.0 KB

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