Skip to content
This repository
Browse code

Added ServerShell for PHP Built-in Server.

  • Loading branch information...
commit dd524c96e19c6f7df7aafcb8132ae93cc5990d8b 1 parent 369bbcc
shin1x1 authored July 13, 2012
5  app/webroot/index.php
@@ -70,6 +70,11 @@
70 70
 	define('WWW_ROOT', dirname(__FILE__) . DS);
71 71
 }
72 72
 
  73
+// for built-in server
  74
+if (php_sapi_name() == 'cli-server') {
  75
+	$_SERVER['PHP_SELF'] = '/'.basename(__FILE__);
  76
+}
  77
+
73 78
 if (!defined('CAKE_CORE_INCLUDE_PATH')) {
74 79
 	if (function_exists('ini_set')) {
75 80
 		ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
165  lib/Cake/Console/Command/ServerShell.php
... ...
@@ -0,0 +1,165 @@
  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
+
  19
+App::uses('AppShell', 'Console/Command');
  20
+
  21
+/**
  22
+ * built-in Server Shell
  23
+ *
  24
+ * @package       Cake.Console.Command
  25
+ */
  26
+class ServerShell extends AppShell {
  27
+/**
  28
+ * Default ServerHost
  29
+ */
  30
+	const DEFAULT_HOST = 'localhost';
  31
+/**
  32
+ * Default ListenPort
  33
+ */
  34
+	const DEFAULT_PORT = 80;
  35
+
  36
+/**
  37
+ * server host
  38
+ *
  39
+ * @var string
  40
+ */
  41
+	protected $_host = null;
  42
+
  43
+/**
  44
+ * listen port
  45
+ *
  46
+ * @var string
  47
+ */
  48
+	protected $_port = null;
  49
+
  50
+/**
  51
+ * document root
  52
+ *
  53
+ * @var string
  54
+ */
  55
+	protected $_documentRoot = null;
  56
+
  57
+/**
  58
+ * Override initialize of the Shell
  59
+ *
  60
+ * @return void
  61
+ */
  62
+	public function initialize() {
  63
+		$this->_host = self::DEFAULT_HOST;
  64
+		$this->_port = self::DEFAULT_PORT;
  65
+		$this->_documentRoot = WWW_ROOT;
  66
+	}
  67
+
  68
+/**
  69
+ * Starts up the Shell and displays the welcome message.
  70
+ * Allows for checking and configuring prior to command or main execution
  71
+ *
  72
+ * Override this method if you want to remove the welcome information,
  73
+ * or otherwise modify the pre-command flow.
  74
+ *
  75
+ * @return void
  76
+ * @link http://book.cakephp.org/2.0/en/console-and-shells.html#Shell::startup
  77
+ */
  78
+	public function startup() {
  79
+		if (!empty($this->params['host'])) {
  80
+			$this->_host = $this->params['host'];
  81
+		}
  82
+		if (!empty($this->params['port'])) {
  83
+			$this->_port = $this->params['port'];
  84
+		}
  85
+		if (!empty($this->params['document_root'])) {
  86
+			$this->_documentRoot = $this->params['document_root'];
  87
+		}
  88
+
  89
+		// for windows
  90
+		if (substr($this->_documentRoot, -1, 1) == DIRECTORY_SEPARATOR) {
  91
+			$this->_documentRoot = substr($this->_documentRoot, 0, strlen($this->_documentRoot) - 1);
  92
+		}
  93
+		if (preg_match("/^([a-z]:)[\\\]+(.+)$/i", $this->_documentRoot, $m)) {
  94
+			$this->_documentRoot = $m[1].'\\'.$m[2];
  95
+		}
  96
+
  97
+		parent::startup();
  98
+	}
  99
+
  100
+/**
  101
+ * Displays a header for the shell
  102
+ *
  103
+ * @return void
  104
+ */
  105
+	protected function _welcome() {
  106
+		$this->out();
  107
+		$this->out(__d('cake_console', '<info>Welcome to CakePHP %s Console</info>', 'v' . Configure::version()));
  108
+		$this->hr();
  109
+		$this->out(__d('cake_console', 'App : %s', APP_DIR));
  110
+		$this->out(__d('cake_console', 'Path: %s', APP));
  111
+		$this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot));
  112
+		$this->hr();
  113
+	}
  114
+
  115
+/**
  116
+ * Override main() to handle action
  117
+ *
  118
+ * @return void
  119
+ */
  120
+	public function main() {
  121
+		if (version_compare(PHP_VERSION, '5.4.0') < 0) {
  122
+			$this->out(__d('cake_console', '<warning>This command is available on PHP5.4 or above</warning>'));
  123
+			return;
  124
+		}
  125
+
  126
+		$command = sprintf("php -S %s:%d -t %s",
  127
+			$this->_host,
  128
+			$this->_port,
  129
+			$this->_documentRoot
  130
+		);
  131
+
  132
+		$port = ($this->_port == self::DEFAULT_PORT) ? '' : ':'.$this->_port;
  133
+		$this->out(__d('cake_console', 'built-in server is running in http://%s%s/', $this->_host, $port));
  134
+		$ret = system($command);
  135
+	}
  136
+
  137
+/**
  138
+ * Get and configure the optionparser.
  139
+ *
  140
+ * @return ConsoleOptionParser
  141
+ */
  142
+	public function getOptionParser() {
  143
+		$parser = parent::getOptionParser();
  144
+
  145
+		$parser->addOption('host', array(
  146
+			'short' => 'H',
  147
+			'help' => __d('cake_console', 'ServerHost')
  148
+		));
  149
+		$parser->addOption('port', array(
  150
+			'short' => 'p',
  151
+			'help' => __d('cake_console', 'ListenPort')
  152
+		));
  153
+		$parser->addOption('document_root', array(
  154
+			'short' => 'd',
  155
+			'help' => __d('cake_console', 'DocumentRoot')
  156
+		));
  157
+
  158
+		$parser->description(array(
  159
+			__d('cake_console', 'PHP Built-in Server for CakePHP'),
  160
+			__d('cake_console', '<warning>[WARN] Don\'t use this at the production environment</warning>'),
  161
+		));
  162
+
  163
+		return $parser;
  164
+	}
  165
+}
5  lib/Cake/Console/Templates/skel/webroot/index.php
@@ -72,6 +72,11 @@
72 72
 	define('WWW_ROOT', dirname(__FILE__) . DS);
73 73
 }
74 74
 
  75
+// for built-in server
  76
+if (php_sapi_name() == 'cli-server') {
  77
+	$_SERVER['PHP_SELF'] = '/'.basename(__FILE__);
  78
+}
  79
+
75 80
 if (!defined('CAKE_CORE_INCLUDE_PATH')) {
76 81
 	if (function_exists('ini_set')) {
77 82
 		ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));

0 notes on commit dd524c9

Renan Gonçalves

There should be a new line in here. I thought phpcs would see that.

Kyle Robinson Young

@renansaddam Was submitting a CS patch and saw your comment. Figured I'd include them here: #928

Please sign in to comment.
Something went wrong with that request. Please try again.