Merge pull request #996 from natanfelles/cli_serve

php spark serve
This commit is contained in:
Lonnie Ezell 2018-04-24 22:06:15 -05:00 committed by GitHub
commit 162dfa6e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 40 deletions

36
serve
View File

@ -1,36 +0,0 @@
<?php
/**
* CodeIgniter PHP-Development Server Launcher
*
* This script launches the built-in PHP development server
* making sure that it knows the webroot is in the public folder,
* and using the rewrite.php file to mimic mod_rewrite functionality.
*
* The script is automatically set to the development environment
* within the rewrite.php file.
*/
$php = PHP_BINARY; // command to call PHP
/*
* Collect any user-supplied options and apply them
*/
$options = getopt(null, ['host:', 'port:']);
$host = $options['host'] ?? 'localhost';
$port = $options['port'] ?? '8080';
/*
* Get the party started
*/
require_once __DIR__.'/system/CLI/CLI.php';
\CodeIgniter\CLI\CLI::write("CodeIgniter development server started on http://{$host}:{$port}", 'green');
\CodeIgniter\CLI\CLI::write("Press Control-C to stop.");
/*
* Call PHP's built-in webserver, making sure to set our
* base path to the public folder, and to use the rewrite file
* to ensure our environment is set and it simulates basic mod_rewrite.
*/
passthru("{$php} -S {$host}:{$port} -t public/ rewrite.php");

View File

@ -0,0 +1,41 @@
<?php namespace CodeIgniter\Commands\Server;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
class Serve extends BaseCommand
{
protected $group = 'CodeIgniter';
protected $name = 'serve';
protected $description = 'Launchs the CodeIgniter PHP-Development Server.';
protected $usage = 'serve';
protected $arguments = [];
protected $options = [
'-php' => 'The PHP Binary [default: "PHP_BINARY"]',
'-host' => 'The HTTP Host [default: "localhost"]',
'-port' => 'The HTTP Host Port [default: "8080"]',
];
public function run(array $params)
{
// Collect any user-supplied options and apply them
$php = CLI::getOption('php') ?? PHP_BINARY;
$host = CLI::getOption('host') ?? 'localhost';
$port = CLI::getOption('port') ?? '8080';
// Get the party started
CLI::write("CodeIgniter development server started on http://{$host}:{$port}", 'green');
CLI::write('Press Control-C to stop.');
// Set the Front Controller path as Document Root
$docroot = FCPATH;
// Mimic Apache's mod_rewrite functionality with user settings
$rewrite = __DIR__ . '/rewrite.php';
// Call PHP's built-in webserver, making sure to set our
// base path to the public folder, and to use the rewrite file
// to ensure our environment is set and it simulates basic mod_rewrite.
passthru("{$php} -S {$host}:{$port} -t {$docroot} {$rewrite}");
}
}

View File

@ -2,27 +2,37 @@
/**
* CodeIgniter PHP-Development Server Rewrite Rules
*
* This script works with serve.php to help run a seamless
* This script works with the CLI serve command to help run a seamless
* development server based around PHP's built-in development
* server. This file simply tries to mimic Apache's mod_rewrite
* functionality so the site will operate as normal.
*/
// Avoid this file run when listing commands
if (php_sapi_name() === 'cli')
{
return;
}
// If we're serving the site locally, then we need
// to let the application know that we're in development mode
$_SERVER['CI_ENVIRONMENT'] = 'development';
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$path = __DIR__.'/public/'.ltrim($uri,'/');
// Front Controller path - expected to be in the default folder
$fcpath = realpath(__DIR__ . '/../../../public') . DIRECTORY_SEPARATOR;
// Full path
$path = $fcpath . ltrim($uri, '/');
// If $path is an existing file or folder within the public folder
// then let the request handle it like normal.
if ($uri !== '/' && (is_file($path) || is_dir($path)))
{
return false;
return false;
}
// Otherwise, we'll load the index file and let
// the framework handle the request from here.
require_once __DIR__.'/public/index.php';
require_once $fcpath . 'index.php';