From dd524c96e19c6f7df7aafcb8132ae93cc5990d8b Mon Sep 17 00:00:00 2001 From: shin1x1 Date: Fri, 13 Jul 2012 02:29:42 +0900 Subject: [PATCH] Added ServerShell for PHP Built-in Server. --- app/webroot/index.php | 5 + lib/Cake/Console/Command/ServerShell.php | 165 ++++++++++++++++++ .../Console/Templates/skel/webroot/index.php | 5 + 3 files changed, 175 insertions(+) create mode 100644 lib/Cake/Console/Command/ServerShell.php diff --git a/app/webroot/index.php b/app/webroot/index.php index e125e09daeb..936bc9a0028 100644 --- a/app/webroot/index.php +++ b/app/webroot/index.php @@ -70,6 +70,11 @@ define('WWW_ROOT', dirname(__FILE__) . DS); } +// for built-in server +if (php_sapi_name() == 'cli-server') { + $_SERVER['PHP_SELF'] = '/'.basename(__FILE__); +} + if (!defined('CAKE_CORE_INCLUDE_PATH')) { if (function_exists('ini_set')) { ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path')); diff --git a/lib/Cake/Console/Command/ServerShell.php b/lib/Cake/Console/Command/ServerShell.php new file mode 100644 index 00000000000..99c18231d80 --- /dev/null +++ b/lib/Cake/Console/Command/ServerShell.php @@ -0,0 +1,165 @@ +_host = self::DEFAULT_HOST; + $this->_port = self::DEFAULT_PORT; + $this->_documentRoot = WWW_ROOT; + } + +/** + * Starts up the Shell and displays the welcome message. + * Allows for checking and configuring prior to command or main execution + * + * Override this method if you want to remove the welcome information, + * or otherwise modify the pre-command flow. + * + * @return void + * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup + */ + public function startup() { + if (!empty($this->params['host'])) { + $this->_host = $this->params['host']; + } + if (!empty($this->params['port'])) { + $this->_port = $this->params['port']; + } + if (!empty($this->params['document_root'])) { + $this->_documentRoot = $this->params['document_root']; + } + + // for windows + if (substr($this->_documentRoot, -1, 1) == DIRECTORY_SEPARATOR) { + $this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1); + } + if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) { + $this->_documentRoot = $m[1].'\\'.$m[2]; + } + + parent::startup(); + } + +/** + * Displays a header for the shell + * + * @return void + */ + protected function _welcome() { + $this->out(); + $this->out(__d('cake_console', 'Welcome to CakePHP %s Console', 'v' . Configure::version())); + $this->hr(); + $this->out(__d('cake_console', 'App : %s', APP_DIR)); + $this->out(__d('cake_console', 'Path: %s', APP)); + $this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot)); + $this->hr(); + } + +/** + * Override main() to handle action + * + * @return void + */ + public function main() { + if (version_compare(PHP_VERSION, '5.4.0') < 0) { + $this->out(__d('cake_console', 'This command is available on PHP5.4 or above')); + return; + } + + $command = sprintf("php -S %s:%d -t %s", + $this->_host, + $this->_port, + $this->_documentRoot + ); + + $port = ($this->_port == self::DEFAULT_PORT) ? '' : ':'.$this->_port; + $this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port)); + $ret = system($command); + } + +/** + * Get and configure the optionparser. + * + * @return ConsoleOptionParser + */ + public function getOptionParser() { + $parser = parent::getOptionParser(); + + $parser->addOption('host', array( + 'short' => 'H', + 'help' => __d('cake_console', 'ServerHost') + )); + $parser->addOption('port', array( + 'short' => 'p', + 'help' => __d('cake_console', 'ListenPort') + )); + $parser->addOption('document_root', array( + 'short' => 'd', + 'help' => __d('cake_console', 'DocumentRoot') + )); + + $parser->description(array( + __d('cake_console', 'PHP Built-in Server for CakePHP'), + __d('cake_console', '[WARN] Don\'t use this at the production environment'), + )); + + return $parser; + } +} diff --git a/lib/Cake/Console/Templates/skel/webroot/index.php b/lib/Cake/Console/Templates/skel/webroot/index.php index fd482f8c81e..be479787721 100644 --- a/lib/Cake/Console/Templates/skel/webroot/index.php +++ b/lib/Cake/Console/Templates/skel/webroot/index.php @@ -72,6 +72,11 @@ define('WWW_ROOT', dirname(__FILE__) . DS); } +// for built-in server +if (php_sapi_name() == 'cli-server') { + $_SERVER['PHP_SELF'] = '/'.basename(__FILE__); +} + if (!defined('CAKE_CORE_INCLUDE_PATH')) { if (function_exists('ini_set')) { ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));