ServerCommand.php 5.3 KB

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