ServerShell.php 5.1 KB

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