ServerShell.php 3.9 KB

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