public
Created

ServeCommand, shortcut for starting serving application to the incoming HTTP requests with PHP 5.4 built-in web server

  • Download Gist
ServeCommand.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
<?php
/**
* ServeCommand class file.
*
* @author resurtm <resurtm@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @since 1.1.11
*/
 
/**
* @property string $help The help information for the serve command.
*
* @author resurtm <resurtm@gmail.com>
* @package system.cli.commands
* @since 1.1.11
*/
class ServeCommand extends CConsoleCommand
{
/**
* @return string the help information for the serve command
*/
public function getHelp()
{
return <<<EOD
USAGE
yiic serve [address[:port [webroot]]]
 
DESCRIPTION
This command starts serving the Yii web application for incoming
HTTP requests using PHP built-in server.
 
PHP built-in server command: $ php -S localhost:8000 -t /home/user/dir
This command: $ protected/yiic serve
 
Note that you should have PHP version 5.4 or newer to use this command.
 
PARAMETERS
* address: optional, listening address. Default value is 'localhost'.
* port: optional, listening port. Default value is '8000'.
* webroot: optional, document root path. Default is a current directory.
 
EOD;
}
 
/**
* Execute the action.
* @param array $args command line parameters specific for this command
* @throws CException
*/
public function run($args)
{
// minimal PHP version is 5.4.0
if(version_compare(PHP_VERSION,'5.4.0')<0)
throw new CException('The minimal PHP version for ServeCommand is 5.4.');
 
// extract arguments
$address=isset($args[0]) ? $args[0] : 'localhost:8000';
$webroot=isset($args[1]) ? $args[1] : getcwd();
 
// append default port if it was not specified
if(strpos($address,':')===false)
$address.=':8000';
 
// additional information in CLI
echo "Serving at: {$address}\n";
echo "Web root is: {$webroot}\n";
 
// now start built-in PHP web server
exec(PHP_BINARY." -S {$address} -t {$webroot}");
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.