\Omega\Environment\Command ServeCommand

Serve cli command.

The ServeCommand starts a development server using PHP's built-in web server.

Summary

Methods
Properties
Constants
__construct
No public properties found
No public constants found
configure
execute
defaultName
No protected constants found
startServer
command
handleSignals
startProcess
No private properties found
No private constants found

Properties

$defaultName

$defaultName : string

Default command name.

Type

string — Holds the default command name.

Methods

__construct()

__construct() : void

Command constructor.

Returns

void —

configure()

configure() : void

Configures the current command.

Returns

void —

execute()

execute(\Symfony\Component\Console\Input\InputInterface  $input, \Symfony\Component\Console\Output\OutputInterface  $output) : int

Executes the current command.

This method starts a development server using the PHP built-in web server. You can specify the host and port for the server using the '--host' and '--port' options.

Parameters

\Symfony\Component\Console\Input\InputInterface $input

Holds an instance of InputInterface.

\Symfony\Component\Console\Output\OutputInterface $output

Holds an instance of OutputInterface.

Throws

\InvalidArgumentException

If 'APP_HOST' or 'APP_PORT' environment variables are missing

Returns

int —

Return 0 if the server started successfully, or an exit code if there was an issue.

startServer()

startServer(string  $host, string  $port, \Symfony\Component\Console\Output\OutputInterface  $output) : void

Starts a development server using PHP's built-in web server.

This method initiates a PHP built-in web server process, binds it to the specified host and port, and directs the server's output and error messages to the provided OutputInterface. It uses proc_open to manage the server process and capture its output in real-time.

The method does the following:

  • Displays a message indicating the server URL where requests are being served.
  • Constructs the command to start the PHP built-in server with the specified host and port.
  • Sets up the process with pipes for STDIN, STDOUT, and STDERR.
  • Opens the process and reads from STDOUT to write output messages to the console.
  • Closes the pipes after the process is finished.
  • Checks the exit code of the process to determine if the server started successfully or failed.
  • Writes an error message to the console if the server fails to start or if the process cannot be opened.

Parameters

string $host

Holds the host name or IP address to bind the server to.

string $port

Holds the port number to listen on.

\Symfony\Component\Console\Output\OutputInterface $output

Holds the Symfony OutputInterface used for writing messages to the console.

Throws

\RuntimeException

If there is an issue starting or managing the server process.

Returns

void —

This method does not return a value.

command()

command(string  $host, string  $port, string  $base) : string[]

Generate the command parameters for starting the PHP built-in web server.

Parameters

string $host

Holds the host name or IP address to bind the server to.

string $port

Holds the port to use for the server.

string $base

Holds the base path of the application.

Returns

string[] —

Return an array of command parameters for starting the server.

handleSignals()

handleSignals(mixed  $process) : void

Set up signal handling to gracefully terminate the PHP built-in server process.

This method enables asynchronous signals handling to allow for graceful termination of the PHP built-in server process when needed.

Parameters

mixed $process

Returns

void —

startProcess()

startProcess(string  $host, string  $port, string  $base, \Symfony\Component\Console\Output\OutputInterface  $output) : void

Start the PHP built-in server process.

This method starts the PHP built-in server process, handling signals and displaying relevant information in the console output.

Parameters

string $host

Holds the hostname or IP address to bind the server to.

string $port

Holds the port number to listen on.

string $base

Holds the base path of the server's document root.

\Symfony\Component\Console\Output\OutputInterface $output

Holds the output interface for displaying server information.

Returns

void —