Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ServerShell for PHP Built-in Server
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
/** | ||
* built-in Server Shell | ||
* | ||
* Implementation of a Cake Shell to show CakePHP core method signatures. | ||
* | ||
* PHP 5 | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since CakePHP(tm) v 1.2.0.5012 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
|
||
App::uses('AppShell', 'Console/Command'); | ||
|
||
/** | ||
* built-in Server Shell | ||
* | ||
* @package Cake.Console.Command | ||
*/ | ||
class ServerShell extends AppShell { | ||
const DEFAULT_HOST = 'localhost'; | ||
const DEFAULT_PORT = 80; | ||
|
||
/** | ||
* server host | ||
* | ||
* @var string | ||
*/ | ||
protected $_host = null; | ||
|
||
/** | ||
* listern port | ||
* | ||
* @var string | ||
*/ | ||
protected $_port = null; | ||
|
||
/** | ||
* document root | ||
* | ||
* @var string | ||
*/ | ||
protected $_documentRoot = null; | ||
|
||
/** | ||
* Override initialize of the Shell | ||
* | ||
* @return void | ||
*/ | ||
public function initialize() { | ||
$this->_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() { | ||
parent::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]; | ||
} | ||
} | ||
|
||
/** | ||
* Override main() to handle action | ||
* | ||
* @return void | ||
*/ | ||
public function main() { | ||
if (version_compare(PHP_VERSION, '5.4.0') < 0) { | ||
$this->out(__d('cake_console', '<warning>This command is available on PHP5.4 or above</warning>')); | ||
return; | ||
} | ||
|
||
$this->out(__d('cake_console', 'ServerHost : %s', $this->_host)); | ||
$this->out(__d('cake_console', 'ListenPort : %d', $this->_port)); | ||
$this->out(__d('cake_console', 'DocumentRoot: %s', $this->_documentRoot)); | ||
$this->hr(); | ||
|
||
$command = sprintf("php -S %s:%d -t %s", | ||
$this->_host, | ||
$this->_port, | ||
$this->_documentRoot | ||
); | ||
|
||
$this->out(__d('cake_console', '<warning>[WARNING] Don\'t use this at the production enviroment</warning>')); | ||
$this->out(__d('cake_console', 'built-in server is running...')); | ||
$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(__('PHP Built-in Server for CakePHP')); | ||
|
||
return $parser; | ||
} | ||
} |