Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php spark serve #996

Merged
merged 2 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions serve

This file was deleted.

41 changes: 41 additions & 0 deletions system/Commands/Server/Serve.php
Original file line number Diff line number Diff line change
@@ -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}");
}
}
18 changes: 14 additions & 4 deletions rewrite.php → system/Commands/Server/rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';