ServerShell.php 4.0 KB

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