/src/Shell/ServerShell.php

https://github.com/LubosRemplik/cakephp · PHP · 181 lines · 88 code · 21 blank · 72 comment · 8 complexity · 916f2c0e2562548caa5922fdd86d1806 MD5 · raw file

  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 = self::DEFAULT_HOST;
  41. /**
  42. * listen port
  43. *
  44. * @var int
  45. */
  46. protected $_port = self::DEFAULT_PORT;
  47. /**
  48. * document root
  49. *
  50. * @var string
  51. */
  52. protected $_documentRoot = WWW_ROOT;
  53. /**
  54. * ini path
  55. *
  56. * @var string
  57. */
  58. protected $_iniPath = '';
  59. /**
  60. * Starts up the Shell and displays the welcome message.
  61. * Allows for checking and configuring prior to command or main execution
  62. *
  63. * Override this method if you want to remove the welcome information,
  64. * or otherwise modify the pre-command flow.
  65. *
  66. * @return void
  67. * @link https://book.cakephp.org/3.0/en/console-and-shells.html#hook-methods
  68. */
  69. public function startup()
  70. {
  71. if ($this->param('host')) {
  72. $this->_host = $this->param('host');
  73. }
  74. if ($this->param('port')) {
  75. $this->_port = $this->param('port');
  76. }
  77. if ($this->param('document_root')) {
  78. $this->_documentRoot = $this->param('document_root');
  79. }
  80. if ($this->param('ini_path')) {
  81. $this->_iniPath = $this->param('ini_path');
  82. }
  83. // For Windows
  84. if (substr($this->_documentRoot, -1, 1) === DIRECTORY_SEPARATOR) {
  85. $this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1);
  86. }
  87. if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) {
  88. $this->_documentRoot = $m[1] . '\\' . $m[2];
  89. }
  90. $this->_iniPath = rtrim($this->_iniPath, DIRECTORY_SEPARATOR);
  91. if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_iniPath, $m)) {
  92. $this->_iniPath = $m[1] . '\\' . $m[2];
  93. }
  94. parent::startup();
  95. }
  96. /**
  97. * Displays a header for the shell
  98. *
  99. * @return void
  100. */
  101. protected function _welcome()
  102. {
  103. $this->out();
  104. $this->out(sprintf('<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
  105. $this->hr();
  106. $this->out(sprintf('App : %s', APP_DIR));
  107. $this->out(sprintf('Path: %s', APP));
  108. $this->out(sprintf('DocumentRoot: %s', $this->_documentRoot));
  109. $this->out(sprintf('Ini Path: %s', $this->_iniPath));
  110. $this->hr();
  111. }
  112. /**
  113. * Override main() to handle action
  114. *
  115. * @return void
  116. */
  117. public function main()
  118. {
  119. $command = sprintf(
  120. 'php -S %s:%d -t %s',
  121. $this->_host,
  122. $this->_port,
  123. escapeshellarg($this->_documentRoot)
  124. );
  125. if (!empty($this->_iniPath)) {
  126. $command = sprintf('%s -c %s', $command, $this->_iniPath);
  127. }
  128. $command = sprintf('%s %s', $command, escapeshellarg($this->_documentRoot . '/index.php'));
  129. $port = ':' . $this->_port;
  130. $this->out(sprintf('built-in server is running in http://%s%s/', $this->_host, $port));
  131. $this->out(sprintf('You can exit with <info>`CTRL-C`</info>'));
  132. system($command);
  133. }
  134. /**
  135. * Gets the option parser instance and configures it.
  136. *
  137. * @return \Cake\Console\ConsoleOptionParser
  138. */
  139. public function getOptionParser()
  140. {
  141. $parser = parent::getOptionParser();
  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. }